博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FZU 1250 BFS(两起点)
阅读量:4217 次
发布时间:2019-05-26

本文共 3834 字,大约阅读时间需要 12 分钟。

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

43 3.#.###.#.3 3.#.#.#.#.3 3...#.#...3 3###..##.#

Sample Output

Case 1: 1Case 2: -1Case 3: 0Case 4: 2

题目大意:从两个点放火,火可以上下左右蔓延,问最小需要的时间(蔓延一个需要一个时间),最少有一个地方是grass

除了grass外为空地。

 

1 A  happy

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;typedef pair
P;int dir[4][2] = { {-1,0},{0,1},{1,0},{0,-1}};int n,m;bool vis[13][13];int step[13][13];char a[11][11];bool isOk(){ for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ if(a[i][j] == '#' && !vis[i][j]) return false; } } return true;}bool check(int x, int y){ if(x < 0 || x >= n || y < 0 || y >= m) return false; if(vis[x][y]) return false; return a[x][y] == '#';}int bfs(P a, P b){ memset(vis,false,sizeof(vis)); memset(step,0,sizeof(step)); queue

q; q.push(a); vis[a.first][a.second] = true; q.push(b); vis[b.first][b.second] = true; step[a.first][a.second] = step[b.first][b.second] = 0; while(!q.empty()){ P cur = q.front(); q.pop(); if(isOk()){ // 取 grass fired 最长的那个时间 int ans = -2; for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ ans = max(ans,step[i][j]); } } return ans; } for(int i = 0; i < 4; ++i){ int nx = cur.first + dir[i][0]; int ny = cur.second + dir[i][1]; if(check(nx,ny)){ step[nx][ny] = step[cur.first][cur.second] + 1; q.push(P(nx,ny)); vis[nx][ny] = true; } } } return -1;}int main() { ios::sync_with_stdio(false); int t; int mmin; int kcase = 0; cin >> t; while(t--){ cin >> n >> m; for(int i = 0; i < n; ++i) cin >> a[i]; vector

p; int cnt = 0; // 这里 对坐标进行了转换 以单个数字表示 (x,y) 其实也可以 四层循环 for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ if(a[i][j] == '#'){ p.push_back(cnt); } ++cnt; } } // 一颗草时候 需要单独判断 if(p.size() == 1){ cout << "Case " << ++kcase << ": " << 0 << endl; continue; } // 记录是否 成功 bool flag = false; int mmin = 1 << 30; for(int i = 0; i < p.size(); ++i){ for(int j = i+1; j < p.size(); ++j){ //得到真实坐标 P p1 = P(p[i]/m,p[i]%m); P p2 = P(p[j]/m,p[j]%m); // 以两个点作为初始点 进行 搜索 int ans = bfs(p1,p2); if(ans >= 0 && mmin > ans){ mmin = ans; flag = true; } } } if(flag){ cout << "Case " << ++kcase << ": " << mmin << endl; } else{ cout << "Case " << ++kcase << ": " << -1 << endl; } } return 0; }

 

转载地址:http://fwsmi.baihongyu.com/

你可能感兴趣的文章
HashMap和Hashtable的区别
查看>>
JVM 对 Java 的原生锁做了哪些优化?
查看>>
JAVA实现简单的阻塞队列
查看>>
我的2020
查看>>
idea快捷键使用
查看>>
2.1MAC协议概述
查看>>
2.3 WSN的MAC协议
查看>>
图解后缀表达式的计算过程
查看>>
栈与队列的应用——计算表达式的值
查看>>
静态链表——sharing
查看>>
静态链表——sorting
查看>>
DFS——背包问题
查看>>
DFS——选数问题
查看>>
BFS——求矩阵中“块”的个数
查看>>
BFS——走迷宫的最小步数
查看>>
并查集——好朋友
查看>>
关键路径
查看>>
DP——数塔问题
查看>>
C/C++学习笔记——C提高:指针强化
查看>>
C/C++学习笔记——C提高:指针的指针(二级指针)
查看>>