FZU-Problem 2150 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.
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 题意:题目的意思是,两个人在玩游戏之前,需要把草堆全部清除掉。他们两个可以选择同一个草堆点燃,也可以选择不同的(但只能点燃一次,此外火势是可以蔓延的时间为1分钟)。 问是否可以清除(燃烧)所有的草堆,若能的话输出做小的燃烧时间,不能就请输出-1。 思路:首先草堆的数量小于等于2,肯定是能烧完的,此时时间输出为0 其他情况:开始的两个着火点可以相同或者不同,所以我们对所有的草堆进行两两组合,以这两个草堆为着火点同时进行广搜 如果燃烧的草堆数量等于草堆数量,则返回时间;否则返回-1 注意:代码实现有很多注意事项,详见code 代码:
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;
class Node{
int x;
int y;
int step;
public Node(int x,int y,int step){
this.x=x;
this.y=y;
this.step=step;
}
}
public class Main{
static int n,m,c,cnt;
static final int N=15;
static final int INF=2147483647;
static char map[][]=new char[N][N];
static boolean vis[][]=new boolean[N][N];
static ArrayDeque<Node> q=new ArrayDeque<Node>();
static Node node[]=new Node[N*N];
static int dx[]={0,0,1,-1};
static int dy[]={1,-1,0,0};
static int bfs(Node t1,Node t2){
//每次广搜每次初始化、清空
for(int i=0;i<N;i++) Arrays.fill(vis[i], false);
while(!q.isEmpty()) q.poll();
q.offer(new Node(t1.x,t1.y,0));
q.offer(new Node(t2.x,t2.y,0));
vis[t1.x][t1.y]=true;
vis[t2.x][t2.y]=true;
//必须判断是否为同1个点
if(t1.x==t2.x && t1.y==t2.y) cnt=1;
else cnt=2;
// System.out.println("t1.x="+t1.x+" t1.y="+t1.y+" cnt="+cnt);
while(!q.isEmpty()){
Node t=q.poll();
for(int i=0;i<4;i++){
int xx=t.x+dx[i];
int yy=t.y+dy[i];
if(xx<0||yy<0||xx>=n||yy>=m||vis[xx][yy]||map[xx][yy]!='#') continue;
vis[xx][yy]=true;
cnt++;
if(cnt==c) return t.step+1;
q.offer(new Node(xx,yy,t.step+1));
}
}
return -1;
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int T=scan.nextInt();
for(int k=1;k<=T;k++){
n=scan.nextInt();
m=scan.nextInt();
for(int i=0;i<n;i++) map[i]=scan.next().toCharArray();
c=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(map[i][j]=='#'){
node[c++]=new Node(i,j,0);
}
if(c<=2) {
System.out.println("Case "+k+": 0");
continue;
}
int ans=INF;
for(int i=0;i<c;i++)
for(int j=i;j<c;j++){
int res=bfs(node[i],node[j]);//这里错搞了2次bfs
if(res!=-1) ans=Math.min(ans, res);
}
if(ans==INF) System.out.println("Case "+k+": -1");
else System.out.println("Case "+k+": "+ans);
}
}
}
FZU-Problem 2150 Fire Game(两点bfs)的更多相关文章
- FZU Problem 2150 Fire Game(bfs)
这个题真要好好说一下了,比赛的时候怎么过都过不了,压点总是出错(vis应该初始化为inf,但是我初始化成了-1....),wa了n次,后来想到完全可以避免这个问题,只要入队列的时候判断一下就行了. 由 ...
- FZU Problem 2150 Fire Game
Problem 2150 Fire Game Accept: 145 Submit: 542 Time Limit: 1000 mSec Memory Limit : 32768 KB P ...
- FZU 2150 fire game (bfs)
Problem 2150 Fire Game Accept: 2133 Submit: 7494Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- FZOJ Problem 2150 Fire Game
...
- FZU 2150 Fire Game --两点同步搜索
枚举两点,然后同步BFS,看代码吧,很容易懂的. 代码: #include <iostream> #include <cstdio> #include <cstring& ...
- FZU 2150 Fire Game(BFS)
点我看题目 题意 :就是有两个熊孩子要把一个正方形上的草都给烧掉,他俩同时放火烧,烧第一块的时候是不花时间的,每一块着火的都可以在下一秒烧向上下左右四块#代表草地,.代表着不能烧的.问你最少花多少时间 ...
- foj 2150 Fire Game(bfs暴力)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M ...
- FZU 2150 Fire Game 【两点BFS】
Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows, M columns) ...
- fzu 2150 Fire Game 【身手BFS】
称号:fzupid=2150"> 2150 Fire Game :给出一个m*n的图,'#'表示草坪,' . '表示空地,然后能够选择在随意的两个草坪格子点火.火每 1 s会向周围四个 ...
- FZU 2150 Fire Game(点火游戏)
FZU 2150 Fire Game(点火游戏) Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Description - 题目描述 ...
随机推荐
- 在windows系统安装nginx
1.下载Nginx,链接:http://nginx.org/en/download.html 2.解压放到自己的磁盘,双击击运行nginx.exe,会有命令框一闪而过,在浏览器上面输入localhos ...
- 我国自主研发的先进辅助驾驶系统(ADAS)控制器产品实现量产配套
来源: http://www.most.gov.cn/kjbgz/201710/t20171023_135606.htm 感谢对我们ADAS团队的肯定!
- JavaScript——event事件详解
1.事件对象 Event 对象代表事件的状态,比如事件在其中发生的元素.键盘按键的状态.鼠标的位置.鼠标按钮的状态. 什么时候会产生Event 对象呢? 例如: 当用户单击某个元素的时候,我们给这个元 ...
- 【STM32H7教程】第62章 STM32H7的MDMA,DMA2D和通用DMA性能比较
完整教程下载地址:http://www.armbbs.cn/forum.php?mod=viewthread&tid=86980 第62章 STM32H7的MDMA,DMA2D和通 ...
- Android布局管理器-使用LinearLayout实现简单的登录窗口布局
场景 Android布局管理器-从实例入手学习相对布局管理器的使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1038389 ...
- 泡泡后台Couchbase缓存使用经验分享
一.导读 爱奇艺的社交业务“泡泡”,拥有日活用户6千万+,后台系统每日高峰期间接口QPS可以达到80K+,与视频业务的主要区别是泡泡业务更多地引入了与用户互动相关的数据,读.写的量均很大.无论是庞大的 ...
- ssh远程连接到Ubuntu
1.ubuntu首先得安装ssh sudo apt-get install openssh-server 2.启动ssh sudo /etc/init.d/ssh start 3.检查是否开启 ps ...
- 在本地搭建git服务器
GitHub就是一个免费托管开源代码的远程仓库.但是对于某些视源代码如生命的商业公司来说,既不想公开源代码,又舍不得给GitHub交保护费,那就只能自己搭建一台Git服务器作为私有仓库使用. 搭建Gi ...
- 9.16java总结
枚举 EnunTest.java 运行结果 falsefalsetrueSMALLMEDIUMLARGE 枚举类型可以直接用==来判断是否相等,即代表数据串,又有数的属性.是引用类型. 浮点数计算 ...
- 堆优化 dijkstra 简介
dijkstra 前言 原本我真的不会什么 dijkstra 只用那已死的 spfa ,还有各种玄学优化,可是,我不能相信一个已死的算法,就像我不能相信自己. ps : 虽然他已经活了 序 我站在镜子 ...