HDOJ 2364 Escape

29 篇文章 0 订阅
8 篇文章 0 订阅

题意:@为入口,在矩形边缘的点'.'为出口,找到一条最短路径并输出消耗的时间。要求优先考虑转向,若不能转向可以直走,但无论遇到什么情况都不能掉头。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2364

思路:正常的bfs,加上第三维度标记方向。若对同一个点同一方向走第二次时进行剪枝。

注意点:起点可能为出口,需要特判。存在一种情况,该点可以走2个或者3个方向,其中有一个方向时直走,剩余方向时转向但已经走过一次,需要进行加标记判断(代码中的flag为标记)。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
200749602017-03-11 13:42:07Accepted236415MS1776K3265 BG++luminous11
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

int h, w;
char str[100][100];
int d[100][100][4];
int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct  node
{
    int x, y;
    int dir;
    int step;
};


void init()
{
    for ( int i = 0; i < 100; i ++ ){
        for ( int j = 0; j < 100; j ++ ){
            str[i][j] = '#';
        }
    }
    memset ( d, 0, sizeof(d) );
}
int check( node nt,bool& flag )
{
    //printf ( "%d %d\n", nt.x, nt.y );
    if ( str[nt.x][nt.y] == '$'  ){
        return 2;
    }
    if ( str[nt.x][nt.y] == '.' ){
        flag = 1;
        if ( d[nt.x][nt.y][nt.dir] == 0 ){
            return 1;
        }
    }
    return 0;
}
int bfs(node be)
{
    queue<node> q;
    be.step = 0;
    for ( int i = 0; i < 4; i ++ ){
        be.dir = i;
        d[be.x][be.y][be.dir] = 1;
        q.push(be);
    }
    node now, nt;
    while ( ! q.empty() ){
        now =  q.front();q.pop();
        //printf ( "%d %d %d %d\n", now.x, now.y, now.step, now.dir );
        int pre[3];
        if ( now.dir == 0 ){
            pre[0] = 2;
            pre[1] = 3;
            pre[2] = 0;
        }
        if ( now.dir == 1 ){
            pre[0] = 2;
            pre[1] = 3;
            pre[2] = 1;
        }
        if ( now.dir == 2 ){
            pre[0] = 0;
            pre[1] = 1;
            pre[2] = 2;
        }
        if ( now.dir == 3 ){
            pre[0] = 0;
            pre[1] = 1;
            pre[2] = 3;
        }
        bool flag = 0;
        for ( int i = 0; i < 3; i ++ ){
            if ( flag == 1 && i >= 2 )break;
            nt.x = now.x + dir[pre[i]][0];
            nt.y = now.y + dir[pre[i]][1];
            nt.step = now.step + 1;
            nt.dir = pre[i];
            int tmp = check(nt,flag);
            if ( tmp ){
                q.push(nt);
                d[nt.x][nt.y][nt.dir] = 1;
                if ( tmp == 2 ){
                    return nt.step;
                }
            }
        }
    }
    return -1;
}
void preTreat()
{
    for ( int i = 0; i <= h; i ++ ){
        if ( str[i][0] == '.' )str[i][0] = '$';
        if ( str[i][w-1] == '.' )str[i][w-1] = '$';
    }
    for ( int i = 0; i <= w; i ++ ){
        if ( str[0][i] == '.' )str[0][i] = '$';
        if ( str[h-1][i] == '.' )str[h-1][i] = '$';
    }
    /*
    for ( int i = 0; i < h; i ++ ){
        for ( int j = 0; j < w; j ++ ){
            printf ( "%c", str[i][j] );
        }
        printf ( "\n" );
    }
    */
}
int main()
{
    int T;
    scanf ( "%d", &T );
    while ( T -- ){
        init();
        node be;
        scanf ( "%d%d", &h, &w );
        for ( int i = 0; i < h; i ++ ){
            scanf ( "%s", str[i] );
            for ( int j = 0; j < w; j ++ ){
                if ( str[i][j] == '@' ){
                    be.x = i;
                    be.y = j;
                    str[i][j] = '.';
                }
            }
        }
        if ( be.x == 0 || be.x == h-1 || be.y == 0 || be.y == w-1 ){
            printf ( "0\n" );continue;
        }
        preTreat();
        printf ( "%d\n", bfs(be) );
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值