Problem 2150 Fire Game
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.
4 3 3 .#. ### .#. 3 3 .#. #.# .#. 3 3 ... #.# ... 3 3 ### ..# #.#
Case 1: 1 Case 2: -1 Case 3: 0 Case 4: 2
::今天组队赛我的时间基本都用在这道题上了,马力不强,还是要多注意细节
主要思路: bfs 。。先bfs遍历看图的连通分量tree。若tree>2,则直接输出-1.
若tree==2,则相当于要遍历两个子图,枚举两个图遍历的起点。
若tree==1,则只要遍历一个图,注意这里可以同时点燃2个草地,枚举1个起点,固定,再枚举第2个起点。
下面的代码现在看自己都有点恐惧,汗:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <vector> #include <map> #include <queue> using namespace std; const int INF=2147483600; const int maxn=100000+10; const int mod=100000007; typedef long long ll; struct node { int x,y,st; }; int t; char g[15][15]; int biao[15][15],tt[15][15]; int dx[5]={0,0,1,-1}; int dy[5]={1,-1,0,0}; queue<node> q; int bfs(int x,int y) { node u,v; biao[x][y]=1; int ans=0; u.x=x;u.y=y;u.st=0; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); for(int i=0; i<4; i++) { int x1=u.x+dx[i],y1=u.y+dy[i]; if(g[x1][y1]=='#'&&biao[x1][y1]==0) { biao[x1][y1]=1; v.x=x1;v.y=y1;v.st=u.st+1; q.push(v); ans=max(v.st,ans); } } } return ans; } int bfs2(int x,int y,int a,int b) { node u,v; biao[x][y]=1; biao[a][b]=1; int ans=0; u.x=x;u.y=y;u.st=0; q.push(u); u.x=a;u.y=b;u.st=0; q.push(u); while(!q.empty()) { u=q.front(); q.pop(); for(int i=0; i<4; i++) { int x1=u.x+dx[i],y1=u.y+dy[i]; if(g[x1][y1]=='#'&&biao[x1][y1]==0) { biao[x1][y1]=1; v.x=x1;v.y=y1;v.st=u.st+1; q.push(v); ans=max(v.st,ans); } } } return ans; } int is_ok(int n,int m) { memset(biao,0,sizeof(biao)); int i,j; for(i=1; i<=n ;i++) { for(j=1; j<=m; j++) { if(g[i][j]=='#') { bfs(i,j); break; } } if(j<=m) break; } for(i=1; i<=n ;i++) { for(j=1; j<=m; j++) { if(g[i][j]=='#'&&biao[i][j]==0) { bfs(i,j); break; } } if(j<=m) break; } if(i>n) return 1; for(i=1; i<=n; i++) { for(j=1; j<=m; j++) { if(g[i][j]=='#'&&biao[i][j]==0) return 0; } } return 2; } void fu(int n,int m,int a[15][15],int b[15][15]) { for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) a[i][j]=b[i][j]; } } void run() { int T,cas=1; scanf("%d",&T); while(T--) { int n,m,i; scanf("%d%d",&n,&m); for(i=0; i<=m+1; i++)//把边界附上值' .',方便后来处理 { g[0][i]='.'; g[n+1][i]='.'; } for(int i=1; i<=n; i++) { scanf("%s",g[i]+1); g[i][0]=g[i][m+1]='.';// } int j,k,h,a,b,ans=INF; //show(n,m); int tree=is_ok(n,m); printf("Case %d: ",cas++); if(tree==0) {printf("-1\n"); continue;} for(i=1; i<=n; i++) { for(j=1; j<=m ;j++) { if(g[i][j]=='#') { memset(biao,0,sizeof(biao)); if(tree==2)//两个子图 { a=bfs(i,j); for(k=i; k<=n; k++ ) { if(k==i) h=j+1; else h=1; for( ;h<=m ; h++) { if(biao[k][h]==0&&g[k][h]=='#') { fu(n,m,tt,biao); b=bfs(k,h); fu(n,m,biao,tt); int tmp=max(a,b); ans=min(tmp,ans); } } } } else//一个子图,固定起点i,j,枚举其后的点 { for(k=i; k<=n; k++) { if(i==k) h=k+1; else h=1; for(; h<=m; h++) { biao[i][j]=1; if(biao[k][h]==0&&g[k][h]=='#') { a=bfs2(i,j,k,h); ans=min(ans,a); memset(biao,0,sizeof(biao)); } } } } } } } if(ans==INF) ans=0; printf("%d\n",ans); } } int main() { //freopen("in.txt","r",stdin); run(); return 0; }