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. [cocos2dx笔记015]关于cocos2dx Button三种状态说明

    经过几天的填坑,最终将现有的项目由cocos2dx 2.2.2移到cocos2dx 3.2,差点放弃3.2了,但在最后一刻,又把坑填平了. cocos2dx 2.x到3.x是一个巨大的变化,能够算是全 ...

  2. Javascript声明变量类型

    声明变量类型 当您声明新变量时,可以使用关键词 "new" 来声明其类型: var carname=new String; var x= new Number; var y= ne ...

  3. word排版论文小结

    毕业论文如何用WORD排版 本人折腾了一晚上看别人的百度经验,做个总结,方便后人看懂,其实特别简单 用WORD自动生成页码 第一种情况:从第一页或者从第二页开始设置页码 这种情况只要“插入-> ...

  4. 关于js封装框架类库之样式操作

    在js中,对样式的操作我们并不感到陌生,在很多框架中都是用极少的代码,实现更强大的功能,在这做出一些的总结.存在不足还望指出! 1.封装一个添加css的方法(这篇引用了前面的框架结构) 在 js 中 ...

  5. python初探-collections容器数据类型

    collections容器数据类型是对基本数据类型的补充,简单介绍下计数器.有序字典.默认字典.可命名元祖.队列. 计数器(Counter) Counter是对字典类型的补充,用于追踪值得出现次数 c ...

  6. Android系统设置— android.provider.Settings

    android.provider.Settings Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS); sta ...

  7. Win32 GDI基础(笔记)

    1.GDI名字的意义 GDI Graphic Device Interface,我说不清和GUI有什么区别.可能一种针对设备,一种针对用户而言吧,反正以后都说GDI,也就是Windows的图形编程. ...

  8. 配置管理多个ssh key

    假设需要为两台主机建立ssh key以便抓取.更新代码,主机A:111.111.111.111,主机B:222.222.222.222. 首先,用两个账户(hostA@email.com与hostB@ ...

  9. POJ-1010 Stamps

    [题目描述] 题目大意是:邮票发行商会发行不同面值.不同种类的邮票给集邮爱好者,集邮爱好者有总目标面额,通过不同的邮票组合(总数在4张以内)达到该面值,卖给集邮爱好者.另外,发行商发行的邮票面值最多2 ...

  10. Ajax概述及浅谈其与服务器的交互过程

    概念: 首先AJAX不只是一个特定的客户端技术,更应算是一种技巧.Ajax技术的核心操作是用XmlHttpRequest(下称XHR)对象进行异步数据处理. 所谓异步,即通过 AJAX,JavaScr ...