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 ...
随机推荐
- 【SAM】bzoj5084: hashit
做得心 力 憔 悴 Description 你有一个字符串S,一开始为空串,要求支持两种操作 在S后面加入字母C 删除S最后一个字母 问每次操作后S有多少个两两不同的连续子串 Input 一行一个字符 ...
- mysql 备份 常用脚本
全备: innobackupex --defaults-file=/data/mysql3316/my3316.cnf --user=root --password=mysqlpass /data/b ...
- Linux - 后台运行 ctrl + z , jobs , bg , fg
一.& 最经常被用到 这个用在一个命令的最后,可以把这个命令放到后台执行 二.ctrl + z 可以将一个正在前台执行的命令放到后台,并且暂停三.jobs查看当前有多少在后台运行的命令四.fg ...
- LNMP一键安装包开启pathinfo和rewrite模式
此教程适用于集成安装包lnmp,官网是https://lnmp.org/ 一. 开启pathinfo #注释 下面这一行 #include enable-php.conf #载入新的配置文件 incl ...
- 【js】【vue】获取当前dom层
多层嵌套,$event.currentTarget 指当前点击层
- Goroutines和Channels
原文链接 https://golangbot.com/goroutines/ Goroutines Goroutines 可以被认为是多个函数或方法同时允许.可以认为是一个轻量级的线程.与线程的花费相 ...
- python爬虫基础15-python图像处理,PIL库
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- 素数筛选:HDU2710-Max Factor
Max Factor Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
- Linux网络文件系统NFS详解
什么是文件系统,NFS文件系统又是什么? 简单的说,文件系统就是通过软件对磁盘上的数据进行组织和管理的一种机制,对其的一种封装或透视. 你女朋友拍了美美的暧昧照片,放一个文件夹里发送给了A服务器,当你 ...
- 开源OA系统启动:基础数据,工作流设计
原文:http://www.cnblogs.com/kwklover/archive/2007/01/13/bpoweroa_03_baseandworkflowdesign.html自从开源OA系统 ...