hdu 1240 Asteroids!
来源:互联网 发布:姚明职业生涯场均数据 编辑:程序博客网 时间:2024/06/06 03:39
http://acm.hdu.edu.cn/showproblem.php?pid=1240
Asteroids!
Problem Description
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
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.
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.
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 1O0 0 00 0 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
Sample Output
1 03 4NO ROUTE
写了长长的代码,最后却发现后台只有三组测试数据,就是题上给的三组数据,我也是醉了,不过还是练练BFS好一点
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <stack>#include <queue>using namespace std;#define N 106#define met(a, b) memset (a, b, sizeof (a))typedef long long LL;#define INF 0x3f3f3f3fconst int dir[6][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 0, -1}, {0, -1, 0}, {-1, 0, 0}};struct node{ int x, y, z, step;}sa, en;char g[N][N][N];int vis[N][N][N], n;int check (int x, int y, int z){ if (x>=0 && x<n && y>=0 && y<n && z>0 && z<n && !vis[x][y][z] && g[x][y][z]!='X') return 1; return 0;}int BFS (){ queue <node> que; que.push (sa); met (vis, 0); vis[sa.x][sa.y][sa.z] = 1; while (que.size()) { sa = que.front(); que.pop(); if (sa.x==en.x && sa.y==en.y && sa.z==en.z) return sa.step; for (int i=0; i<6; i++) { node q = sa; q.x += dir[i][0], q.y += dir[i][1], q.z += dir[i][2]; if (check (q.x, q.y, q.z)) { vis[q.x][q.y][q.z] = 1; q.step++; que.push (q); } } } return -1;}int main (){ char str1[10], str2[10]; while (scanf ("%s %d", str1, &n) != EOF) { met (g, 0); for (int i=0; i<n; i++) for (int j=0; j<n; j++) { getchar (); for (int k=0; k<n; k++) scanf ("%c", &g[i][j][k]); } scanf ("%d%d%d%d%d%d", &sa.z,&sa.y,&sa.x, &en.z,&en.y,&en.x); sa.step = en.step = 0; scanf ("%s", str2); int ans = BFS (); if (ans != -1) printf ("%d %d\n", n, ans); else puts ("NO ROUTE"); } return 0;}
0 0
- hdu 1240 Asteroids!
- HDU-1240-Asteroids!
- hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- Hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- HDU 1240 Asteroids!
- hdu 1240 Asteroids BFS
- HDU 1240 Asteroids!
- hdu 1240 Asteroids!
- hdu-1240-Asteroids!
- HDU 1240 Asteroids!
- hdu 1240 Asteroids!bfs
- Asteroids! HDU 1240
- HDU 1240 Asteroids!(BFS)
- hdu 1240 Asteroids!(BFS)
- HDU 1240 Asteroids!
- node stream api的应用,断电续传与秒传的例子
- jfinal 使用ehcache 存储登陆信息
- LeetCode Move Zeroes
- 17.IDA-基本块的定义
- Gradle version 2.2 is required. Current version is 2.10
- hdu 1240 Asteroids!
- 蓝桥杯 - 最大最小公倍数
- MySql中的事务
- 详解基于Binder的BroadcastReceiver && 基于Handler的LocalBroadcastManager
- 移动市场竞争安卓虎视眈眈苹果如何走出困顿之局
- Swing-树型组件
- spark transform系列__sortByKey
- 密码翻译
- 自定义配置文件,适配渠道更新