水灾 1000MS 64MB (广搜)
水灾(sliker.cpp/c/pas) 1000MS 64MB
大雨应经下了几天雨,却还是没有停的样子。土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没。
CCY所在的城市可以用一个N*M(N,M<=50)的地图表示,地图上有五种符号:“. * X D S”。其中“X”表示石头,水和人都不能从上面经过。“.”表示平原,CCY和洪水都可以经过。“*”表示洪水开始地方(可能有多个地方开始发生洪水)。“D”表示CCY的别墅。“S”表示CCY现在的位置。
CCY每分钟可以向相邻位置移动,而洪水将会在CCY移动之后把相邻的没有的土地淹没(从已淹没的土地)。
求CCY回到别墅的最少时间。如果聪哥回不了家,就很可能会被淹死,那么他就要膜拜黄金大神涨RP来呼叫直升飞机,所以输出“ORZ hzwer!!!”。
输入文件 sliker.in
输出文件 sliker.out
Input
3 3
D.*
…
.S.
Output
3
Input
3 3
D.*
…
..S
Output
ORZ hzwer!!!
Input
3 6
D…*.
.X.X..
….S.
Output
6
分析:找出洪水开始的所有节点,写两个广搜,另一个搜索洪水走的路线bfs_1,一个搜索人走的路线 bfs_2。
在bfs_2中,人每走一步之前,先bfs_1搜索洪水下一秒将要淹没点,然后搜索人走的路线。
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream> using namespace std;
const int N = ; struct node{
int x,y,step;
}cur,nxt,t,u; queue <node> q;
queue <node> q2;
int mp[N][N];
bool v[N][N],v2[N][N];
int dx[] = {,,,-};
int dy[] = {,,-,};
int n,m,ex,ey,sx,sy,now;
char ch; void bfs_1(int w) //搜出w+1秒的地图
{
while(!q.empty())
{
cur = q.front();
if(cur.step!=w) return ;
q.pop();
for(int i=;i<;++i)
{
int a = dx[i]+cur.x;
int b = dy[i]+cur.y;
if(a> && b> && a<=n && b<=m && mp[a][b]== && !v[a][b])
{
if(a==ex && b==ey) continue;
mp[a][b] = ;
v[a][b] = true;
nxt.x = a; nxt.y = b; nxt.step = cur.step+;
q.push(nxt);
}
}
}
}
void bfs_2()
{
now = ;
bfs_1(now);
t.x = sx; t.y = sy; t.step = ;
q2.push(t);
v2[sx][sy] = true;
while(!q2.empty())
{
t = q2.front();
if(t.step!=now)
{now++; bfs_1(now); continue;}
q2.pop();
for(int i=;i<;++i)
{
int a = dx[i]+t.x;
int b = dy[i]+t.y;
if(a> && b> && a<=n && b<=m && mp[a][b]== && !v2[a][b])
{
if(a==ex && b==ey)
{
printf("%d\n",t.step+);return ;
}
v2[a][b] = true;
u.x = a; u.y = b; u.step = t.step+;
q2.push(u);
}
}
}
printf("ORZ hzwer!!!\n");
}
int main()
{
freopen("sliker.in","r",stdin);
freopen("sliker.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i)
for(int j=;j<=m;++j)
{
cin>>ch;
if(ch=='S') sx=i,sy=j,mp[i][j]=;
else if(ch=='D') ex=i,ey=j,mp[i][j]=;
else if(ch=='*')
{
mp[i][j] = ;
v[i][j] = true;
cur.x = i; cur.y = j; cur.step = ;
q.push(cur);
}
else if(ch=='.') mp[i][j] = ;
else if(ch=='X') mp[i][j] = ;
}
bfs_2();
return ;
}
水灾 1000MS 64MB (广搜)的更多相关文章
- T4870 水灾(sliker.cpp/c/pas) 1000MS 64MB
题目描述 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地赚完1e元回来,知道不久除了自己别墅,其他的地方都将会被洪水淹没. CCY所在的城市可以用一个N*M(N,M<=50)的地图表 ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- 双向广搜 POJ 3126 Prime Path
POJ 3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16204 Accepted ...
- 广搜+打表 POJ 1426 Find The Multiple
POJ 1426 Find The Multiple Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25734 Ac ...
- hdu 1242:Rescue(BFS广搜 + 优先队列)
Rescue Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- poj 3026 Borg Maze 最小生成树 + 广搜
点击打开链接 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7097 Accepted: 2389 ...
- (广搜)Fire Game -- FZU -- 2150
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82828#problem/I Fire Game Time Limit:1000MS ...
- Eight_pku_1077(广搜).java
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21718 Accepted: 9611 Special ...
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
随机推荐
- [译] 怎样(以及为什么要)保持你的 Git 提交记录的整洁
最近在掘金翻译了一篇文章,主要讲的是 Git 提交记录的维护,确实很有用,感兴趣的同学可以去看一下.链接如下: [译] 怎样(以及为什么要)保持你的 Git 提交记录的整洁 截图:
- iOS学习笔记09-核心动画CoreAnimation
http://www.cnblogs.com/liutingIOS/p/5368536.html 一.CALayer CALayer包含在QuartzCore框架中,具有跨平台性,在iOS中使用Cor ...
- git 比较不同版本文件的差异
Git 比较不同版本文件差异的常用命令格式: git diff 查看尚未暂存的文件更新了哪些部分 git diff filename 查看尚未暂存的某个文件更新了哪些 git diff –cached ...
- ASP.NET Web API编程——使用自签名SSL证书
1自签名SSL证书的创建 创建自签名SSL工具xca为:https://sourceforge.net/projects/xca/ 创建过程 1)创建根证书 打开软件,界面如下. 点击,看到下拉菜单, ...
- 什么是Apache Isis
这个页面展示了一个现代的 Apache Isis 应用程序的外观. 下边是Isis 插件里的 todoapp 示例 (非 ASF)截图,你可以随意使用. 界面里对应的领域类可以在这里找到. 这个 to ...
- CSU-ACM2018暑假集训6—BFS
可以吃饭啦!!! A:连通块 ZOJ 1709 Oil Deposits(dfs,连通块个数) B:素数变换 打表+bfs POJ 3216 Prime Path(打表+bfs) C:水bfs HDU ...
- HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/ ...
- C#通过拼接协议的方式来发送邮件类库
using System; using System.Collections.Generic; using System.Net; using System.Net.Mail; using Syste ...
- 数据库——MySQL——事务
数据的事务是指作为单个逻辑工作单元执行的一系列操作,要么完全执行,要么完全不执行. 事务必须具备四个特性: 原子性 原子性是指事务包含的所有操作要么全部成功,要么全部失败回滚 一致性 在事务T开始时, ...
- 类似QQ的聊天工程
首先建立一个html:<!DOCTYPE html><html lang="en"><head> <meta charset=" ...