HDU 1240 Asteroids! 题解

Asteroids!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6174    Accepted Submission(s): 3876

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
 


Recommend
zf   |   We have carefully selected several similar problems for you:  1072 1241 1016 1010 1175 
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
这道题的题目是真的长( ̄▽ ̄)"
还是英文
(小声BB)
但是这道题目看懂了过后发现就是
非常基础的三维bfs
和胜利大逃亡类似
几乎没有难度
于是就直接上手写≡(▔﹏▔)≡
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 struct node
33 {
34     int x,y,z,step;
35 };
36 char str[11];
37 int n,vis[101][101][101];
38 char map[101][101][101];
39 int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};//三维,六个自由度,三个方向
40 int bfs(int a,int b,int c,int x,int y,int z)
41 {
42     int i;
43     queue<node> q;
44     q.push(node{a,b,c,0});
45     vis[a][b][c]=1;
46     while(!q.empty())
47     {
48         node t=q.front();
49         q.pop();
50         if(t.x==x&&t.y==y&&t.z==z)
51             return t.step;
52         for(i=0;i<6;i++)
53         {
54             int dx=t.x+dir[i][0];
55             int dy=t.y+dir[i][1];
56             int dz=t.z+dir[i][2];
57             if(dx>=0&&dy>=0&&dz>=0&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs
58             {
59                 vis[dx][dy][dz]=1;
60                 q.push(node{dx,dy,dz,t.step+1});
61             }
62         }
63     }
64     return -1;
65 }
66 int main()
67 {
68     int a,b,c,x,y,z;
69     while(scanf("%s%d",str,&n)!=EOF)//吸收start
70     {
71         getchar();
72         memset(vis,0,sizeof(vis));
73         for(int i=0;i<n;i++)
74         {
75             for(int j=0;j<n;j++)
76             {
77                 scanf("%s",map[i][j]);//输入map
78             }
79         }
80         scanf("%d%d%d%d%d%d",&a,&b,&c,&x,&y,&z);//输入起点和终点
81         scanf("%s",str);//吸收掉end
82         int ans=bfs(a,b,c,x,y,z); 
83         if(ans==-1)
84             printf("NO ROUTE\n");
85         else
86             printf("%d %d\n",n,ans);
87     }
88     return 0;
89 }

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

然后恭喜你

第二组样例

都过不了⊙﹏⊙∥

原因很简单

题意说的n是按照层数排列的

而这里的层数是z

而输入的时候x作为深度

显然就与题意不符了

这里有两种改法

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第一种:

 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 struct node
33 {
34     int x,y,z,step;
35 };
36 char str[11];
37 int n,vis[101][101][101];
38 char map[101][101][101];
39 int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};//六个自由度,三个方向 
40 int bfs(int a,int b,int c,int x,int y,int z)
41 {
42     int i;
43     queue<node> q;
44     q.push(node{a,b,c,0});
45     vis[a][b][c]=1;
46     while(!q.empty())
47     {
48         node t=q.front();
49         q.pop();
50         if(t.x==x&&t.y==y&&t.z==z)
51             return t.step;
52         for(i=0;i<6;i++)
53         {
54             int dx=t.x+dir[i][0];
55             int dy=t.y+dir[i][1];
56             int dz=t.z+dir[i][2];
57             if(dx>=0&&dy>=0&&dz>=0&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs 
58             {
59                 vis[dx][dy][dz]=1;
60                 q.push(node{dx,dy,dz,t.step+1});
61             }
62         }
63     }
64     return -1;
65 }
66 int main()
67 {
68     int a,b,c,x,y,z;
69     while(scanf("%s%d",str,&n)!=EOF)//吸收掉start 
70     {
71         getchar();
72         memset(vis,0,sizeof(vis));
73         for(int i=0;i<n;i++)
74         {
75             for(int j=0;j<n;j++)
76             {
77                 scanf("%s",map[i][j]);//输入map 
78             }
79         }
80         scanf("%d%d%d%d%d%d",&c,&b,&a,&z,&y,&x);//注意输入的方向是反过来的,层数是z,输入的时候做了一个纠正 
81         scanf("%s",str);//同理,吸收掉end 
82         int ans=bfs(a,b,c,x,y,z); 
83         if(ans==-1)
84             printf("NO ROUTE\n");
85         else
86             printf("%d %d\n",n,ans);
87     }
88     return 0;
89 }

这里在输入起点与终点方面交换了一下位置

效果拔群

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第二种:

 

 1 //Author:LanceYu
 2 #include<iostream>
 3 #include<string>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<fstream>
 7 #include<iosfwd>
 8 #include<sstream>
 9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 struct node
33 {
34     int x,y,z,step;
35 };
36 char str[11];
37 int n,vis[101][101][101];
38 char map[101][101][101];
39 int dir[6][3]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1}};//六个自由度,三个方向 
40 int bfs(int a,int b,int c,int x,int y,int z)
41 {
42     int i;
43     queue<node> q;
44     q.push(node{a,b,c,0});
45     vis[a][b][c]=1;
46     while(!q.empty())
47     {
48         node t=q.front();
49         q.pop();
50         if(t.x==x&&t.y==y&&t.z==z)
51             return t.step;
52         for(i=0;i<6;i++)
53         {
54             int dx=t.x+dir[i][0];
55             int dy=t.y+dir[i][1];
56             int dz=t.z+dir[i][2];
57             if(dx>=0&&dy>=0&&dz>=0&&dx<n&&dy<n&&dz<n&&!vis[dx][dy][dz]&&map[dx][dy][dz]=='O')//三维bfs 
58             {
59                 vis[dx][dy][dz]=1;
60                 q.push(node{dx,dy,dz,t.step+1});
61             }
62         }
63     }
64     return -1;
65 }
66 int main()
67 {
68     int a,b,c,x,y,z;
69     while(scanf("%s%d",str,&n)!=EOF)//吸收掉start 
70     {
71         getchar();
72         memset(vis,0,sizeof(vis));
73         for(int i=0;i<n;i++)
74         {
75             for(int j=0;j<n;j++)
76             {
77                 for(int k=0;k<n;k++) 
78                     cin>>map[k][j][i];//输入map的时候直接按照需求输入 
79             }
80         }
81         scanf("%d%d%d%d%d%d",&a,&b,&c,&x,&y,&z);//输入起点和终点 
82         scanf("%s",str);//同理,吸收掉end 
83         int ans=bfs(a,b,c,x,y,z); 
84         if(ans==-1)
85             printf("NO ROUTE\n");
86         else
87             printf("%d %d\n",n,ans);
88     }
89     return 0;
90 }

 

 

输入时按照需求输入

效果同样拔群

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Notes:看清题目,看清题目,看清题目(重要的事情说三遍)

2018-11-16  03:24:04  Author:LanceYu

原文地址:https://www.cnblogs.com/lanceyu/p/9967127.html