HDU_1180_诡异的楼梯_BFS
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1180
诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 12255 Accepted Submission(s): 3036
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.
第一行有两个数,M和N,接下来是一个M行N列的地图,'*'表示障碍物,'.'表示走廊,'|'或者'-'表示一个楼梯,并且标明了它在一开始时所处的位置:'|'表示的楼梯在最开始是竖直方向,'-'表示的楼梯在一开始是水平方向.地图中还有一个'S'是起点,'T'是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.
**..T
**.*.
..|..
.*.*.
S....
Hint
地图如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int dir[][]= {{-,},{,},{,},{,-}};
bool vis[][];
char map[][];
int m,n,sx,sy; struct node
{
short x,y;
short int time;
}; bool inside(int x,int y)
{
if(x>=&&x<m&&y>=&&y<n)
return ;
return ;
} void BFS()
{
queue<node>q;
node sta;
sta.x=sx;
sta.y=sy;
sta.time=;
vis[sx][sy]=;
q.push(sta);
while(!q.empty())
{
node now=q.front();
q.pop();
if(map[now.x][now.y]=='T')
{
printf("%d\n",now.time);
return;
}
for(int i=; i<; i++)
{
node next;
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
next.time=now.time+;
if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!='*')
{
if(map[next.x][next.y]=='|')
{
next.x+=dir[i][];
next.y+=dir[i][];
if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!='*')
{if((now.time%==&&(i==||i==))||(now.time%==&&(i==||i==)))
next.time+=;}
else
continue;
}
else if(map[next.x][next.y]=='-')
{
next.x+=dir[i][];
next.y+=dir[i][];
if(inside(next.x,next.y)&&!vis[next.x][next.y]&&map[next.x][next.y]!='*')
{if((now.time%==&&(i==||i==))||(now.time%==&&(i==||i==)))
next.time+=;}
else
continue;
}
vis[next.x][next.y]=;
q.push(next);
}
}
}
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(vis,,sizeof(vis));
for(int i=; i<m; i++)
scanf("%s",map[i]);
for(int i=; i<m; i++)
for(int j=; j<n; j++)
{
if(map[i][j]=='S')
{
sx=i;
sy=j;
}
}
BFS();
}
return ;
}
HDU_1180_诡异的楼梯_BFS的更多相关文章
- hdu 1180 诡异的楼梯
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu 1180 诡异的楼梯 (bfs)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Sub ...
- HDU 1180 诡异的楼梯(BFS)
诡异的楼梯 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1180诡异的楼梯(bfs)
诡异的楼梯 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submis ...
- 诡异的楼梯(bfs)hdu1180
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...
- hdu 1180:诡异的楼梯(BFS广搜)
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- HDU 1180 诡异的楼梯(超级经典的bfs之一,需多回顾)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1180 诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1180 诡异的楼梯【BFS/楼梯随时间变化】
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submis ...
- M - 诡异的楼梯 HDU - 1180(BFS + 在某个点等待一下 / 重复走该点)
M - 诡异的楼梯 HDU - 1180 Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯 ...
随机推荐
- Android自己主动提示文本框(AutoCompleteTextView)
自己主动提示文本框(AutoCompleteTextView)能够加强用户体验,缩短用户的输入时间(百度的搜索框就是这个效果). 首先.在xml中定义AutoCompleteTextView控件: a ...
- android PercentRelativeLayout 支持百分比来设置控件的宽高
Android 最终官方支持按百分比来设置控件的宽高了. 我们先来看看效果: 看一下布局: PercentRelativeLayout <android.support.percen ...
- 推荐IOS开发3个工具:Homebrew、TestFight、Crashlytics-b
1. Homebrew 什么是Homebrew? Homebrew is the easiest and most flexible way to install the UNIX tools App ...
- MySQL-数据表锁定
MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...
- SDK Manager配置
改Host的都是扯淡,现在不好使了.. 还是使用东软的国内镜像好使,打开SDK Manager Tools - Options Http proxy Server: mirrors.neusoft. ...
- STL 之 list源码自行实现(iterator)
(0)文件夹 STL 之 vector源码实现(云算法<< [] = 重载, new delete,throw catch) STLc++中string类的源码 堆(stack) 之 c ...
- SRM598 Div1
这次直接进到div1里面搞了,不过div1果然难度要高一些 第一题直接贪心算法了... 先排序,然后判断是否三个小于100,然后2个,最后一个 第二题看错了输入数据,理解错了题意,失误 第三题比较难办 ...
- What does jQuery.fn mean?
n jQuery, the fn property is just an alias to the prototype property. The jQuery identifier (or $) i ...
- USACO Section 1.1PROB Your Ride Is Here
题目传送门 不能提交哦 http://www.nocow.cn/index.php/Translate:USACO/ride /* ID: jusonal1 PROG: ride LANG: C+ ...
- jquery autocomplete自动补全
简单用法: $(function(){ var data = "the People's Republic of China".split(" "); $(&q ...