hdu 1240广搜

简单的三维空间求最短路,用广搜就可以了。

/*
 * hdu1240/win.cpp
 * Created on: 2012-11-18
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
const int MAXN = 13;
char graph[MAXN][MAXN][MAXN];
bool visited[MAXN][MAXN][MAXN];
const int move[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
typedef struct MyPoint {
    int x, y, z, step;
    MyPoint(int xx, int yy, int zz, int ss = 0) {
        x = xx, y = yy, z = zz;
        step = ss;
    }
    MyPoint() {
        step = x = y = z = 0;
    }
};
inline bool operator ==(const MyPoint &mp1, const MyPoint &mp2) {
    return (mp1.x == mp2.x) && (mp1.y == mp2.y) && (mp1.z == mp2.z);
}
int N;

int work() {
    int a, b, c;
    queue<MyPoint> Q;
    memset(visited, false, sizeof(visited));
    scanf("%d%d%d", &c, &b, &a);
    Q.push(MyPoint(a, b, c));
    visited[a][b][c] = true;
    MyPoint end;
    scanf("%d%d%d END", &end.z, &end.y, &end.x);
    while(!Q.empty()) {
        MyPoint cur = Q.front();
        Q.pop();
        if(cur == end) {
            return cur.step;
        }
        for(int i = 0; i < 6; i++) {
            a = cur.x + move[i][0];
            b = cur.y + move[i][1];
            c = cur.z + move[i][2];
            if(a >= 0 && a < N && b >= 0 && b < N && c >= 0 && c < N) {
                if(graph[a][b][c] == 'O' && !visited[a][b][c]) {
                    Q.push(MyPoint(a, b, c, cur.step + 1));
                    visited[a][b][c] = true;
                }
            }
        }
    }
    return -1;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("data.in", "r", stdin);
#endif
    char str[100];
    while(scanf("%s%d", str, &N) == 2) {
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                for(int k = 0; k < N; k++) {
                    scanf(" %c", &graph[i][j][k]);
                }
            }
        }
        int ret = work();
        if(ret == -1) {
            puts("NO ROUTE");
        }else {
            printf("%d %d\n", N, ret);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/moonbay/p/2775751.html