HDU1180:诡异的楼梯(BFS)

诡异的楼梯
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 20219 Accepted Submission(s): 5264

Problem Description
Hogwarts正式开学以后,Harry发现在Hogwarts里,某些楼梯并不是静止不动的,相反,他们每隔一分钟就变动一次方向.
比如下面的例子里,一开始楼梯在竖直方向,一分钟以后它移动到了水平方向,再过一分钟它又回到了竖直方向.Harry发现对他来说很难找到能使得他最快到达目的地的路线,这时Ron(Harry最好的朋友)告诉Harry正好有一个魔法道具可以帮助他寻找这样的路线,而那个魔法道具上的咒语,正是由你纂写的.

Input
测试数据有多组,每组的表述如下:
第一行有两个数,M和N,接下来是一个M行N列的地图,’*‘表示障碍物,’.‘表示走廊,’|‘或者’-‘表示一个楼梯,并且标明了它在一开始时所处的位置:’|‘表示的楼梯在最开始是竖直方向,’-'表示的楼梯在一开始是水平方向.地图中还有一个’S’是起点,‘T’是目标,0<=M,N<=20,地图中不会出现两个相连的梯子.Harry每秒只能停留在’.'或’S’和’T’所标记的格子内.

Output
只有一行,包含一个数T,表示到达目标的最短时间.
注意:Harry只能每次走到相邻的格子而不能斜走,每移动一次恰好为一分钟,并且Harry登上楼梯并经过楼梯到达对面的整个过程只需要一分钟,Harry从来不在楼梯上停留.并且每次楼梯都恰好在Harry移动完毕以后才改变方向.

Sample Input
5 5
**…T
**..
…|…
.
.*.
S…

Sample Output
7

Hint
Hint

地图如下:
在这里插入图片描述

这题的bfs思路大概如下:
在这里插入图片描述

不遇到楼梯的话比较ez,就和普通的bfs一样就行(如8皇后问题)。遇到楼梯就很烦了,首先你可能碰到 - or | ,这其中又要看时间是奇数还是偶数,奇数楼梯方向就要改变(比如 | 变 - ,- 变 | ),偶数楼梯方向就不变,然后这下面走的方向又要分两种,是上下走呢,还是左右走呢。
所以最后就分得8中情况。

其中值得注意的几点:

  1. 比如说你要往上走,但是楼梯是横着的 ‘ - ’ ,并不是直接把这种情况排除了。因为题目说 “每秒只能停留在’.'或’S’和’T’所标记的格子内.”,所以可以等一秒,等他转成‘ | ’的时候再走。
    所以写code的时候就要有个暂时保存 i 和 j 的变量tempi 和 tempj 若这个楼梯可以走的,没挡路,就将tempi 和 tempj 再加一遍刚才的动作,(走上就在走上一次,走左就再走左一次),然后检查一下再走一次后是否仍然合法(不检查就MLE 爆空间了),如果合法就老样子三步走:1.标记 2.+1 3.push。

  2. 好多 i 和 j 啊,别打错了 : p

  3. 爆空间的原因:要么是走过的路径没有标记,要么是标记的位置不准确(打错了?)导致还有部分情况遗漏。

  4. 先写简单的再写难的,不然写了难得大舒一口气就把简单的给忘了,比如这题没有遇到楼梯的情况。

  5. 如果自己写的代码弄出来的结果和题目中给的output不一样,就沿着题目给的样例中的最优路径(自己找找应该就找到了),去找你代码里对应的那条路径,看看是不是有点小错误(没push 没标记 检测是否合法的条件写错 等等)。

下面附上ac代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <ctype.h>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
#define mod 1000000007
using namespace std;
typedef long long int ll;
int m,n;
int si,sj;
int tempi,tempj;
char mp[30][30];
int sgn[30][30];
int mv[6][4] = {{-1,0},{0,1},{1,0},{0,-1}};
struct node {
    int i,j,step;
};
bool check(int i,int j) {
    if(i < 0 || i >= m || j < 0 || j >= n) {
        return false;
    }
    if(sgn[i][j] == 1) {
        return false;
    }
    if(mp[i][j] == '*') {
        return false;
    }
    return true;
}
int bfs() {
    memset(sgn,0,sizeof(sgn));  //清空
    node cur,next;
    cur.i = si;
    cur.j = sj;
    sgn[cur.i][cur.j] = 1; //标记
    cur.step = 0;
    queue<node>q;
    q.push(cur);
    while(!q.empty()) {
        cur = q.front();
        q.pop();
        if(mp[cur.i][cur.j] == 'T') {
            return cur.step;
        }
        for(int k = 0; k < 4; k++) {
            tempi = cur.i + mv[k][0];
            tempj = cur.j + mv[k][1];
            if(check(tempi,tempj)) {
                int s = cur.step % 2; //若是0,则说明方向不变,若为1,方向改变
                if(mp[tempi][tempj] == '|') {
                    if(s == 1) {
                        if(k == 0 || k == 2) {
                            next.i = tempi - mv[k][0];
                            next.j = tempj - mv[k][1];
                            next.step = cur.step + 1;
                            q.push(next);
                        }
                        if(k == 1 || k == 3) {
                            next.i = tempi + mv[k][0];
                            next.j = tempj + mv[k][1];
                            if(check(next.i,next.j)) { //爆空间的原因,这里还要再check一次,傻了
                                sgn[next.i][next.j] = 1;
                                next.step = cur.step + 1;
                                q.push(next);
                            }
                        }
                    }
                    if(s == 0) {
                        if(k == 0 || k == 2) {
                            next.i = tempi + mv[k][0];
                            next.j = tempj + mv[k][1];
                            if(check(next.i,next.j)) { //爆空间的原因,这里还要再check一次,傻了
                                sgn[next.i][next.j] = 1;
                                next.step = cur.step + 1;
                                q.push(next);
                            }
                        }
                        if(k == 1 || k == 3) {
                            next.i = tempi - mv[k][0];
                            next.j = tempj - mv[k][1];
                            next.step = cur.step + 1;
                            q.push(next);
                        }
                    }
                } else if(mp[tempi][tempj] == '-') {
                    if(s == 1) {
                        if(k == 0 || k == 2) {
                            next.i = tempi + mv[k][0];
                            next.j = tempj + mv[k][1];
                            if(check(next.i,next.j)) { //爆空间的原因,这里还要再check一次,傻了
                                sgn[next.i][next.j] = 1;
                                next.step = cur.step + 1;
                                q.push(next);
                            }
                        }
                        if(k == 1 || k == 3) {
                            next.i = tempi - mv[k][0];
                            next.j = tempj - mv[k][1];
                            next.step = cur.step + 1;
                            q.push(next);
                        }
                    }
                    if(s == 0) {
                        if(k == 0 || k == 2) {
                            next.i = tempi - mv[k][0];
                            next.j = tempj - mv[k][1];
                            next.step = cur.step + 1;
                            q.push(next);
                        }
                        if(k == 1 || k == 3) {
                            next.i = tempi + mv[k][0];
                            next.j = tempj + mv[k][1];
                            if(check(next.i,next.j)) { //爆空间的原因,这里还要再check一次,傻了
                                sgn[next.i][next.j] = 1;
                                next.step = cur.step + 1;
                                q.push(next);
                            }
                        }
                    }
                } else {
                    next.i = tempi;
                    next.j = tempj;
                    sgn[next.i][next.j] = 1;
                    next.step = cur.step + 1;
                    q.push(next);
                }
            } //if
        } //for
    }
}
int main() {
    while(cin >> m >> n) {
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                cin >> mp[i][j];
                if(mp[i][j] == 'S') {
                    si = i;
                    sj = j;
                }
            }
        }
        cout << bfs() << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43555854/article/details/86667571