HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180
题目大意:迷宫中有一堆楼梯,楼梯横竖变化。这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达前进方向的下一个点(跳过楼梯)。
同时可以在原地等待,问到达终点的最少时间。
解题思路:
很有趣的一个题。
还是先BFS,对于一个楼梯,change函数负责计算出走楼梯能够到达的新的X和Y,再判一次是否越界或不可达。
注意,在'.'点是可以原地等待的,需要额外Push这个点,这也是这题不会出现走不出迷宫的数据的原因,否则因为出不去迷宫,会不停在一个点BFS。
然后,程序就爆了。
#include "cstdio"
#include "string"
#include "cstring"
#include "iostream"
#include "queue"
using namespace std;
char map[][];
int n,m,sx,sy,ex,ey,dir[][]={-,,,,,-,,},vis[][];
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
};
status change(status s,int dir,char c,int dep)
{
if(dep%) c=(c=='|'?'-':'|');
if(c=='|')
{
if(dir==) return status(s.x-,s.y,);
if(dir==) return status(s.x+,s.y,);
if(dir==||dir==) return status(-,-,);
}
if(c=='-')
{
if(dir==||dir==) return status(-,-,);
if(dir==) return status(s.x,s.y-,);
if(dir==) return status(s.x,s.y+,);
}
}
int bfs(int x,int y)
{
queue<status> Q;
Q.push(status(x,y,));
vis[x][y]=true;
while(!Q.empty())
{
status t=Q.front();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(X<||X>n||Y<||Y>m||vis[X][Y]||map[X][Y]=='*') continue;
if(map[X][Y]=='|'||map[X][Y]=='-')
{
status nw=change(status(X,Y,),s,map[X][Y],t.dep);
X=nw.x;Y=nw.y;
}
if(X<||X>n||Y<||Y>m||vis[X][Y]||map[X][Y]=='*') continue;
vis[X][Y]=true;
if(X==ex&&Y==ey) return t.dep+;
Q.push(status(X,Y,t.dep+));
}
if(map[t.x][t.y]!='|'||map[t.x][t.y]!='-') Q.push(status(t.x,t.y,t.dep+));
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='S') {sx=i;sy=j+;}
if(tt[j]=='T') {ex=i;ey=j+;}
}
}
int ans=bfs(sx,sy);
cout<<ans<<endl;
}
}
11891440 | 2014-10-17 01:13:57 | Accepted | 1180 | 15MS | 308K | 2067 B | C++ | Physcal |
HDU 1180 (BFS搜索)的更多相关文章
- M - 诡异的楼梯 HDU - 1180(BFS + 在某个点等待一下 / 重复走该点)
M - 诡异的楼梯 HDU - 1180 Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU 4499.Cannon 搜索
Cannon Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
随机推荐
- linux下复制一个文件的内容到另一个文件
cat path/to/file/filename1 >> path/to/file/filename2 例如: cat id_rsa.pub >> ~/.ssh/author ...
- Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- CentOS6.5以runlevel 3开机时自动连接某无线设置示例
[参考]http://blog.csdn.net/simeone18/article/details/8580592 [方法一] 假设无线网卡代号为wlan0,无线AP的essid为:TheWiFi, ...
- Object-c 控制语句
控制语句: 分支语句 if-else 有控制机制 switch 循环语句 while do-while for 跳转语句 break,continue,goto
- 18.用两个栈实现队列[2StacksToImplementQueue]
[题目] 某队列的声明如下: C++ Code 123456789101112131415 template<typename T> class CQueue { public: ...
- linux自定义脚本添加到rc.local脚本无法正常运行的问题
为了能科学地上网,你懂的.其中需要将服务端做成开机启动.然而脚本在secure crt下能正常运行,添加到/etc/rc.local下却无法正常启动服务.用ps查找了下,脚本是运行了,但服务没起来.于 ...
- 【转】kettle 的内存设置及输出日志的时间类型
本文转载自:http://blog.csdn.net/dqswuyundong/archive/2010/10/19/5952004.aspx 设置kettle的内存 REM ************ ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Linux下RPM、tar.gz、DEB格式软件包的区别
初接解Linux的朋友一定对软件的安装特别头疼,同样都是for Linux,但RPM.tar.gz.DEB包还是有很大区别的,这种区别很有可能使你的安装过程进行不下去.那我们应该下载什么格式的包呢 ...
- 一个iOS图片选择器的DEMO(实现图片添加,宫格排列,图片长按删除,以及图片替换等功能)
在开发中,经常用到选择多张图片进行上传或作其他处理等等,以下DEMO满足了此功能中的大部分功能,可直接使用到项目中. 主要功能如下: 1,图片九宫格排列(可自动设置) 2,图片长按抖动(仿苹果软件删除 ...