kuangbin 简单搜索 I

来源:互联网 发布:js 正则大于0的正整数 编辑:程序博客网 时间:2024/05/16 20:30

I - Fire Game

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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2

题解:

没看到题目黑体字的那句话,WA了一次。
题目数据量较小,可以直接暴力枚举两两个点进行bfs。

代码:

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>using namespace std;const int INF = 0x3f3f3f3f;char maze[15][15];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};int d[15][15];struct Node{    int x,y;    Node(int x,int y):x(x),y(y){}};vector<Node> grass;int N,M;int bfs(int p,int q){   queue<Node> que;   que.push(grass[p]);   que.push(grass[q]);   d[grass[p].x][grass[p].y]=0;   d[grass[q].x][grass[q].y]=0;   while(que.size())   {       Node temp = que.front();       que.pop();       for(int i=0;i<4;i++)       {           int nx = temp.x+dir[i][0],ny = temp.y+dir[i][1];           if(0<=nx&&nx<N&&0<=ny&&ny<M&&maze[nx][ny]=='#'&&d[nx][ny]==INF)           {               d[nx][ny]=d[temp.x][temp.y]+1;               que.push(Node(nx,ny));           }       }   }   /*for(int i=0;i<N;i++)   {       for(int j=0;j<M;j++)       {          cout<<d[i][j]<<" ";       }       cout<<endl;   }    system("pause");*/    int MAX=-INF;   for(int i=0;i<grass.size();i++)   {        if(d[grass[i].x][grass[i].y]==INF)        {            return INF;        }        MAX=max(MAX,d[grass[i].x][grass[i].y]);   }   return MAX;}int main(){    int T;    cin>>T;    int ks=1;    while(T--)    {      cin>>N>>M;      for(int i=0;i<N;i++)        for(int j=0;j<M;j++)      {          cin>>maze[i][j];          if(maze[i][j]=='#')            grass.push_back(Node(i,j));      }       int siz = grass.size();       int MIN=INF;       for(int i=0;i<siz;i++)          for(int j=i;j<siz;j++)          {              memset(d,0x3f,sizeof(d));              MIN=min(bfs(i,j),MIN);          }       if(MIN==INF)       {         printf("Case %d: %d\n",ks++,-1);       }       else       {          printf("Case %d: %d\n",ks++,MIN);       }       grass.clear();    }    return 0;}
原创粉丝点击