FZU Problem 2150 Fire Game
Problem 2150 Fire Game
Accept: 145 Submit: 542
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
::今天组队赛我的时间基本都用在这道题上了,马力不强,还是要多注意细节
主要思路: bfs 。。先bfs遍历看图的连通分量tree。若tree>2,则直接输出-1.
若tree==2,则相当于要遍历两个子图,枚举两个图遍历的起点。
若tree==1,则只要遍历一个图,注意这里可以同时点燃2个草地,枚举1个起点,固定,再枚举第2个起点。
下面的代码现在看自己都有点恐惧,汗:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 using namespace std; 10 const int INF=2147483600; 11 const int maxn=100000+10; 12 const int mod=100000007; 13 typedef long long ll; 14 struct node 15 { 16 int x,y,st; 17 }; 18 int t; 19 char g[15][15]; 20 int biao[15][15],tt[15][15]; 21 int dx[5]={0,0,1,-1}; 22 int dy[5]={1,-1,0,0}; 23 queue<node> q; 24 25 int bfs(int x,int y) 26 { 27 node u,v; 28 biao[x][y]=1; 29 int ans=0; 30 u.x=x;u.y=y;u.st=0; 31 q.push(u); 32 while(!q.empty()) 33 { 34 u=q.front(); 35 q.pop(); 36 for(int i=0; i<4; i++) 37 { 38 int x1=u.x+dx[i],y1=u.y+dy[i]; 39 if(g[x1][y1]=='#'&&biao[x1][y1]==0) 40 { 41 biao[x1][y1]=1; 42 v.x=x1;v.y=y1;v.st=u.st+1; 43 q.push(v); 44 ans=max(v.st,ans); 45 } 46 } 47 } 48 return ans; 49 } 50 51 int bfs2(int x,int y,int a,int b) 52 { 53 node u,v; 54 biao[x][y]=1; 55 biao[a][b]=1; 56 int ans=0; 57 u.x=x;u.y=y;u.st=0; 58 q.push(u); 59 u.x=a;u.y=b;u.st=0; 60 q.push(u); 61 while(!q.empty()) 62 { 63 u=q.front(); 64 q.pop(); 65 for(int i=0; i<4; i++) 66 { 67 int x1=u.x+dx[i],y1=u.y+dy[i]; 68 if(g[x1][y1]=='#'&&biao[x1][y1]==0) 69 { 70 biao[x1][y1]=1; 71 v.x=x1;v.y=y1;v.st=u.st+1; 72 q.push(v); 73 ans=max(v.st,ans); 74 } 75 } 76 } 77 return ans; 78 } 79 80 int is_ok(int n,int m) 81 { 82 memset(biao,0,sizeof(biao)); 83 int i,j; 84 for(i=1; i<=n ;i++) 85 { 86 for(j=1; j<=m; j++) 87 { 88 if(g[i][j]=='#') 89 { 90 bfs(i,j); 91 break; 92 } 93 } 94 if(j<=m) break; 95 } 96 97 for(i=1; i<=n ;i++) 98 { 99 for(j=1; j<=m; j++) 100 { 101 if(g[i][j]=='#'&&biao[i][j]==0) 102 { 103 bfs(i,j); 104 break; 105 } 106 } 107 if(j<=m) break; 108 } 109 if(i>n) return 1; 110 111 for(i=1; i<=n; i++) 112 { 113 for(j=1; j<=m; j++) 114 { 115 if(g[i][j]=='#'&&biao[i][j]==0) 116 return 0; 117 } 118 } 119 return 2; 120 } 121 122 void fu(int n,int m,int a[15][15],int b[15][15]) 123 { 124 for(int i=1; i<=n; i++) 125 { 126 for(int j=1; j<=m; j++) 127 a[i][j]=b[i][j]; 128 } 129 } 130 131 void run() 132 { 133 int T,cas=1; 134 scanf("%d",&T); 135 while(T--) 136 { 137 int n,m,i; 138 scanf("%d%d",&n,&m); 139 for(i=0; i<=m+1; i++)//把边界附上值' .',方便后来处理 140 { 141 g[0][i]='.'; 142 g[n+1][i]='.'; 143 } 144 for(int i=1; i<=n; i++) 145 { 146 scanf("%s",g[i]+1); 147 g[i][0]=g[i][m+1]='.';// 148 } 149 int j,k,h,a,b,ans=INF; 150 //show(n,m); 151 int tree=is_ok(n,m); 152 printf("Case %d: ",cas++); 153 if(tree==0) {printf("-1\n"); continue;} 154 155 for(i=1; i<=n; i++) 156 { 157 for(j=1; j<=m ;j++) 158 { 159 if(g[i][j]=='#') 160 { 161 memset(biao,0,sizeof(biao)); 162 if(tree==2)//两个子图 163 { 164 a=bfs(i,j); 165 for(k=i; k<=n; k++ ) 166 { 167 if(k==i) h=j+1; 168 else h=1; 169 for( ;h<=m ; h++) 170 { 171 if(biao[k][h]==0&&g[k][h]=='#') 172 { 173 fu(n,m,tt,biao); 174 b=bfs(k,h); 175 fu(n,m,biao,tt); 176 int tmp=max(a,b); 177 ans=min(tmp,ans); 178 } 179 } 180 } 181 182 } 183 else//一个子图,固定起点i,j,枚举其后的点 184 { 185 for(k=i; k<=n; k++) 186 { 187 if(i==k) h=k+1; else h=1; 188 for(; h<=m; h++) 189 { 190 biao[i][j]=1; 191 if(biao[k][h]==0&&g[k][h]=='#') 192 { 193 a=bfs2(i,j,k,h); 194 ans=min(ans,a); 195 memset(biao,0,sizeof(biao)); 196 } 197 } 198 } 199 200 } 201 } 202 203 } 204 } 205 if(ans==INF) ans=0; 206 printf("%d\n",ans); 207 } 208 } 209 210 int main() 211 { 212 //freopen("in.txt","r",stdin); 213 run(); 214 return 0; 215 }