fzu 2150 - Fire Game解题报告

来源:互联网 发布:看电视的软件大全 编辑:程序博客网 时间:2024/06/05 01:16

#include <string.h>#include <stdio.h>#include <cmath>#include <queue>#include <vector>using namespace std;#define max(x,y) x>y?x:y;#define min(x,y) x<y?x:y;const int inf = 0x3f3f3f3f;char map[13][13];int n, m, cnt;typedef struct dd{int x, y, step;}node;vector<node> w;int in(){w.clear();scanf("%d%d\n", &m, &n);for (int i = 0; i < m; i++)scanf("%s", map[i]);cnt = 0;for (int i = 0; i < m; i++)for (int j = 0; j < n; j++)if (map[i][j] == '#'){cnt++;node b;b.x = i;b.y = j;b.step = 0;w.push_back(b );}return cnt;}int bfs(node a, node b){bool vis[13][13] = { false };int maxstep = 0, cur = 2;queue<node> q;a.step = b.step = 0;vis[a.x][a.y] = vis[b.x][b.y] = true;q.push(a); q.push(b);int dx[] = { -1, 1, 0, 0 }, dy[] = { 0, 0, -1, 1 };while (!q.empty()){a = q.front();q.pop();for (int d = 0; d < 4; d++){b.x = a.x + dx[d];b.y = a.y + dy[d];b.step = a.step + 1;if (b.x >= 0 && b.x < m&&b.y >= 0 && b.y < n&&!vis[b.x][b.y] && map[b.x][b.y] == '#'){vis[b.x][b.y] = true;q.push(b);cur++;maxstep = max(maxstep, b.step);}}}if (cur == cnt)return maxstep;elsereturn inf;}int main(){int T, cc = 1;scanf("%d\n", &T);while (T--){in(); if (cnt <= 2){printf("Case %d: %d\n", cc++, 0);continue;}int step = inf;for (int i = 0; i < w.size() - 1; i++)for (int j = i + 1; j < w.size(); j++){step = min(step, bfs(w[i], w[j]));}if (step == inf)printf("Case %d: %d\n", cc++, -1);elseprintf("Case %d: %d\n", cc++, step);}return 0;}

题目意思是胖哥和maze要在草地上玩变态游戏,必须先把草地都烧光,于是可以选择两个草地烧起并让火势蔓延,求烧光草地所用时间。

花式bfs,由于数据量少,可以枚举被烧的两处草地,注意,不能一次只用一个初始草地作为bfs的起点,否则超时,必须两个一起作为bfs的起点。代码如下


0 0
原创粉丝点击