FZU 2150 Fire Game (队列压入两个点)
来源:互联网 发布:淘宝购物并发技术 编辑:程序博客网 时间:2024/06/05 20:47
http://acm.fzu.edu.cn/problem.php?pid=2150
Accept: 1160 Submit: 4121
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns). At the beginning, each grid of this board is consisting of grass or just empty and then they start to fire all the grass. Firstly they choose two grids which are consisting of grass and set fire. As we all know, the fire can spread among the grass. If the grid (x, y) is firing at time t, the grid which is adjacent to this grid will fire at time t+1 which refers to the grid (x+1, y), (x-1, y), (x, y+1), (x, y-1). This process ends when no new grid get fire. If then all the grid which are consisting of grass is get fired, Fat brother and Maze will stand in the middle of the grid and playing a MORE special (hentai) game. (Maybe it’s the OOXX game which decrypted in the last problem, who knows.)
You can assume that the grass in the board would never burn out and the empty grid would never get fire.
Note that the two grids they choose can be the same.
Input
The first line of the date is an integer T, which is the number of the text cases.
Then T cases follow, each case contains two integers N and M indicate the size of the board. Then goes N line, each line with M character shows the board. “#” Indicates the grass. You can assume that there is at least one grid which is consisting of grass in the board.
1 <= T <=100, 1 <= n <=10, 1 <= m <=10
Output
For each case, output the case number first, if they can play the MORE special (hentai) game (fire all the grass), output the minimal time they need to wait after they set fire, otherwise just output -1. See the sample input and output for more details.
Sample Input
Sample Output
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <stack>#include <queue>using namespace std;#define N 106#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;#define INF 0x3f3f3f3fconst int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};struct node{ int x, y, step;}s1, s2, cnt[N];int vis[N][N], n, m, k;char g[N][N];int BFS (){ queue <node> que; que.push (s1);//将枚举的两个点进入队列 que.push (s2);//将枚举的两个点进入队列 met (vis, 0); int maxn = 0; vis[s1.x][s1.y] = 1; vis[s2.x][s2.y] = 1; while (que.size()) { node q = que.front(); que.pop(); int flag = 0; for (int i=0; i<k; i++) if (!vis[cnt[i].x][cnt[i].y]) {//判断是否还有草坪可烧 flag = 1; break; } if (!flag) return maxn;//没有草坪可烧就返回较大的那个时间 for (int i=0; i<4; i++) { node p = q; p.x += dir[i][0], p.y += dir[i][1]; if (p.x>=0 && p.x<n && p.y>=0 && p.y<m && !vis[p.x][p.y] && g[p.x][p.y]=='#') { vis[p.x][p.y] = 1; p.step++; que.push (p); maxn = max (maxn, p.step);//舍弃较短的那个时间 } } } return -1;}int main (){ int t, nCase = 1; scanf ("%d", &t); while (t--) { scanf ("%d %d", &n, &m); met (g, 0); int minx = INF; k = 0; for (int i=0; i<n; i++) { getchar (); for (int j=0; j<m; j++) { scanf ("%c", &g[i][j]); if (g[i][j] == '#')//将所有的草坪都保存在一个结构体数组里 cnt[k++] = (node){i, j, 0}; } } if (!k || k==1) { printf ("Case %d: %d\n", nCase++, 0); continue; } int flag = 0; for (int i=0; i<k-1; i++) { for (int j=i+1; j<k; j++) { s1 = cnt[i], s2 = cnt[j]; int ans = BFS (); if (ans != -1) { minx = min (minx, ans);//求得最短的时间 flag = 1; } } } if (!flag) minx = -1; printf("Case %d: %d\n", nCase++, minx); } return 0;}
- FZU 2150 Fire Game (队列压入两个点)
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- FZU-2150 Fire Game
- fzu 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150 - Fire Game
- fzu 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- FZU 2150Fire Game
- FZU 2150 Fire Game
- FZU-2150-Fire Game
- FZU 2150 Fire Game
- fzu 2150 Fire Game
- IOS随机产生字符串,数字
- 5大Android hotfix 线上热修复框架比较
- char数组转成NSString
- 如何写一个自动抢微信红包的程序
- 使用netty4 和 protobuf2.6.1进行网络通信
- FZU 2150 Fire Game (队列压入两个点)
- extjs学习总结
- JS this作用域
- springMvc integrated with springSecurity 常见问题
- 高仿百度加载图片自定义控件
- "XX cannot be resolved to a type "eclipse报错及解决说明
- Android------startActivityForResult的详细用法
- jsp性能优化
- 缓存技术PK:选择Memcached还是Redis?