Fire Game
来源:互联网 发布:武汉js防水材料市场 编辑:程序博客网 时间:2024/06/06 07:36
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.
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
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.
43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#
Case 1: 1Case 2: -1Case 3: 0Case 4: 2
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <algorithm>#include <queue>using namespace std;const int maxn = 100+10;const int INF = 0x3f3f3f3f;int T,n,m,ans,cnt,num,step,vist[maxn][maxn];char Map[maxn][maxn];struct node{ int x,y,cnt;}a[100100];int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};int BFS(node a,node b){ vist[a.x][a.y] = vist[b.x][b.y] = 1; queue<node>Q; Q.push(a); Q.push(b); node temp1,temp2; int ttt = 0; while(!Q.empty()){ temp1 = Q.front(); ttt = max(ttt,temp1.cnt); Q.pop(); for(int i = 0; i < 4; i++){ temp2.x = temp1.x+dx[i]; temp2.y = temp1.y+dy[i]; temp2.cnt = temp1.cnt+1; if(temp2.x<n && temp2.y<m && temp2.x>=0 && temp2.y>=0 && !vist[temp2.x][temp2.y] && Map[temp2.x][temp2.y] == '#'){ vist[temp2.x][temp2.y] = 1; Q.push(temp2); } } } return ttt;}int main(){ scanf("%d",&T); for(int t = 1; t <= T; t++){ ans = INF; cnt = 0; scanf("%d %d",&n,&m); for(int i = 0; i < n; i++) scanf("%s",Map[i]); for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ if(Map[i][j] == '#'){ a[cnt].x = i; a[cnt].y = j; a[cnt++].cnt = 0; } } } printf("Case %d: ",t); if(cnt == 0 || cnt == 1 || cnt == 2){ printf("0\n"); continue; } else{ for(int i = 0; i < cnt-1; i++) for(int j = i+1; j < cnt; j++){ memset(vist,0,sizeof(vist)); step = BFS(a[i],a[j]); for(int i = 0; i < cnt; i++){ if(!vist[a[i].x][a[i].y]){ step = INF; break; } } ans = min(ans,step); } if(ans == INF) printf("-1\n"); else printf("%d\n",ans); } } return 0;}
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- Fire Game
- FZU 2150 Fire Game
- FZU 2150 Fire Game
- Problem 2150 Fire Game
- Fire Game(BFS)
- Fire Game(BFS)
- FZU 2150 Fire Game
- FZU2150 Fire Game (bfs)
- FZU 2150 Fire Game
- [BFS]HDU1045 Fire Game
- CAS框架单点登录原理解析
- Hive深入浅出
- Ios下移除原生button样式
- 实现点击ListView某一item项的选中效果
- css3 写等边三角形(非border)
- Fire Game
- 解决conda安装qt-5.6.2-vc9_1包时出现的字符集编码问题
- 十八、How many prime numbers
- unity UI学习记录
- This version of Android Studio is incompatible with the Gradle Plugin used
- C# 二进制字节流读写封装
- 非贪婪匹配:如何使用正则表达式碰到到第一个匹配到的字符串就停止
- Fastjson json和java之间转换
- redis + Tomcat 8 的session共享解决