I - Fire Game FZU - 2150
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
题意:n*m的广场,#代表草,点代表空地。现在随机选取两个草(可以重合)点燃,点燃后每一秒向上下左右扩散一格,是草则也被点燃,空地没有反应。问能如果能把所以得草点燃输出最优方案得最少时间,否则输入-1。
思路:枚举选取两堆草得所有情况,选好两堆草进行从两个点出发的bfs(宽度优先搜索)。详细见代码注释。
代码:
#include <iostream>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f
using namespace std;
typedef struct node {
int x,y;
int step;
} ty;
int n,m;
char a[11][11];
int via[11][11];
ty date[201];
ty t,d;
int bfs( int x, int y, int x1, int y1 )
{
int re;
int next[4][2] = {
{0,1},{0,-1},{1,0},{-1,0}
};
t.x = x; // 从两个点开始的bfs,只在开始的时候push两个点,之后没区别
d.x = x1;
t.y = y;
d.y = y1;
t.step = 0;
d.step = 0;
via[t.x][t.y] = 1;
via[d.x][d.y] = 1;
queue <ty> Q;
Q.push(t);
Q.push(d);
while ( !Q.empty() ) {
t = Q.front();
re = t.step;
Q.pop();
for ( int i=0; i<4; i++ ) {
d.x = t.x + next[i][0];
d.y = t.y + next[i][1];
d.step = t.step + 1;
if ( d.x<0||d.y<0||d.x>=n||d.y>=m ) {
continue ;
}
if ( a[d.x][d.y]=='#' && via[d.x][d.y]==0 ) {
Q.push(d);
via[d.x][d.y] = 1;
}
}
}
return re; // 返回最后一个点被点燃的时间
}
int main()
{
int i,j,listt,t1,t2,t,ans,ji,ii,jj;
cin >> listt;
for ( ji=1; ji<=listt; ji++ ) {
ans = INF; // 无穷大
cin >> n >> m;
int cnt = 0;
for ( i=0; i<n; i++ ) {
for ( j=0; j<m; j++ ) {
cin >> a[i][j];
if ( a[i][j]=='#' ) { // 存入date 方便枚举
date[cnt].x = i;
date[cnt].y = j;
cnt ++;
}
}
}
for ( i=0; i<cnt; i++ ) { // 枚举
for ( j=0; j<cnt; j++ ) { // 枚举
if ( i<j ) { // 减少一半的重复判断
continue ;
}
int isp = 1;
memset(via,0,sizeof(via)); // 每一次枚举完成都要重新归零
t = bfs(date[i].x,date[i].y,date[j].x,date[j].y);
for ( ii=0; ii<n; ii++ ) { // 判断广场的草是否全部被点燃
for ( jj=0; jj<m; jj++ ) {
if ( a[ii][jj]=='#' && via[ii][jj]==0 ) {
isp = 0;
}
}
}
if ( isp==1 && t<ans ) {
ans = t;
}
}
}
if ( ans==INF ) {
ans = -1;
}
cout << "Case " << ji << ": " << ans << endl;
}
return 0;
}