hdoj1180 诡异的楼梯

题意

  走迷宫,求最少步数,很容易就想到BFS,而这是一道加了点难度的BFS,但是框架是不变的。唯一的难点就是“梯子”。题意中有两个地方需要理清:
  1.梯子可以有很多个
  2.人物可以在某个时候选择停止,不走

  另外,梯子的方向,是随着时间(或者说走的步数)的增加而进行,因此这里存在一个类似于“奇偶性”的问题。即:
  (1)如果梯子开始的时候是“|”,那么走了奇数步之后就会变成“-”,偶数步就还是“|”。
  (2)如果梯子一开始是“-”,那么奇数步后是“|”,偶数步后是“-”。

  关键的处理“梯子”的逻辑:当遇到梯子的时候,要么可以“越过”, 要么“等待1秒”,但首先得判断越过梯子之后的那个格子能不能走,如果不能走的话,那么就没有必要进行这两种选择了。

代码:

// 0ms, 1804K
/*
*BFS
*题意要明确的地方有:
*   (1)人物可以停留即不走,让时间过1秒,以等楼梯变向
*   (2)梯子可以有很多个,只是不会相连 
*技巧:
*   (1)奇偶性校验判断能不能过梯子 
*/ 
#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>
using namespace std;

struct Point {
    int x, y, step;

    Point(int x, int y, int s) : x(x), y(y), step(s) {
    }
};

const int MAXN(21);
const int DIR[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};   // go up, down, left or right
const int UP(0), DOWN(1), LEFT(2), RIGHT(3);
char maze[MAXN][MAXN];
bool isVisited[MAXN][MAXN];
int row, col;
int startX, startY, endX, endY;

bool isValid(int x, int y) {
    return x >= 0 && x < row && y >= 0 && y < col;
}

void init() {
    memset(isVisited, false, sizeof isVisited);
}

int bfs() {
    queue<Point> points;
    points.push( Point(startX, startY, 0) );
    isVisited[startX][startY] = true;
    while (!points.empty()) {
        Point cur = points.front();
        points.pop();
        if (cur.x == endX && cur.y == endY) {   // 终点 
            return cur.step;
        }
        for (int dir = 0; dir < 4; ++dir) { // 往4个方向探索 
            int xx = cur.x + DIR[dir][0];
            int yy = cur.y + DIR[dir][1];
            if (!isValid(xx, yy) || isVisited[xx][yy] || maze[xx][yy] == '*') { // 不能走的情况 
                continue;
            }
            if (maze[xx][yy] == '.' || maze[xx][yy] == 'T') {
                points.push( Point(xx, yy, cur.step + 1) );
                isVisited[xx][yy] = true;
            } else if (maze[xx][yy] == '-' || maze[xx][yy] == '|') {
                int xxx = xx + DIR[dir][0];
                int yyy = yy + DIR[dir][1];
                if (isValid(xxx, yyy) && !isVisited[xxx][yyy] && maze[xxx][yyy] != '*') {   // 共同前提:梯子另一边的格子可以走 
                    bool isLadderVertical;  // 标记梯子目前是竖的还是横的,这主要是看走了几步 
                    if (maze[xx][yy] == '-') {
                        // 如果一开始是'-',走了奇数步就是'|',偶数步就还是'-' 
                        isLadderVertical = (cur.step & 1) ? true : false;
                    } else if (maze[xx][yy] == '|') {
                        // 而如果一开始是'|',走了奇数步就是'-',偶数步就还是'|' 
                        isLadderVertical = (cur.step & 1) ? false : true;
                    }

                    if ((isLadderVertical && (dir == UP || dir == DOWN))
                        || (!isLadderVertical && (dir == LEFT || dir == RIGHT))) {  // 如果梯子满足方向, 
                        points.push( Point(xxx, yyy, cur.step + 1) );   // 就走梯子 
                        isVisited[xxx][yyy] = true;
                    } else {    // 如果梯子和现在的方向不一致, 
                        points.push( Point(cur.x, cur.y, cur.step + 1) );   // 那就等1秒 
                    }
                }
            }
        }
    }
}

int main() {
//  freopen("in.txt", "r", stdin);
//  freopen("out_1180.txt", "w", stdout);
    while (cin >> row >> col) {
        init();
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                cin >> maze[i][j];
                switch (maze[i][j]) {
                    case 'S':
                        startX = i;
                        startY = j;
                        break;
                    case 'T':
                        endX = i;
                        endY = j;
                        break;
                    default: 
                        break; 
                }
            }
        }
        cout << bfs() << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值