FZU 2150 Fire Game
2015-08-17 10:17
267 查看
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 gridswhich 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
43 3.#.
###
.#.
3 3
.#.
#.#
.#.
3 3
...
#.#
...
3 3
###
..#
#.#
Sample Output
Case 1: 1Case 2: -1
Case 3: 0
Case 4: 2
题目大意是选取两丛草,然后开始烧并开始蔓延,问最短多少时间可以烧完
考虑用BFS,因为数据给的实在是比较小,所以就当是基础的BFS来练了,暴力枚举所有的样例,然后BFS算出最短的时间,比较保存最短的时间
/************************************************************************* > File Name: Fire_Game.cpp > Author: Zhanghaoran0 > Mail: chiluamnxi@gmail.com > Created Time: 2015年08月02日 星期日 08时53分45秒 ************************************************************************/ #include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; int T; char a[15][15]; int n, m; int num = 0; bool flag[15][15]; int grass[201][2]; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; struct node{ int x; int y; int step; }q[100000]; int BFS(int xx, int yy){ memset(flag, false, sizeof(flag)); flag[grass[xx][0]][grass[xx][1]] = flag[grass[yy][0]][grass[yy][1]] = 1; int head = 0; int tail = 1; q[head].x = grass[xx][0]; q[head].y = grass[xx][1]; q[head].step = 0; q[tail].x = grass[yy][0]; q[tail].y = grass[yy][1]; q[tail].step = 0; tail ++; int ans = 1000001; int cas = 1; while(head < tail){ ans = q[head].step; for(int i = 0; i < 4; i ++){ q[tail].x = q[head].x + dx[i]; q[tail].y = q[head].y + dy[i]; q[tail].step = q[head].step + 1; if(q[tail].x > 0 && q[tail].y > 0 && q[tail].x <= n && q[tail].y <= m && flag[q[tail].x][q[tail].y] == false && a[q[tail].x][q[tail].y] == '#'){ flag[q[tail].x][q[tail].y] = 1; tail ++; } } head ++; } return ans; } int main(void){ int T; cin >> T; for(int cas = 1; cas <= T; cas ++){ int S = 0; cin >> n >> m; for(int i = 1; i <= n; i ++){ for(int j = 1; j <= m; j ++){ cin >> a[i][j]; if(a[i][j] == '#'){ grass[S][0] = i; grass[S ++][1] = j; } } } int ans = 1000001; for(int i = 0; i < S; i ++){ for(int j = 0; j < S; j ++){ int temp = BFS(i, j); bool suc = false; for(int k = 1; k <= n; k ++){ for(int l = 1; l <= m; l ++){ if(flag[k][l] == 0 && a[k][l] == '#'){ suc = true; break; } } if(suc == true) break; } if(!suc) ans = min(ans, temp); } } printf("Case %d: ", cas); if(ans == 1000001) printf("-1\n"); else printf("%d\n", ans); } return 0; }
相关文章推荐
- 集训第六周 数学概念与方法 UVA 11722 几何概型
- 2015届华为机试等式变换
- 使用docker实现半自动化代码自动部署与回滚
- 使用docker实现半自动化代码自动部署与回滚
- 【Matrix Factorization】林轩田机器学习技法
- 段错误调试方法
- C++便利文件夹下文件
- POJ 1183 反正切函数的应用
- 使用yum源安装CDH Hadoop集群
- [049] 微信公众平台视频公开课1说话-基础知识
- ArcGIS Runtime SDK for Android 各版本下载地址
- 对table的tr使用display:block显示colspan失效问题的解决
- HTML框架与布局
- EasyUI 基本的拖动和放置
- PostgreSQL 常用命令
- Mac制作映像文件
- Math.round(11.5)等于多少? Math.round(-11.5)等于多少?
- windows多线程同步之事件对象
- jquery当文档载入完毕就执行
- MongoDB学习