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. 解决word转pdf后图片失真

    碰到问题: 将word转pdf后图片出现失真 问题分析: 上述问题必定跟图片类型和所用软件有关,现将不同图片在不同软件下的失真情况汇总,见表1 问题解决:迫不得已,不要使用截图:若必需要用,则word ...

  2. ceph启动脚本

    放在/etc/init.d/目录下,用法如下: root@u253:~# /etc/init.d/ceph === mon.a === usage: /etc/init.d/ceph [options ...

  3. Node.js模块os

    OS 操作系统模块 os.hostname() 操作系统的主机名. os.type() 操作系统的名称 os.release() 操作系统的发行版本 os.uptime() 当前系统的时间 以秒为 o ...

  4. SQL Server2012新特性概述

    公司最近要升级数据库,SQL Server 2008R2-->2012.再开始升级之前先找了点资料分析一下2012的新特性和功能,提前预热一下. 2012中主要关注一下三个领域: 性能:改进的核 ...

  5. LINQ的用法

    http://www.cnblogs.com/liulun/archive/2013/02/26/2909985.html(转载)

  6. 安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机

    版权声明:本文为博主原创文章,未经博主允许不得转载. 我们安装VMware vSphere 的目的就是在一台物理服务器上安装很多很多的虚拟机,我们可以通过VMware vSphere Client直接 ...

  7. Tomcat embed

    http://www.iflym.com/index.php/code/use-embeded-tomcat-to-javaee-start-tomcat.html http://java.dzone ...

  8. Qt实战之开发软件数据获取助手(eventFilter处理鼠标按下,event处理鼠标松开)

    前段时间,受朋友委托,需要做一个能够获取别人软件文本框中内容的助手.当然这需要调用win api来解决问题.一开始,我想都没想,就用getWindowText()....居然没用,好郁闷.于是查msd ...

  9. php 登录实例演示

    <pre name="code" class="python">一.模板的使用 (重点) a.规则 模板文件夹下[TPL]/[分组文件夹/][模板主 ...

  10. Win32 GDI基础(笔记)

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