dhu 1180 诡异的楼梯

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

char Map[25][25];
int vist[25][25];
int dir[][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
int n, m;
struct node {

    int x, y;
    int time;
};
int sx, sy, ex, ey;
int ans;

bool judge(int a, int b) {

    if(a >= 1 && a <= n && b >= 1 && b <= m&& Map[a][b] != '*'&&vist[a][b] == 0)
        return true;
    return false;
}

void work(int sx, int sy) {

    memset(vist, 0, sizeof(vist));
    queue<node>que;
    node cur, next;
    cur.x = sx;
    cur.y = sy;
    cur.time = 0;
    vist[sx][sy] = 1;
    que.push(cur);
    while(!que.empty()) {

        cur = que.front();
        que.pop();
       // printf("%d  %d %d\n", cur.x, cur.y , cur.time);
        if(cur.x == ex && cur.y == ey) {

            ans = cur.time;
            return;
        }
    for(int i = 0; i < 4; i++) {

        int a = cur.x + dir[i][0];
        int b = cur.y + dir[i][1];
        if(judge(a, b)) {

            if(Map[a][b] == '|' || Map[a][b] == '-') {

                if(cur.time %2 == 0) {

                    if((Map[a][b] == '|' && i < 2)||(Map[a][b] == '-'&&i > 1)) {

                        a = a + dir[i][0];
                        b = b + dir[i][1];
                    }else {

                        next = cur;
                        next.time++;
                        que.push(next);
                        continue;
                    }
                }else {

                if((Map[a][b] == '|' && i > 1)||(Map[a][b] == '-'&&i < 2)) {

                        a = a + dir[i][0];
                        b = b + dir[i][1];
                    }else {

                        next = cur;
                        next.time++;
                        que.push(next);
                        continue;
                    }

                }
            }
            if(judge(a, b)) {
                if(Map[a][b] == 'T') {
                    ans = cur.time + 1;
                    return ;
                }
                else if(Map[a][b] == '.'){
                next.x = a;
                next.y = b;
                next.time = cur.time+1;
                vist[a][b] = 1;
                que.push(next);
                }
            }
        }
     }
    }
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF) {

        for(int i = 1; i <= n; i++) {

            scanf("%s", Map[i] + 1);
            for(int j = 1; j <= m; j++) {

                if(Map[i][j] == 'T') {

                    ex = i;
                    ey = j;
                }
                if(Map[i][j] == 'S') {

                    sx = i;
                    sy = j;
                }
            }
        }
        ans = 0;
        work(sx, sy);
        printf("%d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值