POJ

http://poj.org/problem?id=2251

题意:给你一个三维的地图,找一条最短路

题解:因为是最短路,所以宽搜。三维就用六个单位向量。

坑:

第一次打bfs各种打错:

首先是字符数组处理: 一行一行读可以用cin,但这样就不能map[i][j]+1读入。(for i=1 or i=0;)

数组初始定义,int定义成char了,memset也错了

忘记pop,

return now not v;

l,r,c,x,y,z,对应关系弄混

测试的时候改了一行忘记改回来,

于是一直卡样例Orz


ac代码:

#define    _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int maxn = 1e5 + 5;
char map[35][35][35];
int step[35][35][35];//
bool vis[35][35][35];
int l, r, c;
int dir[6][3] = { 1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1 };
struct point {
    int x, y, z;
    point(int x=0, int y=0, int z=0) :x(x), y(y),z(z) {}
}s,now,v;
//bool check(point q) {
//    if (!vis[q.z][q.x][q.y] && q.z >= 0 && q.z < l&&q.x >= 0 && q.x < r&&q.y >= 0 && q.y < c&&map[q.z][q.x][q.y] != '#')
//        return 1;
//    return 0;
//}
int check(int x, int y, int z)
{
    if (!vis[z][x][y] && z >= 0 && z < l&&x >= 0 && x < r&&y >= 0 && y < c&&map[z][x][y] != '#')//
        return 1;
    return 0;
}

int bfs() {
    memset(vis, 0, sizeof(vis));
    memset(step, 0, sizeof(step));//
    
    vis[s.z][s.x][s.y] = 1;
    step[s.z][s.x][s.y] = 0;    
    //step[s.]
    queue<point> Q;
    Q.push(s);
    while (!Q.empty()) {
        now =    Q.front();
        Q.pop();//
        for (int i = 0; i < 6; i++) {
            int dz = now.z + dir[i][0];
            int dx = now.x + dir    [i][1];
            int dy = now.y + dir[i][2];
            
            v = point( dx, dy,dz);//
            if (check(v.x,v.y,v.z)) {
                if (map[v.z][v.x][v.y] == 'E')return step[now.z][now.x][now.y] + 1;//

                Q.push(v);
                vis[v.z][v.x][v.y] = 1;
                step[v.z][v.x][v.y] = step[now.z][now.x][now.y] + 1;
            }
        }
    }
    
    return -1;
};
int main() {
    
        while (cin >> l >> r >> c) {
        if (!l && !r && !c) break;
        for (int i = 0; i <l; i++)
            for (int j = 0; j < r; j++)scanf("%s", map[i][j]);//cin >> map[i][j];
        
        for (int i = 0; i < l; i++)
            for (int j =0; j < r; j++)
                for (int k = 0; k < c; k++) {
                    if (map[i][j][k] == 'S') {//
                        s=point(j,k,i);    
                    }
                }
        //cout << s.y << endl;
        int ans = bfs();
        ans == -1 ? printf("Trapped!
"): printf("Escaped in %d minute(s).
", ans);
    }

}
成功的路并不拥挤,因为大部分人都在颓(笑)
原文地址:https://www.cnblogs.com/SuuT/p/8537671.html