FZU 2150 - Fire Game - [BFS]
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
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
题意:
两个人在一个平面草地上某两个点放火,每一单位时间火就向四周四格蔓延(如果是草地的话),求问两个人最少用多少时间能把整片草地烧完。
题解:
很小,可以暴力枚举两个人的起始放火点,然后BFS。对于每次BFS,返回把这片草地烧完所需的时间。
能过样例的代码(因为福大的OJ崩掉了……):
#include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; const int dx[4]={1,0,-1,0}; const int dy[4]={0,1,0,-1}; int n,m; char mp[15][15]; struct Node{ int x,y; int step; Node(){} Node(int _x,int _y,int _step) { x=_x, y=_y, step=_step; } bool operator==(const Node &oth)const { return x==oth.x && y==oth.y && step==oth.step; } }; int vis[15][15]; queue<Node> Q; int bfs(Node a,Node b) { memset(vis,-1,sizeof(vis)); if(a==b) Q.push(a); else Q.push(a), Q.push(b); vis[a.x][a.y]=vis[b.x][b.y]=0; while(!Q.empty()) { Node now=Q.front(); Q.pop(); for(int k=0;k<4;k++) { Node nxt=Node(now.x+dx[k],now.y+dy[k],now.step+1); if(mp[nxt.x][nxt.y]=='.') continue; if(vis[nxt.x][nxt.y]!=-1) continue; vis[nxt.x][nxt.y]=nxt.step; Q.push(nxt); } } int res=-1; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(mp[i][j]=='.') continue; if(vis[i][j]==-1) return INF; else res=max(res,vis[i][j]); } } return res; } int main() { int T; cin>>T; for(int kase=1;kase<=T;kase++) { scanf("%d%d",&n,&m); for(int i=0;i<15;i++) for(int j=0;j<15;j++) mp[i][j]='.'; for(int i=1;i<=n;i++) scanf("%s",mp[i]+1); int ans=INF; for(int i1=1;i1<=n;i1++) { for(int j1=1;j1<=m;j1++) { if(mp[i1][j1]=='.') continue; for(int i2=1;i2<=n;i2++) { for(int j2=1;j2<=m;j2++) { if(mp[i2][j2]=='.') continue; ans=min(ans,bfs(Node(i1,j1,0),Node(i2,j2,0))); } } } } if(ans>=INF) ans=-1; printf("Case %d: %d\n",kase,ans); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
2017-10-30 UVA 12304 - 2D Geometry 110 in 1! - [平面几何基础题大集合][计算几何模板]