FZU2150 Fire Game —— BFS
题目链接:https://vjudge.net/problem/FZU-2150
Accept: 2702 Submit: 9240
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.
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
Sample Output
题解:
1.直接枚举两个起火点,然后BFS。
2.计算次数:100(测试组数)*100(第一个起火点)*100(第二个起火点)*100(棋盘大小) = 1e8,不是会超时吗?可能数据比较弱吧。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 10+10; int n, m;
char M[MAXN][MAXN];
int dir[4][2] = {1,0,0,1,-1,0,0,-1}; struct node
{
int x, y, step;
}; int vis[MAXN][MAXN];
queue<node>que;
int bfs(int x, int y, int xx, int yy)
{
ms(vis, 0);
while(!que.empty()) que.pop(); node now, tmp;
now.x = x; now.y = y;
now.step = 0;
vis[now.x][now.y] = 1;
que.push(now); now.x = xx; now.y = yy;
now.step = 0;
vis[now.x][now.y] = 1;
que.push(now); int ret = 0;
while(!que.empty())
{
now = que.front();
que.pop(); for(int i = 0; i<4; i++)
{
tmp.x = now.x + dir[i][0];
tmp.y = now.y + dir[i][1];
if(tmp.x>=1 && tmp.x<=n && tmp.y>=1 && tmp.y<=m
&& M[tmp.x][tmp.y]=='#' && !vis[tmp.x][tmp.y])
{
vis[tmp.x][tmp.y] = 1;
tmp.step = now.step + 1;
ret = max(ret, tmp.step);
que.push(tmp);
}
}
}
for(int i = 1; i<=n; i++) //判断是否所有的草地都被焚了
for(int j = 1; j<=m; j++)
if(M[i][j]=='#' && !vis[i][j])
return -1;
return ret;
} int solve()
{
int ret = INF;
for(int i = 1; i<=n; i++) //枚举两个起火点
for(int j = 1; j<=m; j++)
if(M[i][j]=='#')
{
for(int ii = 1; ii<=n; ii++)
for(int jj = 1; jj<=m; jj++)
if(M[ii][jj]=='#')
{
int tmp = bfs(i,j,ii,jj);
if(tmp!=-1) ret = min(ret, tmp);
}
}
return (ret!=INF)?ret:-1;
} int main()
{
int T;
scanf("%d",&T);
for(int kase = 1; kase<=T; kase++)
{
scanf("%d%d",&n,&m);
for(int i = 1; i<=n; i++)
scanf("%s", M[i]+1);
printf("Case %d: %d\n", kase, solve() );
}
}
FZU2150 Fire Game —— BFS的更多相关文章
- FZU2150 Fire Game BFS搜索
题意:就是选两个点出发,只能走草坪,看能不能走完所有的草坪 分析:由于数据范围很小,所有枚举这两个点,事先将所有的草坪点存起来,然后任选两个点走,(两个点可以是同一个点) 然后BFS就行了 注:无解的 ...
- UVA 11624 - Fire! 图BFS
看题传送门 昨天晚上UVA上不去今天晚上才上得去,这是在维护么? 然后去看了JAVA,感觉还不错昂~ 晚上上去UVA后经常连接失败作死啊. 第一次做图的题~ 基本是照着抄的T T 不过搞懂了图的BFS ...
- FZU2150 :Fire Game (双起点BFS)
传送门:点我 题意:“#”是草,"."是墙,询问能不能点燃俩地方,即点燃俩“#”,把所有的草烧完,如果可以,那么输出最小需要的时间,如果不行输出-1 思路:暴力BFS,看到n和m都 ...
- UVa 11624 Fire!(BFS)
Fire! Time Limit: 5000MS Memory Limit: 262144KB 64bit IO Format: %lld & %llu Description Joe ...
- (简单) UVA 11624 Fire! ,BFS。
Description Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the ow ...
- [宽度优先搜索] FZU-2150 Fire Game
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)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2150 Problem Description Fat brother and Maze are playing ...
- FZU 2150 Fire Game (bfs+dfs)
Problem Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board ...
- UVALive 5066 Fire Drill BFS+背包
H - Fire Drill Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Sta ...
随机推荐
- Spring-IOC源码解读2.1-BeanDefinition的Resource定位
Spring通过ResourceLoader来处理得到的Resource.在前面我们知道容器初始化是以refresh()方法为入口的,内部的实现首先准备上下文,然后通过obtainFreshBeanF ...
- 洛谷 P1522 牛的旅行
题目描述 农民 John的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场.但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通.这样,Farmer John就有多个 ...
- Codeforces Round #267 (Div. 2) C. George and Job (dp)
wa哭了,,t哭了,,还是看了题解... 8170436 2014-10-11 06:41:51 njczy2010 C - George and Jo ...
- Python入门---易错已错易混淆----知识点
1.not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 结果会输出啥? 根据优先级:(not 1) or (0 and 1) or (3 a ...
- MongoDB增删改查操作详解(命令行)
一.插入 MongoDB的插入操作很简单,使用insert方法,这里演示从创建数据库.创建集合到插入文档.查询文档. 集合创建方法参数说明: size:集合最大空间 max:集合最多文档数量 (超出s ...
- UTF-8 编码的文件在处理时要注意 BOM 文件头问题
最近在给项目团队开发一个基于 Java 的通用的 XML 分析器时,设计了一个方法,能够读取现成的 XML 文件进行分析处理,当然 XML 都是采用 UTF-8 进行编码的.但是在用 UltraEdi ...
- HDU4850 构造一个长度为n的串,要求任意长度为4的子串不相同
n<=50W.(使用26个字母) 构造方法:26个,最多构造出26^4种不同的串,长度最长是26^4+3,大于是输出"impossble",用四维数组判重.每次向前构造一位( ...
- 生成PDF文档
byte[] buffer = context.Response.Clear(); context.Response.ClearHeaders(); context.Response.ClearCon ...
- web信息搜索之目标扫描篇
https://blog.csdn.net/dongfei2033/article/details/78175421
- MongoDB学习day10--Mongoose的populate实现关联查询
一.Mongoose populate官方文档 https://mongoosejs.com/docs/populate.html 二.Mongoose populate关联查询 1.定义ref va ...