題目連結:HDU 1240 【Asteroids!】
思路
程式碼
#include <iostream>
#include <queue>
#include <stdlib.h>
#include <cstring>
#define ll long long
using namespace std;
const int N = 20;
const int M = 1e4;
struct point {
int x, y, z, step;
}position[M],start, target;
int mp[N][N][N], n, dir[10][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0},
{0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
bool vis[N][N][N];
bool check(int x, int y, int z) {
return x >= 0 && x < n && y >= 0 && y < n && z >= 0 && z < n && vis[z][x][y] == 0 && mp[z][x][y] == 0;
}
void bfs() {
queue<point> q;
q.push(start);
vis[start.x][start.y][start.z] = true;
while (!q.empty()) {
start = q.front();
q.pop();
if (start.x == target.x && start.y == target.y && start.z == target.z) {
cout << n << " " << start.step << endl;
return;
}
for (int i = 0; i < 6; i++) {
point now = start;
now.x += dir[i][0], now.y += dir[i][1], now.z += dir[i][2];
if (check(now.x, now.y, now.z) == true) {
vis[now.z][now.x][now.y] = true;
now.step++;
q.push(now);
}
}
}
cout << "NO ROUTE" << endl;
}
int main() {
string s;
while (cin >> s >> n) {
memset(vis, 0, sizeof vis);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
char auxious;
cin >> auxious;
if (auxious == 'O') {
mp[i][j][k] = 0;
} else {
mp[i][j][k] = 1;
}
}
}
}
cin >> start.x >> start.y >> start.z;
cin >> target.x >> target.y >> target.z;
cin >> s;
start.step = target.step = 0;
bfs();
}
return 0;
}