1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
int bfs(){
int ans = -1;
int h, w, time;
input_data();
using data = tuple<int, int, int>;
queue<data> bfs;
bfs.emplace(make_tuple(0,0,0));
while(!bfs.empty())
{
tie(h, w, time) = bfs.front();
bfs.pop();
if(h < 0 || h >= H || w < 0 || w >= W)
continue;
if(h == H-1 && w == W-1)
{
ans = time;
break;
}
if(map[h][w] == '.')
{
map[h][w] = 'a';
bfs.emplace(make_tuple(h-1, w, time+1));
bfs.emplace(make_tuple(h, w+1, time+1));
bfs.emplace(make_tuple(h+1, w, time+1));
bfs.emplace(make_tuple(h, w-1, time+1));
}
}
}
|