首页 > 代码库 > FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏)
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.
胖哥和Maze 正用一块N*M的板子(N行,M列)玩一种特别(hentai)的游戏。开始时,板上的每个格子里要么是杂草丛生,要么空空如也。他们正准备烧掉所有的杂草。首先他们选了两个草格子点火。众所周知,火势可以沿着杂草传播。如果格子(x, y)在时间t被点燃,火势则会在t+1的时候蔓延到相邻格子(x+1, y), (x-1, y), (x, y+1), (x, y-1)上。这个过程会持续到火势无法传递。如果所有草格子都被点燃,胖哥和Maze 会站在格子中间并玩一个更加特别(hentai)的游戏。(或许是在最后一题后被解码的OOXX游戏,天知道。) 你可以认为格子无法烧坏,并且空格子无法燃烧。 注意,被选择的两个格子可以是同一个。
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
数据的第一行为一个整数T,表示测试用例的数量。 随后T行,每个用例有两个表示板子大小的整数N和M。随后N行,每行M个表示格子的字符。“#”表示杂草。你可以任务板上至少有一个草格子。 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.
对于每个测试用例,先输出用例编号,如果他们可以玩更特别(hentai)的游戏(点燃所有杂草),输出他们点火后需要等待的最短时间,否则输出-1。详情参照输入输出样例。
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,就是多了一个起点。
先判断杂草区域是否多余两片(只能点2次火),再进行BFS,比较每次的时间即可。
代码 C++
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #define INF 0x7F7F7F7F 6 #define MX 15 7 struct Point{ 8 int y, x; 9 }pit[MX*MX], now, nxt; 10 char map[MX][MX]; 11 int data[MX][MX], fx[8] = { 1, 0, -1, 0, 0, -1, 0, 1 }; 12 std::queue<Point> q; 13 int main(){ 14 int t, it, n, m, opt, tmpOPT, i, j, k, iP, siz, sum, tmp; 15 for (it = scanf("%d", &t); it <= t; ++it){ 16 opt = INF; iP = siz = sum = 0; 17 memset(map, ‘.‘, sizeof map); 18 scanf("%d%d ", &n, &m); 19 for (i = 1; i <= n; ++i) gets(&map[i][1]); 20 21 for (i = 1; i <= n; ++i) for (j = 1; j <= m; ++j){ 22 if (map[i][j] != ‘#‘) continue; 23 now.y = i; now.x = j; 24 q.push(now); ++siz; map[now.y][now.x] = ‘@‘; 25 while (!q.empty()){ 26 now = q.front(); q.pop(); 27 pit[iP++] = now; ++sum; 28 for (k = 0; k < 8; k += 2){ 29 nxt.y = now.y + fx[k]; nxt.x = now.x + fx[k + 1]; 30 if (map[nxt.y][nxt.x] == ‘#‘){ q.push(nxt); map[nxt.y][nxt.x] = ‘@‘; } 31 } 32 } 33 } 34 if (siz>2){ printf("Case %d: -1\n", it); continue; } 35 for (i = 0; i < iP; ++i) for (j = i; j < iP; ++j){ 36 memset(data, 0x7F, sizeof data); 37 data[pit[i].y][pit[i].x] = data[pit[j].y][pit[j].x] = 0; 38 q.push(pit[i]); q.push(pit[j]); tmpOPT = 0; 39 siz = i == j ? 1 : 2; 40 while (!q.empty()){ 41 now = q.front(); q.pop(); 42 tmpOPT = data[now.y][now.x]; tmp = tmpOPT + 1; 43 for (k = 0; k < 8; k += 2){ 44 nxt.y = now.y + fx[k]; nxt.x = now.x + fx[k + 1]; 45 if (map[nxt.y][nxt.x] == ‘@‘ && data[nxt.y][nxt.x] == INF){ 46 q.push(nxt); data[nxt.y][nxt.x] = tmp; ++siz; 47 } 48 } 49 } 50 if (siz == sum) opt = std::min(opt, tmpOPT); 51 } 52 printf("Case %d: %d\n", it, opt); 53 } 54 return 0; 55 }
FZU 2150 Fire Game(点火游戏)