题目链接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搜索)的更多相关文章

  1. M - 诡异的楼梯 HDU - 1180(BFS + 在某个点等待一下 / 重复走该点)

    M - 诡异的楼梯 HDU - 1180 Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向. 比如下面的例子里,一开始楼梯 ...

  2. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  3. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  4. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  5. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  6. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

  7. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. 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 ...

  9. HDU 4499.Cannon 搜索

    Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

随机推荐

  1. emu1

    第一题 一个很奇怪的贪心.先排序一遍,再扫描一遍,能加入尽量加入,不能加入就一定不能加入..由于每次都在可能的最早时间加入一个数可以保证差最小?反正差不多这样了. O(n log n) #includ ...

  2. 用Python操纵MySQL

    本例用Python操纵MySQL,从指定文件读取数据,并对数据进行处理,处理之后批量插入MySQL. 贴上代码: # -*- coding: gbk -*- import re import MySQ ...

  3. BADIP filter

    #!/bin/bash touch /tmp/badipnew.log;touch /tmp/newip.log; if [ ! -f "/tmp/badip.log" ];the ...

  4. Python: 什么是*args和**kwargs

    转自: http://blog.sina.com.cn/s/blog_65a8ab5d0101fglm.html http://blog.csdn.net/chenjinyu_tang/article ...

  5. Rehashing

    The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...

  6. recv和send函数

    转自  http://www.cnblogs.com/blankqdb/archive/2012/08/30/2663859.html 1. send解析 sockfd:指定发送端套接字描述符. bu ...

  7. 编译安装0bda 8179无线网卡

    CentOS下安装USB无线网卡(Obda:8179) 参考:http://blog.163.com/thinki_cao/blog/static/83944875201311593529913/ c ...

  8. MYSQL 删除字段值为NULL的语法

    2014年9月1日 15:11:05 delete form your_table where your_field is null and your_field1 = '123' ...

  9. 浅谈mysql集群

    一.什么是MySQL集群 MySQL集群是一个无共享的(shared-nothing).分布式节点架构的存储方案,其目的是提供容错性和高性能. 数据更新使用读已提交隔离级别(read-committe ...

  10. Java for LeetCode 072 Edit Distance【HARD】

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...