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 ...
随机推荐
- chm文件打开空白无内容的解决办法
今天下载了个chm文件,但是打开空白,也没显示什么内容,经过一番研究之后终于可以正常显示了,下面把解决办法分享出来供大家参考下,谢谢. 工具/原料 windows7系统 chm文件 方法/步骤 ...
- 每天一个命令day1【diff 命令】(具体实例看下一节)
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- 【云计算】K8S DaemonSet 每个node上都运行一个pod
Kubernetes容器集群中的日志系统集成实践 Kubernetes是原生的容器编排管理系统,对于负载均衡.服务发现.高可用.滚动升级.自动伸缩等容器云平台的功能要求有原生支持.今天我分享一下我们在 ...
- Burst Balloons
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- ListBox1控件
前台: <div> <asp:ListBox ID="ListBox1" runat="server" AutoPostBack=" ...
- [转]C程序内存区域分配(5个段作用)
[转]C程序内存区域分配(5个段作用) 2012-08-10 14:45:32| 分类: C++基础|字号 订阅 参考:http://www.360doc.com/content/11/03 ...
- [Android Studio] 使用本地 aar 文件
导出aar 首先Android Library项目的gradle脚本只需要在开头声明 apply plugin: 'com.android.library' 之后就和导出apk文件一样的方法,执行 . ...
- [产品相关] A/B测试终极指南(翻译)
转载地址: http://blog.sina.com.cn/s/blog_9149268d0100zrx7.html 还记得以前导师说看了英文的文章就把它翻译一下吧,这样会对文章更好地理解,也会有更深 ...
- cocos2dx实现象棋之运动
1.头文件 void moveStone(int moveid, int killid, int x, int y); void moveComplete(CCNode*, void*); bool ...
- js 实现文字列表滚动效果
今天要实现抽奖名单在首页滚动展示的效果,就用js写了一个,代码如下: html代码: <style type="text/css"> *{margin:;padding ...