Problem 2150 Fire Game (广搜+枚举)
Problem 2150 Fire Game
Accept: 3225 Submit: 11130
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
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.# Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Accept: 3225 Submit: 11130
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
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.# Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
题意:选择两个以内的#开始点火,如果能把所有的“#”字烧完就找出最短时间,否则输出-1;
思路:把所有的#字存一下,然后枚举每一对的情况找出最小值。(昨天就很BAD)
#include<stack> #include<queue> #include<math.h> #include<vector> #include<string> #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<map> #define MAXM 10005 #define memset(a,b) memset(a,b,sizeof(a)) #define ll long long #define inf 0x3f3f3f using namespace std; char s[20][20]; int b[MAXM],a[MAXM],vis[20][20],d[4][2]={1,0,-1,0,0,1,0,-1}; int ans,n,m,r,anss,q; struct node{ int x; int y; int step; }p[MAXM]; bool judge(int a,int b){ if(a>=0&&b>=0&&a<n&&b<m&&vis[a][b]==0&&s[a][b]=='#') return true; return false; } int main(){ int t;int test=1; scanf("%d",&t); while(t--){ memset(s,0);memset(p,0);memset(a,0);memset(b,0);memset(vis,0); scanf("%d%d",&n,&m);r=0; for(int i=0;i<n;i++){ scanf("%s",s[i]); for(int j=0;j<m;j++) if(s[i][j]=='#') p[r].x=i,p[r].y=j,r++; } if(r==1||r==0){printf("Case %d: 0\n",test++);continue;} q=0; for(int i=0;i<r;i++){ for(int j=i+1;j<r;j++) if(i!=j)a[q]=i,b[q]=j,q++; } anss=inf; for(int i=0;i<q;i++){ans=-1; queue<node>Q;memset(vis,0); while(!Q.empty()){Q.pop();} node src1,src2;src1.x=p[a[i]].x,src1.y=p[a[i]].y;src2.x=p[b[i]].x,src2.y=p[b[i]].y; src1.step=0;src2.step=0;Q.push(src1);Q.push(src2);vis[src1.x][src1.y]=1;vis[src2.x][src2.y]=1; while(!Q.empty()){ node c=Q.front();Q.pop(); int x=c.x,y=c.y,step=c.step; ans=max(ans,step); for(int i=0;i<4;i++){ int tx=x+d[i][0]; int ty=y+d[i][1]; if(judge(tx,ty)){ vis[tx][ty]=1; c.x=tx;c.y=ty,c.step=step+1; Q.push(c); } } } int flag=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(s[i][j]=='#'&&vis[i][j]==0){flag=1;break;} }if(flag==1)break; } if(flag==0&&ans!=-1) anss=min(ans,anss); } printf("Case %d: ",test++); if(anss==-1||anss==inf)printf("-1\n"); else printf("%d\n",anss); } }