1.题面
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2671
2.题意
在一个迷宫里,Joe希望走出去,但是某几个位置上有火种,火种和Joe一样,可以往上下左右四个方向蔓延,问Joe有没有逃生的方法
3.思路
把火种和Joe放在同一个队列里面BFS,注意区分就好
4.代码
/*****************************************************************
> File Name: acm.cpp
> Author: Uncle_Sugar
> Mail: uncle_sugar@qq.com
> Created Time: 2016/11/4 12:51:44
*****************************************************************/
# include <cstdio>
# include <cstring>
# include <cctype>
# include <cmath>
# include <cstdlib>
# include <climits>
# include <iostream>
# include <iomanip>
# include <set>
# include <map>
# include <vector>
# include <stack>
# include <queue>
# include <algorithm>
using namespace std;
const int debug = 1;
const int size = 1000 + 10;
const int INF = INT_MAX>>1;
typedef long long ll;
struct coord{
int x, y, type;
coord(){}
coord(int _x, int _y, int _type):x(_x), y(_y), type(_type){}
};
char mp[size][size];
int dist[size][size];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int r, c;
bool inrange(int x, int y){
return x>=0 && x<r && y>=0 && y<c;
}
int main(){
int T;
scanf("%d", &T);
queue<coord> que;
while (T--){
while (!que.empty()) que.pop();
memset(dist, 0, sizeof(dist));
int sx, sy;
scanf("%d%d", &r, &c);
for (int i = 0; i < r; i++) {
scanf("%s", mp[i]);
for (int j = 0; j < c; j++){
if (mp[i][j] == 'F'){
que.push(coord(i, j, 1));
dist[i][j] = 1;
}else if (mp[i][j] == 'J'){
sx = i, sy = j;
}
}
}
que.push(coord(sx, sy, -1));
dist[sx][sy] = -1;
int flag = 0;
while (!que.empty()){
coord T = que.front(); que.pop();
int x = T.x, y = T.y;
if (dist[x][y] < 0 && (x == r-1 || y == c-1 || x == 0 || y == 0)) flag = dist[x][y];
if (flag) break;
for (int i = 0; i < 4; i++){
int nx = x + dx[i];
int ny = y + dy[i];
if (inrange(nx, ny) && dist[nx][ny] == 0 && mp[nx][ny] != '#'){
dist[nx][ny] = dist[x][y] + T.type;
que.push(coord(nx, ny, T.type));
}
}
}
if (flag)
printf("%d\n", -flag);
else
printf("IMPOSSIBLE\n");
}
return 0;
}