题目:
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问题,可以看作由一个“超级源点”到达的这些多起点,所以在代码上的改动就是刚开始把所有的起点入队列,其他的不用变。需要注意的是记录深度最好用结构体中的变量,不能再用数组的值了。
FZU的oj不知道怎么回事,总是不太稳定,要多刷新几次才能交上去,并且莫名其妙就会compile error……
#include<iostream> #include<queue> using namespace std; struct Pos{ int x,y; int step; //Pos(int x,int y,int step):x(x),y(y),step(step){} Pos(int x,int y):x(x),y(y){} Pos(){}; }; int N=0,M=0; Pos grass[100];//所有的草地 int c[10][10]={0}; //棋盘 int time=-1;//总时间 queue<Pos> Q; //BFS队列 int g_num=0;//草地数量 int main(){ int T; cin >>T; for(int t=0;t<T;t++){ cin >>N>>M; for(int i=0;i<N;i++){ for(int j=0;j<M;j++){ char input; cin >>input; if(input==‘.‘){ c[i][j]=-1; //空地标记为-1 c[i][j]=-1; }else if(input==‘#‘){ c[i][j]=0; //草地标记为0 c[i][j]=0; Pos pos(i,j); grass[g_num++]=pos; } } } int X[]={-1,1,0,0}; int Y[]={0,0,-1,1}; //BFS for(int g=0;g<g_num;g++){ for(int gg=0;gg<g_num;gg++){ if(g<gg) continue; //根据对称性减去一些重复的情况 bool all=true; //记录是否所有草地都被覆盖 int t=0; //时间 grass[g].step=0;grass[gg].step=0; c[grass[g].x][grass[g].y]=1; c[grass[gg].x][grass[gg].y]=1; Q.push(grass[g]); //双起点BFS在开始时将两个起点都入栈即可 Q.push(grass[gg]); while(!Q.empty()){ Pos p=Q.front(); Q.pop(); if(p.step>t){ t=p.step; } for(int j=0;j<4;j++){ Pos pp(p.x+X[j],p.y+Y[j]); if(pp.x>=0&&pp.x<N&&pp.y>=0&&pp.y<M&&c[pp.x][pp.y]==0){ c[pp.x][pp.y]=1; pp.step=p.step+1; Q.push(pp); } } } //检查是否草地被全部覆盖 for(int e=0;e<N;e++){ for(int r=0;r<M;r++){ if(c[e][r]==0){ all=false; } } } if(all){ if(time<0||t<time){ time=t; } } //还原数组c for(int e=0;e<N;e++){ for(int r=0;r<M;r++){ if(c[e][r]==1){ c[e][r]=0; } } } } } cout <<"Case "<<t+1<<": "<<time<<endl; time=-1; g_num=0; } //system("pause"); return 0; }
原文:https://www.cnblogs.com/shiyu-coder/p/13155750.html