[kuangbin带你飞]专题一 简单搜索-J - Fire!

来源:互联网 发布:知乎 毕业一年后 编辑:程序博客网 时间:2024/05/18 03:30

分析:先确定火的扩散速度,当J走到该格,若时间小于扩散到该格的时间则无事,否则烧死


#include <cstdio>#include <queue>#include <iostream>#include <cstring>using namespace std;typedef long long ll;typedef pair <int, int > P;typedef struct {int a;int b;int wal;}dat;dat p;queue <dat> f;char temp[1005][1005];int maze[1005][1005];int n, m, ans, vis[1005][1005], dir[][2] = { 0,1, 1,0, 0,-1, -1,0 };dat star;void init(){cin >> n >> m;//scanf("%d %d", &n, &m);ans = 100000; star.a = 0, star.b = 0;memset(vis, 0, sizeof vis);memset(maze, 0, sizeof maze);for (int i = 1; i <= n; i++)cin >> temp[i] + 1;//scanf("%s", temp[i] + 1);for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++){if (temp[i][j] == 'J'){star.a = i;star.b = j;maze[i][j] = 0;}if (temp[i][j] == '.')maze[i][j] = 0;if (temp[i][j] == '#')maze[i][j] = -1;if (temp[i][j] == 'F'){p.a = i, p.b = j;maze[i][j] = 0;vis[i][j] = 1;p.wal = 0;f.push(p);}}//vis[star.a][star.b] = 1;}void fire(){dat now, nex;while (f.size()){now = f.front();f.pop();for (int i = 0; i<4; i++){int a, b;a = now.a + dir[i][0], b = now.b + dir[i][1];nex.a = a, nex.b = b;nex.wal = now.wal + 1;if (1 <= a&&a <= n && 1 <= b&&b <= m)if (maze[a][b] == 0&&vis[a][b]==0){maze[a][b] = now.wal + 1;f.push(nex);vis[a][b] = 1;}}}}void bfs(){memset(vis, 0, sizeof vis);vis[star.a][star.b] = 1;queue <dat> q;star.wal = 0;q.push(star);// cout<<maze[1][1]<<endl;while (q.size()){dat now = q.front();q.pop();if (now.a == 0 || now.a>n || now.b == 0 || now.b>m){if (ans > now.wal){ans = now.wal;return;}}if (maze[now.a][now.b] <= now.wal&&maze[now.a][now.b]!=0 ){continue;}for (int i = 0; i<4; i++){int a = now.a + dir[i][0], b = now.b + dir[i][1];dat nex;nex.a = a, nex.b = b;nex.wal = now.wal + 1;if (vis[nex.a][nex.b] == 0 && maze[nex.a][nex.b] != -1){q.push(nex);vis[nex.a][nex.b] = 1;}}}}void solve(){init();fire();bfs();if (ans != 100000)printf("%d\n", ans );elseprintf("IMPOSSIBLE\n");}int main(void){int t;cin >> t;while (t--){solve();}}


0 0
原创粉丝点击