CDZSC_2015寒假新人(4)——搜索 - D

本文介绍了一个基于宽度优先搜索(BFS)算法解决太空迷航问题的实例。在一个由空格('O')和障碍物('X')组成的三维矩阵中,利用BFS算法寻找从起点到终点的最短路径。详细解析了算法实现过程,包括数据结构设计、状态转移逻辑和边界条件判断。

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

  

 题解:BFS。题目很好理解。详见代码。
 
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 using namespace std;
 5 
 6 const int MAX = 16;
 7 const int x[] = { -1, 0, 1, 0, 0, 0 };
 8 const int y[] = { 0, 1, 0, -1, 0, 0 };
 9 const int z[] = { 0, 0, 0, 0, -1, 1 };
10 char arr[MAX][MAX][MAX];
11 bool vis[MAX][MAX][MAX];
12 
13 struct POINT{
14     int x, y, z, s;
15 };
16 
17 int BFS(POINT s, POINT e, int n)
18 {
19     POINT nx, t;
20     memset(vis, 0, sizeof(vis)); vis[s.z][s.y][s.x] = true;
21     queue<POINT> qu;
22     qu.push(s);
23     while(!qu.empty())
24     {
25         nx = qu.front(); qu.pop();
26         if(nx.x == e.x && nx.y == e.y && nx.z == e.z)
27         {
28             return nx.s;
29         }
30         for(int i = 0; i < 6; i++)
31         {
32             t.x = nx.x + x[i]; t.y = nx.y + y[i]; t.z = nx.z + z[i];
33             if((t.x >= 0 && t.x < n) && (t.y >= 0 && t.y < n) && (t.z >= 0 && t.z < n) && !vis[t.z][t.y][t.x] && arr[t.z][t.y][t.x] == 'O')
34             {
35                 vis[t.z][t.y][t.x] = true;
36                 t.s = nx.s + 1;
37                 qu.push(t);
38             }
39         }
40     }
41     return -1;
42 }
43 
44 int main()
45 {
46 #ifdef CDZSC_OFFLINE
47     freopen("in.txt", "r", stdin);
48     freopen("out.txt", "w", stdout);
49 #endif
50     int n, ans;
51     char str[32];
52     POINT s, t;
53     while(~scanf("%s", str) && str[0] == 'S')
54     {
55         scanf("%d", &n);
56         memset(arr, 0, sizeof(arr));
57         for(int i = 0; i < n; i++)
58         {
59             for(int j = 0; j < n; j++)
60             {
61                 scanf("%s", arr[i][j]);
62             }
63         }
64         scanf("%d%d%d%d%d%d", &s.x, &s.y, &s.z, &t.x, &t.y, &t.z); s.s = 0;
65         ans = BFS(s, t, n);
66         if(ans == -1)
67         {
68             printf("NO ROUTE\n");
69         }
70         else
71         {
72             printf("%d %d\n", n, ans);
73         }
74         scanf("%s", str);
75     }
76     return 0;
77 }
View Code

 

 

 

转载于:https://www.cnblogs.com/LiuACG/p/4253939.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值