Accept: 623 Submit: 2445
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
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 2: -1
Case 3: 0
Case 4: 2
直接把两个点放入队列进行bfs。
注意要比较时间,选择时间少的放入队列
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int INF = 0x3f3f3f3f;
char G[20][20];
int times[20][20];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
struct node{
int x,y;
}a[300],tmp,nexttmp;
int bfs(node s1,node s2){
queue<node>q;
memset(times,INF,sizeof(times));
q.push(s1);
times[s1.x][s1.y] = 0;
q.push(s2);
times[s2.x][s2.y] = 0;
while(!q.empty()){
tmp = q.front();
q.pop();
for(int i = 0; i < 4; i++){
int xx = tmp.x + dir[i][0];
int yy = tmp.y + dir[i][1];
if(xx < 0 || yy < 0 || xx >= n || yy >= m || G[xx][yy] == '.') continue;
if(times[xx][yy] < times[tmp.x][tmp.y] + 1) continue;
times[xx][yy] = times[tmp.x][tmp.y] + 1;
nexttmp.x = xx;
nexttmp.y = yy;
q.push(nexttmp);
}
}
int maxt = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(G[i][j] == '#')
maxt = max(maxt,times[i][j]);
}
}
return maxt;
}
int main(){
int t,cas = 0;
scanf("%d",&t);
while(t--){
int cnt = 0;
scanf("%d%d",&n,&m);
for(int i = 0; i < n; i++){
scanf("%s",G[i]);
for(int j = 0; j < m; j++){
if(G[i][j] == '#'){
a[cnt].x = i;
a[cnt].y = j;
cnt++;
}
}
}
printf("Case %d: ",++cas);
if(cnt <= 2){
printf("0\n");
continue;
}
int mint = INF;
for(int i = 0; i < cnt; i++){
for(int j = i+1; j < cnt; j++){
mint = min(mint,bfs(a[i],a[j]));
}
}
if(mint >= INF) printf("-1\n");
else printf("%d\n",mint);
}
return 0;
}
本文介绍了一个名为FireGame的算法问题,玩家需要在一个N*M的棋盘上点燃所有草地以进入特殊游戏阶段。文章详细阐述了如何通过广度优先搜索(BFS)找到点燃所有草地所需的最短时间。
1969

被折叠的 条评论
为什么被折叠?



