HDU1180+BFS
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的更多相关文章
- 诡异的楼梯(bfs)hdu1180
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...
- HDU1180:诡异的楼梯(bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 Problem Description Hogwarts正式开学以后,Harry发现在Hogwarts里, ...
- hdu1180 优先队列bfs+判断方向
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Subm ...
- hdu1180奇怪的楼梯……bfs迷阵……wa该16二级,我太渣滓
#include<iostream> #include<queue> #include<cstring> using namespace std; int row, ...
- hdu1180 诡异的楼梯 bfs
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1180/ 题目和不同的bfs有个不同的地方就是存在横着的或者竖着的楼梯,楼梯每过一个时刻就改变一次横竖的走向,人可 ...
- BFS的小结
写这类搜索题.首先感觉要有个框架.比如我的框架对于BFS来说(对于DFS,我想有两个一个是递归版一个是栈版).这里是BFS小结.所以介绍一下BFS.我的框架.(也是搜集了网上许多神人的作品.) 1:节 ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- 【BZOJ-1656】The Grove 树木 BFS + 射线法
1656: [Usaco2006 Jan] The Grove 树木 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 186 Solved: 118[Su ...
- POJ 3278 Catch That Cow(bfs)
传送门 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 80273 Accepted: 25 ...
随机推荐
- OC9_字符串的内存管理
// // main.m // OC9_字符串的内存管理 // // Created by zhangxueming on 15/6/18. // Copyright (c) 2015年 zhangx ...
- jquery-ui 中treegird 逐步加载
官方网站上没有ajax逐步加载的例子,自己研究了下 js代码 $("#bomStructureTable").treegrid({ url : "systemcontro ...
- java集合 collection-list-ArrayList 将自定义对象作为元素存到ArrayList集合中,并去除重复元素。
import java.util.*; /* 将自定义对象作为元素存到ArrayList集合中,并去除重复元素. 比如:存人对象.同姓名同年龄,视为同一个人.为重复元素. 思路: 1,对人描述,将数据 ...
- maven中scope参数说明
官方说明文档地址https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Depen ...
- class不想被复制的两个做法
1,当一个class不想被复制的时候,可以将copy构造函数和copy assignment操作符声明为private.(只声明不定义,因此可以不指定函数参数) 2,或者,继承一个专门为了阻止copy ...
- vs工程链接出现error LNK2005...already defined
今天使用vs2008编译工程无错误,链接过程,出现很多这样的错误: error LNK2005: "public: __thiscall std::basic_string<char, ...
- Qt 日志宏
随便写了一个日志帮助的宏,既可以如同qDebug()一般在调试时输出信息,也可以在输出文本文件 #ifndef LOG_H #define LOG_H #include <QDir> #i ...
- PHP网页的工作原理
网络基本概念 IP地址 唯一标识网络上的主机或设备. IP地址是由四段8位二进制构成,中间用小数点隔开.如:192.168.18.70 每一段取值0-255的十进制. 特殊的IP地址:127.0.0. ...
- 在emacs里用w3m浏览网页
给w3m配置个~/.emacs老是暴错误,在配置里把这个注掉就OK了;(require 'mime-w3m) ;;支持w3m (add-to-list 'load-path "/usr/sh ...
- C/C++错误分析errno,perror,strerror和GetLastError()函数返回的错误代码的意义
在C语言编译中,经常会出现一些系统的错误,这些错误如果在编译的时候不能很好的“预见”,会使系统“崩溃”,常见的捕获错误函数有: errno #include<errno.h> 这个变量是程 ...