Problem 2150 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
4
3 3
.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1
Case 2: -1
Case 3: 0
Case 4: 2
题目类型:BFS(两个点)
算法分析:由于问题的规模比较小,所以可以枚举任意两个种草的地方并计算最小的相遇时间。如果枚举完发现还有种草的地方没有被访问到,则直接输出-1。如果发现在一次枚举过程中出现没有被访问到的节点,则不更新此时的最小值(此时包含了可能存在两个联通块的情况!!!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; char g[66][66]; int row, col, minval; bool vis[66][66]; struct node { int a, b, cnt; node (int aa, int bb, int ccnt) : a (aa), b (bb), cnt (ccnt) {} node () {} }; vector <node> aa; int dbfs (node &vala, node &valb) { memset (vis, false, sizeof (vis)); vis[vala.a][vala.b] = true, vis[valb.a][valb.b] = true; queue <node> qu; qu.push (node (vala.a, vala.b, 0)), qu.push (node (valb.a, valb.b, 0)); int res = INF; while (!qu.empty ()) { node tt = qu.front (); qu.pop (); res = tt.cnt; for (int i = 0; i < 4; i++) { int tx = tt.a + dx[i], ty = tt.b + dy[i]; if (tx >= 0 && tx <= row - 1 && ty >= 0 && ty <= col - 1 && !vis[tx][ty] && g[tx][ty] == '#') { vis[tx][ty] = true; qu.push (node (tx, ty, tt.cnt + 1)); } } } return res; } int main() { // CFF; int t, flag = 1; scanf ("%d", &t); while (t--) { printf ("Case %d: ", flag++); scanf ("%d %d", &row, &col); for (int i = 0; i < row; i++) scanf (" %s", g[i]); aa.clear (); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (g[i][j] == '#') aa.push_back (node (i, j, 0)); minval = INF; for (int i = 0; i < aa.size (); i++) { for (int j = i; j < aa.size (); j++) { int temp = dbfs (aa[i], aa[j]); bool is_valid = true; for (int k = 0; k < row && is_valid; k++) { for (int q = 0; q < col && is_valid; q++) { if (g[k][q] == '#' && !vis[k][q]) { is_valid = false; break; } } } if (is_valid) minval = min (minval, temp); } } if (minval == INF) puts ("-1"); else printf ("%d\n", minval); } return 0; } |
- « 上一篇:poj3414
- uva11624:下一篇 »