Asteroids!
Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 5 components:
Start line - A single line, "START N", where 1 <= N <= 10.
Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:
'O' - (the letter "oh") Empty space
'X' - (upper-case) Asteroid present
Starting Position - A single line, "A B C", denoting the <A,B,C> coordinates of your craft's starting position. The coordinate values will be integers separated by individual spaces.
Target Position - A single line, "D E F", denoting the <D,E,F> coordinates of your target's position. The coordinate values will be integers separated by individual spaces.
End line - A single line, "END"
The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, inclusive.
The first coordinate in a set indicates the column. Left column = 0.
The second coordinate in a set indicates the row. Top row = 0.
The third coordinate in a set indicates the slice. First slice = 0.
Both the Starting Position and the Target Position will be in empty space.
Output
For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.
A single output set consists of a single line. If a route exists, the line will be in the format "X Y", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line will be "NO ROUTE" instead.
A move can only be in one of the six basic directions: up, down, left, right, forward, back. Phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.
Sample Input
START 1
O
0 0 0
0 0 0
END
START 3
XXX
XXX
XXX
OOO
OOO
OOO
XXX
XXX
XXX
0 0 1
2 2 1
END
START 5
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
0 0 0
4 4 4
END
Sample Output
1 0
3 4
NO ROUTE
Source
题目类型:三维BFS
算法分析:一道三维的BFS题目,直接将坐标和状态改为三维,然后使用BFS求解最小步数即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
#include <iostream> #include <fstream> #include <sstream> #include <algorithm> #include <iomanip> #include <cstring> #include <cstdio> #include <cmath> #include <map> #include <string> #include <vector> #include <stack> #include <queue> #include <set> #include <list> #include <ctime> using namespace std; const int INF = 0x7FFFFFFF; const int maxn = 36 + 66; const double EPS = 1e-6; const int dx[] = {-1, 1, 0, 0, 0, 0}; const int dy[] = {0, 0, -1, 1, 0, 0}; const int dz[] = {0, 0, 0, 0, -1, 1}; struct Node { int x, y, z, s; Node (int xx, int yy, int zz, int ss) : x (xx), y (yy), z (zz), s (ss) {} Node () {}; }; char ans[maxn][maxn][maxn]; int minval; bool visited[maxn][maxn][maxn]; int s_x, s_y, s_z, e_x, e_y, e_z; char s[66]; int n; inline bool OkMove (int x, int y, int z) { if (x >= 0 && x <= n - 1 && y >= 0 && y <= n - 1 && z >= 0 && z <= n - 1 && !visited[z][x][y] && ans[z][x][y] == 'O') return true; return false; } void bfs (int x, int y, int z, int s) { minval = INF; memset (visited, false, sizeof (visited)); queue <Node> qu; qu.push (Node (x, y, z, s)); visited[z][x][y] = true; while (!qu.empty ()) { Node temp = qu.front (); qu.pop (); if (temp.x == e_x && temp.y == e_y && temp.z == e_z) { minval = temp.s; return ; } int i; for (i = 0; i < 6; i++) { int val_x = temp.x + dx[i], val_y = temp.y + dy[i], val_z = temp.z + dz[i]; if (OkMove (val_x, val_y, val_z)) { visited[val_z][val_x][val_y] = true; qu.push (Node (val_x, val_y, val_z, temp.s + 1)); } } } } int main() { // ifstream cin ("aaa.txt"); while (cin >> s >> n) { int i, j, k; for (k = 0; k < n; k++) { for (i = 0; i < n; i++) for (j = 0; j < n; j++) cin >> ans[k][i][j]; } cin >> s_x >> s_y >> s_z >> e_x >> e_y >> e_z; cin >> s; bfs (s_x, s_y, s_z, 0); if (minval == INF) cout << "NO ROUTE" << endl; else cout << n << " " << minval << endl; } return 0; } |
- « 上一篇:hdu1238
- hdu1251:下一篇 »