I - Fire Game (两个点开始的bfs)

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83084#problem/I

I - Fire Game

**Time Limit:**1000MS **Memory Limit:**32768KB 64bit IO Format:%I64d & %I64u

Submit Status

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

其实一开始是没什么思路的....因为要从两个点开始,太弱,没做过这样的.

然后我看了下数据,如果枚举着火的起点 10101010100 10^6,再*bfs的时间复杂度.......

不过这是考虑所有点都是草的情况下,而且实际上枚举的时候两个点是无序的,只需要枚举一半就行...

虽然心里没什么底,我抱着写一发交上去试试tle掉又不会怀孕的心态写了下....

写的时候有两个地方卡了一下

一个是我不知道怎么表示两个点是同时烧的

bfs的时候万一写成了一个点都烧完,另一个点才开始烧,这肯定是错误的.

然后脑子里隐约记得什么控制步数blablabla,但是记得不清....

然后再一想,不用啊,两个点开始烧,我就把两个点初始化为0不就表示两个点开始烧了?

第二个地方小卡了一下,如果烧光,那答案是哪个点?

换句话说,哪个点的代价是最大的

显然是最后的那个点啊2333

然后写完了,交,竟然他妈过了....

喜极而泣!

好开心!

  1 
  2 
  3
  4    
  5    /*************************************************************************
  6    	> File Name: code/2015summer/searching/I.cpp
  7    	> Author: 111qqz
  8    	> Email: rkz2013@126.com 
  9    	> Created Time: 2015年07月27日 星期一 18时25分27秒
 10     ************************************************************************/
 11    
 12    #include<iostream>
 13    #include<iomanip>
 14    #include<cstdio>
 15    #include<algorithm>
 16    #include<cmath>
 17    #include<cstring>
 18    #include<string>
 19    #include<map>
 20    #include<set>
 21    #include<queue>
 22    #include<vector>
 23    #include<stack>
 24    #define y0 abc111qqz
 25    #define y1 hust111qqz
 26    #define yn hez111qqz
 27    #define j1 cute111qqz
 28    #define tm crazy111qqz
 29    #define lr dying111qqz
 30    using namespace std;
 31    #define REP(i, n) for (int i=0;i<int(n);++i)  
 32    typedef long long LL;
 33    typedef unsigned long long ULL;
 34    const int N=1E2+5;
 35    const int inf = 0x7fffffff;
 36    int x[N],y[N];
 37    int cnt;
 38    int m,n;
 39    char maze[11][11];
 40    int d[11][11];
 41    int dx[4]={0,0,-1,1};
 42    int dy[4]={1,-1,0,0};
 43    int k;
 44    
 45    bool ok (int x,int y)
 46    {
 47        if (x>=0&&x<n&&y>=0&&y<m&&maze[x][y]=='#'&&d[x][y]==-1)
 48    	  return true;
 49        return false;
 50    }
 51    int bfs(int x1,int y1,int x2,int y2)
 52    {
 53        memset(d,-1,sizeof(d));
 54        queue<int>x;
 55        queue<int>y;
 56        x.push(x1);
 57        x.push(x2);
 58        y.push(y1);
 59        y.push(y2);
 60        d[x1][y1]=0;
 61        d[x2][y2]=0;
 62        cnt = 2;
 63        while (!x.empty()&&!y.empty())
 64        {
 65    	  int px = x.front();x.pop();
 66    	  int py = y.front();y.pop();
 67    //	  cout<<"px:"<<px<<" py:"<<py<<endl;
 68    	  int tx,ty;
 69    	  for ( int i = 0 ; i < 4 ; i++ )
 70    	  {
 71    		   int  nx = px + dx[i];
 72    		   int  ny = py + dy[i];
 73    		if (ok(nx,ny))
 74    		{
 75    		    d[nx][ny]=d[px][py]+1;
 76    		    x.push(nx);
 77    		    y.push(ny);
 78    		    cnt++;
 79    		    tx = nx;
 80    		    ty = ny;
 81    		}
 82    	  }
 83    	  if (cnt>=k)
 84    	  {
 85    		return  d[tx][ty]; //最后一次烧到的点一定是最远的点
 86    	  }
 87    
 88        }
 89        return inf;
 90    }
 91    int main()
 92    {
 93        int T;
 94        cin>>T;
 95        int cas = 0;
 96        while (T--)
 97        {
 98    	  cas++;
 99    	  scanf("%d %d",&n,&m);
100    	  for ( int i = 0 ; i < n ; i++ )
101    	  {
102    		scanf("%s",maze[i]);
103    	  }
104    	   k = 0 ;
105    	  for ( int i = 0 ; i < n ; i++ )
106    	  {
107    		for ( int j = 0 ; j  < m ; j++ )
108    		{
109    		    if (maze[i][j]=='#')
110    		    {
111    			  k++;
112    			  x[k]=i;
113    			  y[k]=j;
114    		    }
115    		}
116    	  }
117    	  if (k<=2)
118    	  {
119    
120    		printf("Case %d: %d\n",cas,0);
121    		continue;
122    	  }
123    	  int ans = inf;
124    	  for ( int i = 1 ; i <= k ;  i++ )
125    	  {
126    		for ( int j = i ; j <= k ; j++ )
127    		{
128    		    cnt = 0 ;
129    		    ans = min(ans,bfs(x[i],y[i],x[j],y[j]));
130    		}
131    	  }
132    	  if (ans!=inf) 
133    	  printf("Case %d: %d\n",cas,ans);
134    	  else printf("Case %d: %d\n",cas,-1);
135    
136        }
137      
138    	return 0;
139    }
140    
141
142