Problem地址:http://acm.hdu.edu.cn/showproblem.php?pid=2364

这道题的特殊之处在于能转弯时不能直走,必须转弯,所以在行走时,要判断能否转弯,不能转弯时才选择直走。

因为是一道走迷宫的题,所以可以用BFS解决问题。

有一点需要注意:起点也有可能是终点,所以在判断是否到终点时,最好判断该点是不是'#',而不该判断是不是'.',因为终点有可能是'@'

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue> using namespace std; const int NEVERCOME = -1;
const int NORTH = 0;
const int SOUTH = 2;
const int WEST = 1;
const int EAST = 3; const int MAXN = 80 + 5;
int vis[MAXN][MAXN][5]; // the first param means x, the second one means y
// the third one means dir
char maze[MAXN][MAXN]; // record the maze
int moveToDir[4][2]; int Bfs( int height, int width, int startX, int startY );
bool isEnd( int &height, int &width, int &x, int &y ); // judge whether the person has reached the end point
bool isInside( int &height, int &width, int &x, int &y ); // judge whether the person has reached the end point int main()
{
// dir
moveToDir[NORTH][0] = -1;
moveToDir[NORTH][1] = 0; moveToDir[SOUTH][0] = 1;
moveToDir[SOUTH][1] = 0; moveToDir[WEST][0] = 0;
moveToDir[WEST][1] = -1; moveToDir[EAST][0] = 0;
moveToDir[EAST][1] = 1; int T;
cin >> T;
int height, width;
int i, j;
int startX, startY;
while( T-- ) {
scanf( "%d%d", &height, &width );
for( i=0; i<height; i++ ) {
getchar();
for( j=0; j<width; j++ ) {
scanf( "%c", &maze[i][j] );
if( maze[i][j]=='@' ) {
startX = i;
startY = j;
}
//printf( "%c", maze[i][j] );
}
//printf("\n");
}
printf( "%d\n", Bfs( height, width, startX, startY ) );
}
return 0;
} typedef struct Node{
int x, y;
int step;
int dir; // means the dir the person facing
}Node; int Bfs( int height, int width, int startX, int startY ) {
queue <Node> q;
Node t, next;
t.x = startX;
t.y = startY;
t.step = 0; memset( vis, NEVERCOME, sizeof(vis) );
int i, j;
// the four directions
t.dir = NORTH;
vis[t.x][t.y][t.dir] = 0;
q.push( t ); t.dir = SOUTH;
vis[t.x][t.y][t.dir] = 0;
q.push( t ); t.dir = WEST;
vis[t.x][t.y][t.dir] = 0;
q.push( t ); t.dir = EAST;
vis[t.x][t.y][t.dir] = 0;
q.push( t ); while( !q.empty() ) {
t = q.front();
q.pop(); if( isEnd( height, width, t.x, t.y ) ) {
return t.step;
}
int i;
bool flag = false;
// can the person go left or right
for( i=0; i<4;i++ ) {
if( (i&1) != ( t.dir&1 ) ) { // need to turn
next.x = t.x + moveToDir[i][0];
next.y = t.y + moveToDir[i][1];
next.step = t.step + 1;
next.dir = i; if( isInside( height, width, next.x, next.y ) ) {
flag = true;
if( vis[next.x][next.y][next.dir]==-1 || next.step<vis[next.x][next.y][next.dir] ) {
q.push(next);
vis[next.x][next.y][next.dir] = next.step;
}
}
}
} // should go straight ?
if( flag == false ) {
next.x = t.x + moveToDir[t.dir][0];
next.y = t.y + moveToDir[t.dir][1];
next.step = t.step + 1;
next.dir = t.dir; if( isInside( height, width, next.x, next.y ) ) {
if( vis[next.x][next.y][next.dir]==-1 || next.step<vis[next.x][next.y][next.dir] ) {
q.push(next);
vis[next.x][next.y][next.dir] = next.step;
}
} }
} return -1;
} bool isEnd( int &height, int &width, int &x, int &y ) {
if( maze[x][y] != '#' ) {
if( ( x==0 || x==height-1 ) || ( y==0 || y==width-1) ) {
return true;
}
}
return false;
} bool isInside( int &height, int &width, int &x, int &y ) {
if( maze[x][y] == '.' ) {
if( x>=0 && x<height && y>=0 && y<width ) {
return true;
}
}
return false;
}

Hdu 2364 Escape的更多相关文章

  1. hdu 2364 Escape【模拟优先队列】【bfs】

    题目链接:https://vjudge.net/contest/184966#problem/A 题目大意: 走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往后走, ...

  2. HDU 3533 Escape(大逃亡)

    HDU 3533 Escape(大逃亡) /K (Java/Others)   Problem Description - 题目描述 The students of the HEU are maneu ...

  3. HDU 3605 Escape (网络流,最大流,位运算压缩)

    HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...

  4. Hdu 3605 Escape (最大流 + 缩点)

    题目链接: Hdu 3605  Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...

  5. HDU 3605 Escape(状压+最大流)

    Escape Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  6. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

  7. HDU 3605 Escape 最大流+状压

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others)    ...

  8. hdu 3605 Escape 二分图的多重匹配(匈牙利算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others)    ...

  9. HDU 3533 Escape bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=3533 一道普通的bfs,但是由于代码实现出了bug还是拖了很久甚至对拍了 需要注意的是: 1.人不能经过炮台 2 ...

随机推荐

  1. centos6.5设备mysql5.6

    1. 首先检查版本号number # uname -a 要么 # cat /etc/redhat-release CentOS release 6.6 (Final) 2. 下载并安装Mysql的yu ...

  2. JAVA处理XML

    <xml>  <ToUserName><![CDATA[toUser]]></ToUserName>  <FromUserName>< ...

  3. 使用Highcharts生成折线图与曲线图

    折线图与曲线图可以显示随时间而变化的连续数据,因此非常适用于显示在相等时间间隔下数据的趋势.本文将结合Highcharts,生成一个城市气温变化折线图和一个随时间动态即时显示CPU走势的曲线图. 如果 ...

  4. USACO Seciton 5.4 Canada Tour(dp)

    因为dp(i,j)=dp(j,i),所以令i>j. dp(i,j)=max(dp(k,j))+1(0<=k<i),若此时dp(i,j)=1则让dp(i,j)=0.(因为无法到达此状态 ...

  5. c语言(3)--运算符&表达式&语句

    计算机的本职工作是进行一系列的运算,C语言为不同的运算提供了不同的运算符! 1.那些运算符们 .基本运算符 算术运算符:+ - * /  % ++ -- 赋值运算符:= 逗号运算符:, 关系运算符:& ...

  6. QT全局热键(用nativeKeycode封装API,不跨平台)

    在网上找了很长时间,大家都提到了一个QT全局热键库(qxtglobalshortcut),支持跨平台.在这篇文章中,我将只展示出windows平台下全局热键的设置. 这里提供的方法是在MyGlobal ...

  7. nginx 重写 rewrite 基础及实例(转)

    nginx rewrite 正则表达式匹配 大小写匹配 ~ 为区分大小写匹配 ~* 为不区分大小写匹配 !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 文件及目录匹配 -f和!-f用来判断是否 ...

  8. linux 查看文件系统类型

    1. mount [op@TIM ~]$ mount/dev/mapper/vg_tim-lv_root on / type ext4 (rw)proc on /proc type proc (rw) ...

  9. OC学习那些事:点语法

    1.使用自定义的方法创建get/set方法 Person.h文件: #import <Foundation/Foundation.h> @interface Person : NSObje ...

  10. [每日一题] 11gOCP 1z0-052 :2013-09-15 Enterprise Manager Support Workbench..................B9

    转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/11715219 正确答案:ABD EnterpriseManger Support Work ...