HDU---2364 Escape【广度优先搜索】

原创 2017年09月13日 20:55:37
Problem Description
You find yourself trapped in a large rectangular room, made up of large square tiles; some are accessible, others are blocked by obstacles or walls. With a single step, you can move from one tile to another tile if it is horizontally or vertically adjacent (i.e. you cannot move diagonally).
To shake off any people following you, you do not want to move in a straight line. In fact, you want to take a turn at every opportunity, never moving in any single direction longer than strictly necessary. This means that if, for example, you enter a tile from the south, you will turn either left or right, leaving to the west or the east. Only if both directions are blocked, will you move on straight ahead. You never turn around and go back!
Given a map of the room and your starting location, figure out how long it will take you to escape (that is: reach the edge of the room).


 

Input
On the first line an integer t (1 <= t <= 100): the number of test cases. Then for each test case:

a line with two integers separated by a space, h and w (1 <= h, w <= 80), the height and width of the room;

then h lines, each containing w characters, describing the room. Each character is one of . (period; an accessible space), # (a blocked space) or @ (your starting location).
There will be exactly one @ character in each room description.
 

Output
For each test case:

A line with an integer: the minimal number of steps necessary to reach the edge of the room, or -1 if no escape is possible.
 

Sample Input
2 9 13 ############# #@..........# #####.#.#.#.# #...........# #.#.#.#.#.#.# #.#.......#.# #.#.#.#.#.#.# #...........# #####.####### 4 6 #.#### #.#.## #...@# ######
 

Sample Output
31 -1

SOURCE:点击打开链接

题意:

自己被困在迷宫中,要求可以前进,左转,右转。然而在遇到能转弯的地方时必须转弯,除非不能转弯或者是转弯才能直行,问至少经过多少步才能走出迷宫。

解析:

略微变形的BFS,重点在于向左右方向移动。所以用vis数组来记录所处的状态。

代码:

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

int h,w;
int vis[85][85][4];    //用来记录所处的位置及状态
int dx[4]= {0,1,0,-1};
int dy[4]= {1,0,-1,0};
char str[85][85];

int bfs(const int x,const int y);

class node
{
public:
    int x;
    int y;
    int step;
    int face;
    node() {};
    node (int x,int y,int step,int face);
};

node::node(int x,int y,int step,int face)
{
    this->x=x;
    this->y=y;
    this->step=step;
    this->face=face;
}

int main(void)
{
    int t,x,y;
    cin>>t;
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        cin>>h>>w;
        for(int i=0; i<h; i++)
            for(int j=0; j<w; j++)
            {
                cin>>str[i][j];
                if(str[i][j]=='@')
                {
                    x=i;
                    y=j;
                }
            }
        cout<<bfs(x,y)<<endl;
    }
    return 0;
}

int bfs(const int x,const int y)
{
    queue<node> q;
    node n(x,y,0,-1);   //在起始位置可以面向任意方向,所以face为-1
    int step,face,nx,ny;
    bool flag;   //用来判断是否只能直行
    vis[x][y][0]=1,vis[x][y][1]=1;   //在起始位置的四种状态都为0,因为在行走的过程中不能经过起始状态
    vis[x][y][2]=1,vis[x][y][3]=1;
    q.push(n);
    while(q.size())
    {
        flag=true;
        n=q.front();
        q.pop();
        if(n.x==0||n.x==h-1||n.y==0||n.y==w-1)
            return n.step;
        for(int i=0; i<4; i++)  //看看能不能转弯
        {
            if(i%2!=n.face%2)  //只能向左右方向走
            {
                nx=n.x+dx[i];
                ny=n.y+dy[i];
                step=n.step+1;
                face=i;
                if(nx>=0&&nx<h&&ny>=0&&ny<w&&str[nx][ny]=='.')
                {
                    flag=false;  //只要左右方向能走就走,不管走没走过
                    if(!vis[nx][ny][face])  //若没有走过,将其加入队列
                    {
                        if(nx==0||nx==h-1||ny==0||ny==w-1)
                            return step;
                        q.push(node(nx,ny,step,face));
                        vis[nx][ny][face]=1;
                    }
                }
            }
        }
        if(flag)  //若不能转弯,则只能直行
        {
            nx=n.x+dx[n.face];
            ny=n.y+dy[n.face];
            step=n.step+1;
            face=n.face;
            if(nx>=0&&nx<h&&ny>=0&&ny<w&&str[nx][ny]=='.'&&!vis[nx][ny][face])
            {
                if(nx==0||nx==h-1||ny==0||ny==w-1)
                    return step;
                q.push(node(nx,ny,step,face));
                vis[nx][ny][face]=1;
            }
        }
    }
    return -1;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ---3984 迷宫问题【BFS+回溯法】

Description 定义一个二维数组:  int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1...

各大OJ题

模拟题: POJ1006POJ1008POJ1013POJ1016POJ1017POJ1169POJ1298POJ1326POJ1350POJ1363POJ1676POJ1786POJ1791POJ...

poj 1077 hdu 1043 Eight 八数码问题 DBFS(双向广度优先搜索)a*算法 康拓展开

一,八数码问题简介 编号为1到8的8个正方形滑块被摆成3行3列(有一个格子留空),可以每次把与空格相邻(有公共边)的滑块移动到空格中,而它原来的位置就成了新的空格。给定局面,计算出从当前状态移动到目标...
  • gwq5210
  • gwq5210
  • 2015年08月29日 20:40
  • 1001

HDU 1175 广度优先搜索(BFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1175 题意:能不能将两个位置的棋子(非0)消去,并不能超过两次改变方向. 题型:广度优先搜索(BFS) ...

Hdu1072 - Nightmare - 广度优先搜索

#include int m,n; int map[10][10]; int to[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int mark[10][10]; struc...
  • wsxsd94
  • wsxsd94
  • 2014年07月19日 12:45
  • 267

BFS广度优先搜索(11)--hdu2102(基础题)

A计划 TimeLimit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u Description In...

BFS广度优先搜索(13)--hdu3533(能力题)

Escape                                          Time Limit:10000MS    Memory Limit:32768KB   ...

广度优先搜索——HDU1372

象棋里的马从a到达b的最小步数 -来源HDU1010

Hdu1242 - Rescue - 广度优先搜索

#include #include int m,n; char map[210][210]; int mark[210][210][2]; int to[4][2]={{0,1},{1,0},{0,-...
  • wsxsd94
  • wsxsd94
  • 2014年07月19日 11:37
  • 291

HDU 4230 Robot Navigation (记忆化广度优先搜索)

这题WA了十多次。昨晚调试了一晚上,今天下午又调试了好久还是AC不能,于是决定重敲。重敲之后一次AC,然后又进行了一些优化,109ms。 Run ID Submit Tim...
  • ahfywff
  • ahfywff
  • 2012年04月28日 15:56
  • 622
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU---2364 Escape【广度优先搜索】
举报原因:
原因补充:

(最多只允许输入30个字)