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

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long LL;
char graph[][];
bool vis[][];
struct Node
{
int x,y,step;
};
Node s,t;
int dir[][] = {{-,},{,},{,-},{,}};
int n,m;
bool check(int x,int y)
{
if(x<||x>=n||y<||y>=m||vis[x][y]||graph[x][y]=='*')
return false;
return true;
}
int bfs()
{
memset(vis,false,sizeof(vis));
queue<Node> q;
vis[s.x][s.y]=true;
q.push(s);
while(!q.empty())
{
Node node = q.front();
q.pop();
if(node.x==t.x&&node.y==t.y)
{
return node.step;
}
Node next;
for(int i=; i<; i++)
{
next.x = node.x+dir[i][];
next.y = node.y+dir[i][];
next.step=node.step+;
if(!check(next.x,next.y)) continue;
if(graph[next.x][next.y]=='.')
{
vis[next.x][next.y]=true;
q.push(next);
}
if(graph[next.x][next.y]=='|')
{
if((next.step%==)&&(i==||i==)) ///now this place change to '-'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}
else if((next.step%==)&&(i==||i==)) ///stay '|'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}else{ ///隐含条件,等下一分钟
next.x= node.x;
next.y = node.y;
q.push(next);
}
}
if(graph[next.x][next.y]=='-')
{
if((next.step%==)&&(i==||i==)) ///now this place change to '|'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}
else if((next.step%==)&&(i==||i==)) ///stay '-'
{
next.x+=dir[i][];
next.y+=dir[i][];
if(check(next.x,next.y)){
vis[next.x][next.y]=true;
q.push(next);
}
}else{ ///等下一分钟
next.x= node.x;
next.y = node.y;
q.push(next);
}
}
}
}
return -;
}
int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=; i<n; i++)
{
scanf("%s",graph[i]);
for(int j=; j<m; j++)
{
if(graph[i][j]=='S')
{
s.x = i,s.y = j,s.step=;
graph[i][j]='.';
}
else if(graph[i][j]=='T')
{
t.x = i,t.y = j;
graph[i][j]='.';
}
}
}
int res = bfs();
printf("%d\n",res);
}
return ;
}
hdu 1180(广搜好题)的更多相关文章
- POJ3984 BFS广搜--入门题
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20816 Accepted: 12193 Descriptio ...
- hdu 1072 广搜(逃离爆炸迷宫)
题意: 在n×m的地图上,0表示墙,1表示空地,2表示人,3表示目的地,4表示有定时炸弹重启器.定时炸弹的时间是6,人走一步所需要的时间是1.每次可以上.下.左.右移动一格.当人走到4时如果炸弹的时间 ...
- HDU 2717 宽搜第一题、
题意:求n到k的最小路径, n有三种变法 n+1,n-1或者2*n: 贴个广搜的模版在这里把.... 总结一下:一般涉及到求最短路的话用宽搜 #include<iostream> #in ...
- hdu 1175(广搜)
题意:容易理解... 思路:我开始的思路不好实现,而且有漏洞,时间复杂度也高,后来在网上学了下别人的方法,真心感觉很牛B,不仅代码好实现,而且时间复杂度比较低,具体看代码实现吧!! 代码实现: #in ...
- poj 1184 广搜进阶题
起初的想法果然就是一个6000000的状态的表示. 但是后面觉得还是太过于幼稚了. 可以看看网上的解释,其实就是先转换位置,然后再改变数字的大小. #include<iostream> # ...
- hdu 1072 广搜
路径是可以重复走的,但是如果再一次走过时间重置点是没有意义的 #include <iostream> #include <cstdio> #include <cstrin ...
- hdu 2612:Find a way(经典BFS广搜题)
Find a way Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- poj 3984:迷宫问题(广搜,入门题)
迷宫问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7635 Accepted: 4474 Description ...
- hdu 1253:胜利大逃亡(基础广搜BFS)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
随机推荐
- html页面简单访问限制
PS:突然发现博客园有密码保护功能,已经可以满足基本需求了.博客园还能备份自己的所有数据,做到了数据归用户所有,平台只是展示,真是良心网站,大赞. 想要通过一个站点放一些东西给一些人看,但是又不想让所 ...
- mysql 的 case when 用法
正确的格式: case when condition then result when condition then result when condition then result else re ...
- destoon后台权限-不给客户创始人权限并屏蔽部分功能
1.根目录下后台入口文件admin.php $_founder = $CFG['founderid'] == $_userid ? $_userid : 0; // $CFG['founderid ...
- virtualbox安装win7系统报错(“FATAL:No bootable medium found!”)
virtualbox属于傻瓜式安装虚拟系统,但博主安装win7系统时,无论怎么调试都还是出现截图所述样式,网上教程很多,但是都不行,其实只有一个根本原因安装的iso镜像不是原生镜像,下载的镜像已经是被 ...
- 12.Yii2.0框架视图模版继承与模版相互调用
目录 模板渲染的两种方式 加载视图 index.php 和 about.php 页面 建立控制器HomeController php 新建模板 home\index.php 新建模板home\abou ...
- golang http 中间件
golang http 中间件 源码链接 golang的http中间件的实现 首先实现一个http的handler接口 type Handler interface { ServeHTTP(Respo ...
- 小谈python里 列表 的几种常用用法
在python中列表的常用方法主要包括增加,删除,查看和修改.下面以举例子的方法具体说明,首先我们创建两个列表,列表是用[ ]表示的,里面的元素用逗号隔开. a=[‘hello’,78,15.6,‘你 ...
- request response cookie session
request 1. url传递参数 1)参数没有命名, 如: users/views def weather(request, city, year): print(city) print(year ...
- linux 基础 软件的安装 *****
一软件的安装 原代码与tarball 源代码---->编译------>可执行文件 查看文件类型 file命令 是否是二进制文件 注意如果文件的可执行权限 .c结尾的源文件- ...
- acm之图论基础
1.图的定义 图 是一个顶点集合V和一个顶点间关系的集合E组成,记G=(V,E) V:顶点的有限非空集合. E:顶点间关系的有限集合(边集). 存在一个结点v,可能含有多个前驱节点和后继结点. 1顶点 ...