当前位置: 代码迷 >> 综合 >> [kuangbin带你飞]专题1 简单搜索 I - Fire Game FZU - 2150
  详细解决方案

[kuangbin带你飞]专题1 简单搜索 I - Fire Game FZU - 2150

热度:82   发布时间:2023-12-12 14:22:47.0

题目:

Problem 2150 Fire Game

Accept: 2922    Submit: 10139
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


题意:有一个N*M的地,每个格子可能为空(.)可能为草地 (#) 。初始时两个人可以任意选择两块有草的格子,同时点火。如果着火的格子上/下/左/右四个方向上有草地,那么这四个方向上有草的格子将被点燃。且如果(x,y)在t秒被点燃,则被(x,y)引燃的格子在t+1秒被点燃。整个过程将在没有新格子被点燃的情况下结束。求能使所有草地都被点燃的最小时间.如果所有情况都不能,则输出-1.


思路:BFS搜索。首先枚举这两个人选择的所有情况。之后从这两个点出发宽度优先搜索。直到所有可取到的格子都被扫过。之后判断整个地上是否还有没被点着的草地。如果全部都被点着,则方案可行,更新最小的时间。否则方案不可行。


#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<set>typedef long long ll;using namespace std;int n,m;
char board[15][15];
int dir[4][2] = { 0,1 , 0,-1 , 1,0 , -1,0 };
bool vis[15][15];           //记录i,j位置的是否被烧过 struct node{int x;int y;int time;
};queue<node> q;    //queue放到里面会TLE,由于每次结束的条件都是队列为空,因此不需要重新初始化 int bfs(int ii,int jj,int r,int c){memset(vis,0,sizeof(vis));vis[ii][jj] = vis[r][c] = 1;node t;node nxt;t.x = ii, t.y = jj, t.time = 0;q.push(t);t.x = r, t.y = c, t.time = 0;q.push(t);			//初始两个人选的点加入队列 int time = 0;        //火烧的时间 while(!q.empty()){t = q.front() , q.pop();time = max(time , t.time);                 //更新火烧完的时间 for(int i = 0 ; i<4 ; i++){int tx = t.x + dir[i][0];int ty = t.y + dir[i][1];if(tx>=0 && tx<n && ty>=0 && ty<m &&    //若在i方向上走一步不会走出去 board[tx][ty]=='#' && !vis[tx][ty]){  //若下一步 是草且没被烧过 nxt.x = tx;nxt.y = ty;nxt.time = t.time + 1;vis[nxt.x][nxt.y] = 1;q.push(nxt);}}}for(int i = 0 ; i<n ; i++){for(int j = 0 ; j<m ; j++){if(board[i][j]=='#' && !vis[i][j]){return -1;                  //若火全部烧完之后还有剩余草地,则这个方案不成立 }}                     }return time;
}int main(){int t;scanf("%d",&t);for(int cases = 1 ; cases<=t ; cases++){scanf("%d %d",&n,&m);for(int i = 0 ; i<n ; i++){scanf("%s",board[i]);}int ans = 1<<30;int flag = 0;   //是否有可行解的flag for(int i = 0 ; i<n ; i++){          //枚举两人选择的两个点 for(int j = 0 ; j<m ; j++){		 //i,j是第一个人选择的点 if(board[i][j] == '#'){for(int r = 0 ; r<n ; r++){  //r,c是第二个人选择的点 for(int c = 0 ; c<m ; c++){if(board[r][c] == '#'){int temp = bfs(i,j,r,c);if(temp != -1){flag = 1;    //有可行解 ans = min(ans,temp);  //更新最小时间的解 }}}}}}}if(!flag){printf("Case %d: %d\n",cases , -1);}elseprintf("Case %d: %d\n",cases , ans);}return 0;
}

  相关解决方案