水灾 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 ...
随机推荐
- sqlserver 2008 r2 直接下载地址,可用迅雷下载
转自 http://www.cnblogs.com/chinafine/archive/2010/12/23/1915312.html sqlserver 2008 r2 直接下载地址,可用迅雷下载 ...
- luogu P3801 红色的幻想乡
嘟嘟嘟 首先人人都能想到是线段树,不过二维线段树肯定会MLE+TLE的. 我们换一种想法,不去修改整个区间,而是修改一个点:开横竖两个线段树,分别记录哪些行和列被修改了.因为如果两阵红雾碰撞,则会因为 ...
- [USACO07DEC]Sightseeing Cows
嘟嘟嘟 这题好像属于01分数规划问题,叫什么最优比率生成环. 题目概括一下,就是求一个环,满足∑v[i] / ∑c[i]最大. 我们可以堆上面的式子变个型:令 x = ∑v[i] / ∑c[i],则x ...
- .net mvc 路由
Asp.net Mvc之Action如何传多个参数 在Global.asax文件中,默认路由如下. routes.MapRoute( "Default", // 路由名称 &quo ...
- es6之数组方法
//兼容插件 babel-polyfill values()等存在兼容问题,需要加载babel-polyfill插件 .keys() 获取数组的key值 .values() 获取数组的value值 ...
- Many-to-many relationships in EF Core 2.0 – Part 3: Hiding as ICollection
In the previous post we ended up with entities that hide the join entity from the public surface. Ho ...
- EF Core怎么只Update实体的部分列数据
下面是EF Core中的一个Person实体: public partial class Person { public int Id { get; set; } public string Code ...
- Django开发BUG汇总
使用版本知悉 limengjiedeMacBook-Pro:~ limengjie$ python --version Python :: Anaconda, Inc. limengjiedeMacB ...
- 一点一点看JDK源码(二)java.util.List
一点一点看JDK源码(二)java.util.List liuyuhang原创,未经允许进制转载 本文举例使用的是JDK8的API 目录:一点一点看JDK源码(〇) 1.综述 List译为表,一览表, ...
- you don't have permission to access ...........on this server问题解决
因为刚刚开始使用新架构的项目,于是把老项目统统替换成了新的项目.配置好后,本地登录页面报 you don't have permission to access ...... on this serv ...