hdoj 1240 Asteroids!

来源:互联网 发布:九九乘法表用c语言 编辑:程序博客网 时间:2024/05/14 05:52

题目大意:求3维空间的起点到终点的最短距离,中间有障碍X的点不能走。

解题思路:水题。。。。。宽度优先遍历

#include <iostream>#include <cstdio>#include <cstring>#include <queue>using namespace std;struct node{int x, y, z, step;};const int maxn = 11;char space[maxn][maxn][maxn];int sx, sy, sz, ex, ey, ez, n;int dir[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};int bfs();int main(){char str[10];while(scanf("%s %d", str, &n) != EOF){for(int i = 0; i < n; i++){for(int j = 0; j < n; j++)scanf("%s", space[i][j]);}scanf("%d %d %d %d %d %d", &sz, &sy, &sx, &ez, &ey, &ex);scanf("%s", str);int ans = bfs();if(ans == -1)printf("NO ROUTE\n");elseprintf("%d %d\n", n, ans);}return 0;}int bfs(){node t;queue<node> que;node s;s.x = sx; s.y = sy; s.z = sz; s.step = 0;space[s.x][s.y][s.z] = '*';que.push(s);while(!que.empty()){node tmp = que.front();que.pop();if(tmp.x == ex && tmp.y == ey && tmp.z == ez)return tmp.step;for(int i = 0; i < 6; i++){t.x = tmp.x + dir[i][0]; t.y = tmp.y + dir[i][1]; t.z = tmp.z + dir[i][2];t.step = tmp.step + 1;if(t.x >= 0 && t.x < n && t.y >= 0 && t.y < n && t.z >= 0 && t.z < n && space[t.x][t.y][t.z] == 'O'){space[t.x][t.y][t.z] = '*';que.push(t);}}}return -1;}


 

原创粉丝点击