bfs
思路:三维标记状态 && 处理好 - | 和时刻的关系即可

 /*
bfs
思路:三维标记状态 && 处理好 - | 和时刻的关系即可
*/
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const int dx[]={,-,,};
const int dy[]={,,,-};
const double eps = 1e-;
const int maxm = ;
const int maxn = ; struct Point {
int x,y,t;
};
Point s,e; bool vis[ maxn ][ maxn ][ ];
char mat[ maxn ][ maxn ];
queue<Point>q; int bfs( int n,int m ){
memset( vis,false,sizeof( vis ) );
while( !q.empty() )
q.pop();
Point cur = s;
vis[ s.x ][ s.y ][ s.t% ] = true;
q.push( cur );
while( !q.empty() ){
cur = q.front();
q.pop();
//printf("\ncur:[%d,%d] t = %d\n",cur.x,cur.y,cur.t);
if( cur.x==e.x && cur.y==e.y ){
e.t = min( e.t,cur.t );
}
if( cur.t>=e.t ) continue;
for( int i=;i<;i++ ){
Point nxt;
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.t = cur.t + ;
if( nxt.x<||nxt.x>=n||nxt.y<||nxt.y>=m ) continue;
if( mat[ nxt.x ][ nxt.y ]=='*' ) continue;
if( mat[ nxt.x ][ nxt.y ]=='.' ){
if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==true ) continue;
vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
q.push( nxt );
//printf("0 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
continue;
}//
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.t = cur.t + ;
if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )){
if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + ;
if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - ;
if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
q.push( nxt );
//printf("1 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
continue;
}
}
}//1 ‘-’ 上下无需等待时间
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.t = cur.t + ;
/*恢复nxt很重要*/
if(( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )){
nxt.t = cur.t + ;
if( cur.x==nxt.x&&nxt.y>cur.y ) nxt.y = cur.y + ;
if( cur.x==nxt.x&&nxt.y<cur.y ) nxt.y = cur.y - ;
if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
q.push( nxt );
//printf("2 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
continue;
}
}
}//2 ‘-’ 上下需要等待时间
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.t = cur.t + ;
/*恢复nxt很重要*/
if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )){
if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + ;
if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - ;
if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
q.push( nxt );
//printf("3 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
continue;
}
}
}//3 ‘|’ 左右无需等待时间
nxt.x = cur.x + dx[ i ];
nxt.y = cur.y + dy[ i ];
nxt.t = cur.t + ;
/*恢复nxt很重要*/
if(( mat[ nxt.x ][ nxt.y ]=='|'&&cur.t%== )||( mat[ nxt.x ][ nxt.y ]=='-'&&cur.t%== )){
nxt.t = cur.t + ;
if( cur.y==nxt.y&&nxt.x>cur.x ) nxt.x = cur.x + ;
if( cur.y==nxt.y&&nxt.x<cur.x ) nxt.x = cur.x - ;
if( nxt.x>=&&nxt.x<n&&nxt.y>=&&nxt.y<m&&mat[ nxt.x ][ nxt.y ]=='.' ){
if( vis[ nxt.x ][ nxt.y ][ nxt.t% ]==false ){
vis[ nxt.x ][ nxt.y ][ nxt.t% ] = true;
q.push( nxt );
//printf("4 nxt:[%d,%d] t = %d\n",nxt.x,nxt.y,nxt.t);
continue;
}
}
}//4 ‘|’ 左右需要等待时间
}
}
return e.t;
} int main(){
int n,m;
//freopen( "in.txt","r",stdin );
while( scanf("%d%d",&n,&m)== ){
for( int i=;i<n;i++ ){
scanf("%s",mat[ i ]);
for( int j=;j<m;j++ ){
if( mat[ i ][ j ]=='S' ){
s.x = i;
s.y = j;
s.t = ;
mat[ i ][ j ] = '.';
}
else if( mat[ i ][ j ]=='T' ){
e.x = i;
e.y = j;
e.t = inf;
mat[ i ][ j ] = '.';
}
}
}
printf("%d\n",bfs( n,m ));
}
return ;
}

HDU1180+BFS的更多相关文章

  1. 诡异的楼梯(bfs)hdu1180

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...

  2. HDU1180:诡异的楼梯(bfs+优先队列)

    http://acm.hdu.edu.cn/showproblem.php?pid=1180 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里, ...

  3. hdu1180 优先队列bfs+判断方向

    诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Subm ...

  4. hdu1180奇怪的楼梯……bfs迷阵……wa该16二级,我太渣滓

    #include<iostream> #include<queue> #include<cstring> using namespace std; int row, ...

  5. hdu1180 诡异的楼梯 bfs

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1180/ 题目和不同的bfs有个不同的地方就是存在横着的或者竖着的楼梯,楼梯每过一个时刻就改变一次横竖的走向,人可 ...

  6. BFS的小结

    写这类搜索题.首先感觉要有个框架.比如我的框架对于BFS来说(对于DFS,我想有两个一个是递归版一个是栈版).这里是BFS小结.所以介绍一下BFS.我的框架.(也是搜集了网上许多神人的作品.) 1:节 ...

  7. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  8. 【BZOJ-1656】The Grove 树木 BFS + 射线法

    1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 186  Solved: 118[Su ...

  9. POJ 3278 Catch That Cow(bfs)

    传送门 Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 80273   Accepted: 25 ...

随机推荐

  1. Contoso 大学 - 9 - 实现仓储和工作单元模式

    原文 Contoso 大学 - 9 - 实现仓储和工作单元模式 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Micros ...

  2. oracle视图索引

    reate table fleet_header(  day date,name varchar2(20),  route_id number(5),fleet_id number(5)); crea ...

  3. OpenGL8-直接分配显存-极速绘制(2)

    视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440/*** OpenGL8-直接分配显存-极速绘制(Opengl1 ...

  4. LibCurl笔记四

    1,curl设置fiddler代理curl_easy_setopt(m_easyHandle, CURLOPT_PROXY, "127.0.0.1:8888"); 2,

  5. POJ 3928 Ping pong

    题目链接:http://poj.org/problem?id=3928 乒乓比赛,有N个人参加,输入每个玩家的技能等级,对每个人设置一个特定ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判 ...

  6. vim 跳转命令

    基本跳转: hjkl:左下上右 HML:当前屏幕顶.中.底部 web:下一单词词首.下一单词词尾.前一单词词首 gg:文件首  G:文件末尾  ngg/nG:第n行 ta:移动到所在行之后第一个字符a ...

  7. iOS 非ARC基本内存管理系列 2-多对象内存管理(1)

    单个对象的内存管理非常简单无非就是alloc对应release,retain对应release.但是如果涉及到很多对象,而且对象与对象有联系的时候该怎么去管理对象的内存呢. 比如同样一本书有好3个人购 ...

  8. Projected Coordinate Systems

    Coordinate Systems Projected Coordinate Systems This is an archive of a previous version of the ArcG ...

  9. *HTML5 新元素

    HTML5 新元素 自1999年以后HTML 4.01 已经改变了很多,今天,在HTML 4.01中的几个已经被废弃,这些元素在HTML5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,HT ...

  10. 如何使用Git——(二)

    8.首次安装git,然后要把创建好的仓库放到github上,首先,你要先做一些基本设置,继续设置吧~~. 在git黑框框中输入  ssh-keygen -t rsa -C "your ema ...