FZU 2150 Fire Game (DFS + BFS), fzubfs
On the lawn consisting of n * m grids, you can select two as grass ('#') each ignition grid, the four adjacent grass grids in the next second, will also be ignited. How long will it take to ignition all grass for at least?
The combination of DFS and BFS. If the number of # connected blocks is greater than two, it is certainly not possible to ignite all the first dfs to determine the number of connected blocks. Then, bfs can find out which two grids can be burned out as soon as possible.
#include <map>#include <cstdio>#include <cstring>using namespace std;const int N = 12;int x[] = {0, 0, -1, 1};int y[] = { -1, 1, 0, 0};char g[N][N] , v[N][N];int n, m, nu, cnt, ret;pair<int, int> q[N * N], p[N * N];void dfs(int r, int c){ if(g[r][c] != '#') return; p[nu++] = make_pair(r, c), g[r][c] = cnt; for(int i = 0; i < 4; ++i) if(r + x[i] >= 0 && c + y[i] >= 0) dfs(r + x[i], c + y[i]);}void bfs(int r, int c, int cr, int cc){ int le = 0, ri = 0; q[ri++] = make_pair(r, c); q[ri++] = make_pair(cr, cc); memset(v, 0, sizeof(v)), v[r][c] = v[cr][cc] = 1; while(le < ri) { cr = q[le].first, cc = q[le++].second, ret = v[cr][cc]; for(int i = 0; i < 4; ++i) { r = cr + x[i], c = cc + y[i]; if (r >= 0 && r < n && c >= 0 && c < m && g[r][c] != '.' && !v[r][c]) q[ri++] = make_pair(r, c), v[r][c] = v[cr][cc] + 1; } }}int main(){ int cas, ans, r1, c1, r2, c2; scanf("%d", &cas); for(int k = 1; k <= cas; ++k) { scanf("%d%d", &n, &m); for(int i = 0; i < n; ++i) scanf("%s", g[i]); printf("Case %d: ", k); nu = cnt = ans = 0; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(g[i][j] == '#') ++cnt, dfs(i, j); if(cnt < 3) { ans = n * m; for(int i = 0; i < nu; ++i) for(int j = 0; j < nu; ++j) { r1 = p[i].first, c1 = p[i].second; r2 = p[j].first, c2 = p[j].second; if(cnt == 2 && g[r1][c1] == g[r2][c2]) continue; bfs(r1, c1, r2, c2); if(ret < ans) ans = ret; } } printf("%d\n", ans - 1); } return 0;}
Problem 2150 Fire GameProblem 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 wowould never burn out and the empty grid wowould 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 Input43 3. #. ###. #. 3. #. #. #. #. 3 3... #. #... 3 3 ###.. ##. # Sample OutputCase 1: 1 Case 2:-1 Case 3: 0 Case 4: 2