1240 Asteroids!
来源:互联网 发布:模拟退火算法的伪代码 编辑:程序博客网 时间:2024/05/24 05:00
Asteroids!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3966 Accepted Submission(s): 2566
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,三维而已。然后这道题一直的错误在于没进if(p1.x==gx&&p1.y==gy&&p1.z==gz)很郁闷,幸亏娜姐一句没对应好。才反应x y z轴对应关系。
#include <iostream>#include<queue>#include<cstdio>using namespace std;const int INF=10000;int n;int sx,sy,sz;int gx,gy,gz;struct point{ int x,y,z; int t;};int total;int map[12][12][12];int count[12][12][12];point p1,p2;int go[6][3]={{0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0}};void bfs(int z,int y,int x){ p1.x=x; p1.y=y; p1.z=z; p1.t=0; count[z][y][x]=0; queue<point> que; que.push(p1); while(que.size()) { // cout<<"wrong"<<endl; p1=que.front(); que.pop(); if(p1.x==gx&&p1.y==gy&&p1.z==gz) { total=p1.t; break; } for(int i=0;i<6;i++) { int fx=p1.x+go[i][0]; int fy=p1.y+go[i][1]; int fz=p1.z+go[i][2]; if(fx>=0&&fx<n&&fy>=0&&fy<n&&fz>=0&&fz<n&&map[fz][fy][fx]!=INF) { if(count[fz][fy][fx]>p1.t+1) { count[fz][fy][fx]=p1.t+1; p2.z=fz; p2.y=fy; p2.x=fx; p2.t=count[fz][fy][fx]; que.push(p2); } } } }}int main(){ char ch,ch1[10],ch2[10]; while(scanf("%s%d",&ch1,&n)!=EOF) { for(int i=0;i<n;i++)//z for(int j=0;j<n;j++)//y for(int k=0;k<n;k++)//x { cin>>ch; if(ch=='X') map[i][j][k]=INF; if(ch=='O') map[i][j][k]=1; count[i][j][k]=INF; } cin>>sx>>sy>>sz; cin>>gx>>gy>>gz; cin>>ch2; /* for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { for(int k=0;k<n;k++) cout<<map[i][j][k]<<" "; cout<<endl; } cout<<endl; }*/ /* for(int i=0;i<n;i++) for(int j=0;j<n;j++) for(int k=0;k<n;k++) cout<<count[i][j][k];*/ total=0; bfs(sz,sy,sx); // cout<<total<<endl; //cout<<p1.t<<endl; if(count[gz][gy][gx]!=INF) cout<<n<<" "<<count[gz][gy][gx]<<endl; else cout<<"NO ROUTE"<<endl; }return 0;}
0 0
- 1240 Asteroids!
- 1240:Asteroids!
- hdu 1240 Asteroids!
- HDU-1240-Asteroids!
- hdu 1240 Asteroids!
- hdu 1240 Asteroids!
- hdoj 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
- Java输入输出流IO(1)
- [java]一次for循环实现查询有序数组中所有重复的元素
- windows查看端口占用
- DES SecretKeyFactory not available的处理
- ajaxfileupload 始终不执行success 只执行error方法;SyntaxError: expected expression, got '<
- 1240 Asteroids!
- Android -- 生命周期
- iOS/iPhone 程序文件目录结构以及启动流程
- 【栈、动态规划】Longest Valid Parentheses
- CMMI-基线
- 排序算法之堆排序
- 黑马程序员——OC核心语法点语法,成员变量,@property和@synthetic,构造方法
- 使用 ADF Skin
- 输入缓冲区的清除问题