Fire Game 双向bfs
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.
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 题意,从两个地方点火,看看烧完最短的时间是多少,火不能烧'.',只能烧'#',也就是遇到了句点,火焰停止蔓延;
一开始想看看有没有啥规律,想判断如果连通分支超过两个就不行,然后分开讨论一个连通分支与两个连通分支的情况,可是规律不通用,于是选择了排着遍历,mint给一个很大的值,每次选两个地方进行点火,然后都入队,bfs,如果能把草全烧完,更新最小时间,最后如果最小时间还是原来很大的值,输出-1.
但是忘记了还有只有一块草的情况,那就另外加了一下就过了。 代码:
#include <iostream>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std; int dir[][]={,,,,,-,-,};
int T,n,m,x,y,t,vis[][],c;
char mp[][];
class que
{
public:
int x,y,time;
}temp;
int xx[],yy[],xy,mint;
int main()
{
cin>>T;
for(int l=;l<=T;l++)
{
mint=;//updated
cin>>n>>m;
queue<que>q;
xy=;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='#')
xx[xy]=i,yy[xy]=j,xy++;
}
for(int i=;i<xy;i++)
{
for(int j=i+;j<xy;j++)
{
memset(vis,,sizeof(vis));//initialized
temp.x=xx[i],temp.y=yy[i],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
temp.x=xx[j],temp.y=yy[j],temp.time=;
q.push(temp);
vis[temp.x][temp.y]=;
c=;//updated
t=;//updated
while(!q.empty())
{
if(t<q.front().time)t=q.front().time;
for(int k=;k<;k++)
{
x=q.front().x+dir[k][];
y=q.front().y+dir[k][];
if(x<||y<||x>=n||y>=m||mp[x][y]=='.'||vis[x][y])continue;
temp.x=x,temp.y=y,temp.time=q.front().time+;
q.push(temp);
vis[x][y]=;
c++;
}
q.pop();
}
if(c==xy){if(t<mint)mint=t;}//judged
} }
printf("Case %d: ",l);
if(mint<)cout<<mint<<endl;
else if(xy==)cout<<<<endl;//special
else cout<<-<<endl; }
}
Fire Game 双向bfs的更多相关文章
- UVA - 11624 Fire! 双向BFS追击问题
Fire! Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of ...
- CSUOJ2031-Barareh on Fire(双向BFS)
Barareh on Fire Submit Page Description The Barareh village is on fire due to the attack of the virt ...
- ACM: FZU 2150 Fire Game - DFS+BFS+枝剪 或者 纯BFS+枝剪
FZU 2150 Fire Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- POJ1915Knight Moves(单向BFS + 双向BFS)
题目链接 单向bfs就是水题 #include <iostream> #include <cstring> #include <cstdio> #include & ...
- HDU 3085 Nightmare II 双向bfs 难度:2
http://acm.hdu.edu.cn/showproblem.php?pid=3085 出的很好的双向bfs,卡时间,普通的bfs会超时 题意方面: 1. 可停留 2. ghost无视墙壁 3. ...
- POJ 3170 Knights of Ni (暴力,双向BFS)
题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...
- [转] 搜索之双向BFS
转自:http://www.cppblog.com/Yuan/archive/2011/02/23/140553.aspx 如果目标也已知的话,用双向BFS能很大程度上提高速度. 单向时,是 b^le ...
- 双向BFS
转自“Yuan” 如果目标也已知的话,用双向BFS能很大提高速度 单向时,是 b^len的扩展. 双向的话,2*b^(len/2) 快了很多,特别是分支因子b较大时 至于实现上,网上有些做法是用两个 ...
- HDU 3085 Nightmare Ⅱ (双向BFS)
Nightmare Ⅱ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- Codeforces 805D - Minimum number of steps
805D - Minimum number of steps 思路:简单模拟,a每穿过后面一个b,b的个数+1,当这个a穿到最后,相当于把它后面的b的个数翻倍.每个a到达最后的步数相当于这个a与它后面 ...
- Python爬虫Urllib库的基本使用
Python爬虫Urllib库的基本使用 深入理解urllib.urllib2及requests 请访问: http://www.mamicode.com/info-detail-1224080.h ...
- img srcset 和 sizes
img srcset 和 sizes 诞生的目的是解决图片清晰度和节省加载图片大小问题,比方说我需要在retina高的硬件上看到更细腻的图片,又或者我要在电脑看到的图片和在手机上的图片不一样. 解 ...
- python-day39--数据库
1.什么是数据:描述事物的特征,提取对自己有用的信息 称之为数据 2..什么是数据库: 数据库即存放数据的仓库,只不过这个仓库是在计算机存储设备上,而且数据是按一定的格式存放的 为什么要用数据库: ...
- python-day21--序列化模块模块
什么叫序列化——将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化 序列化的目的: 1.以某种存储形式使自定义对象持久化: 2.将对象从一个地方传递到另一个地方. 3.使程序更具维护性. ...
- UVA-10497 Sweet Child Makes Trouble (计数+高精度)
题目大意:这是一道简单排列组合题 .简单说下题意:n件物品,把这n件物品放到不是原来的位置,问所有的方案数.所有的位置都没有变. 题目解析:按照高中的方法,很快得到一个递推公式:f [n]= (n-1 ...
- HttpServletResponse输出的中文乱码
HttpServletResponse输出有两种格式,一种是字符流,一种是字节流. 1.字符流 // 这句话的意思,是让浏览器用utf8来解析返回的数据,即设置客户端解析的编码 response.se ...
- iOS UI-UIPickerView(拾取器)、UIWebView(网页视图)和传值方式
// // ViewController.m // IOS_0107_finalToolClass // // Created by ma c on 16/1/7. // Copyright (c) ...
- keras-anomaly-detection 代码分析——本质上就是SAE、LSTM时间序列预测
keras-anomaly-detection Anomaly detection implemented in Keras The source codes of the recurrent, co ...
- HDU 1710 二叉树遍历
首先.先序遍历是先访问根节点.然后左节点 然后右节点.从根节点开始 直到它的子节点没有左节点才开始回溯访问上一个节点的右节点.同理.中序遍历 先访问左节点 然后是父节点 然后是右节点.从根节点开始 直 ...