题解:HDU 1240: Asteroids! (DFS 或 BFS)
来源:互联网 发布:软件需求分为哪几类 编辑:程序博客网 时间:2024/06/13 07:34
题目
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.
分析
一道最短路径问题应该是标准的三维BFS模板题
这里我们用DFS来做
在全局变量中定义 minn
然后将DFS求出来的步数与minn比较 最后的minn就是最小步数
上代码 + 注释
#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <climits>const int MAX = 11;const int INF = INT_MAX - 3;const int dirx[6] = { 1, -1, 0, 0, 0, 0 }, diry[6] = { 0, 0, 1, -1, 0, 0 }, dirz[6] = { 0, 0, 0, 0, 1, -1 };// x, y, z //三个方向char map[MAX][MAX][MAX];int dist[MAX][MAX][MAX];int sx, sy, sz, dx, dy, dz;int minx, n;void dfs(int x, int y, int z, int cnt){ if (x<0 || y<0 || z<0 || x >= n || y >= n || z >= n) return; if (map[z][x][y] == 'X') return; if (x == dx && y == dy && z == dz) { if (cnt<minx)minx = cnt; return; } if (dist[z][x][y] <= cnt) return; else dist[z][x][y] = cnt; int i, tx, ty, tz; for (i = 0; i<6; ++i) { tx = x + dirx[i]; ty = y + diry[i]; tz = z + dirz[i]; dfs(tx, ty, tz, cnt + 1);//标准模板 }}int main(){ char str[10]; int i, j, k, cnt; while (scanf("%s %d%*c", str, &n) != EOF) { for (k = 0; k < n; ++k) { for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) { map[k][i][j] = getchar(); dist[k][i][j] = INF; } getchar(); } } scanf("%d %d %d", &sy, &sx, &sz); scanf("%d %d %d", &dy, &dx, &dz); scanf("%s", str); cnt = 0; minx = INT_MAX; dfs(sx, sy, sz, cnt); if (minx == INT_MAX) { printf("NO ROUTE\n"); } else { printf("%d %d\n", n, minx); } } return 0;}
- 题解:HDU 1240: Asteroids! (DFS 或 BFS)
- hdu 1240 Asteroids BFS
- hdu 1240 Asteroids!bfs
- HDU 1240 Asteroids!(BFS)
- hdu 1240 Asteroids!(BFS)
- HDU 1240 Asteroids! 【BFS】
- hdu 1240 Asteroids! (bfs)
- hdu 1240 Asteroids!【BFS】
- HDU 1240 Asteroids!(BFS)
- HDU 1240 Asteroids!【BFS】
- hdu 1240 Asteroids!(BFS)
- hdu 1240 Asteroids! (三维bfs)
- HDU-#1240 Asteroids!(BFS)
- HDU 1240 Asteroids! 三维空间BFS
- hdoj-1240-Asteroids!(dfs和bfs)
- hdu1240 Asteroids!--DFS & BFS
- BFS--- HDU 1240 Asteroids! 广度优先搜索
- POJ 2225 && HDU 1240 Asteroids!(bfs)
- HDU 4550 卡片游戏(贪心+细心)
- poj1741 Tree 点分治
- unlink()
- 全功能智能车之CCD初步实现并发送数据到上位机(第十二篇)
- edX的一些管理命令
- 题解:HDU 1240: Asteroids! (DFS 或 BFS)
- POJ 2031 (最小生成树)
- Video对象的2个状态属性readyState和networkState属性的介绍
- android---异步通讯AsyncTask
- 搭建Open edX经验总结
- TextView限制最大行数,多出部分显示省略号,解决ellipsize = "end" 失效的问题
- nyoj 127 星际之门(一)【最小生成树】
- MATLAB 机器人逆解 ikine
- if语句优化