hdu 1240 3维迷宫 求起点到终点的步数

题意,给出一个N,这是这个三空间的大小,然后给出所有面的状况O为空地,X为墙,再给出起始点的三维坐标和终点的坐标,输出到达的步数

比较坑 z是x,x是y,y是z,
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

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std;
 6
 7 char map[20][20][20];
 8 int v[20][20][20];
 9 int n;
10 int sx,sy,sz;
11 int ex,ey,ez;
12 int dx[] = {1,-1,0,0,0,0};
13 int dy[] = {0,0,1,-1,0,0};
14 int dz[] = {0,0,0,0,1,-1};
15 int ans ;
16
17 struct node
18 {
19     int x , y ,z ,step ;
20 };
21
22 int bfs()
23 {
24     node now , t ;
25     int fx , fy , fz ;
26     queue<node> q ;
27     now.x = sx ;
28     now.y = sy ;
29     now.z = sz ;
30     now.step = 0 ;
31     memset(v,0,sizeof(v)) ;
32     q.push(now) ;
33     v[sz][sx][sy] = 1 ;
34     while(!q.empty())
35     {
36         now = q.front() ;
37         q.pop() ;
38         if (now.x == ex && now.y == ey && now.z == ez)
39         {
40             ans  = now.step ;
41             return  1 ;
42         }
43         for (int i = 0 ; i < 6 ; i++)
44        {
45         fx = now.x + dx[i] ;
46         fy = now.y + dy[i] ;
47         fz = now.z + dz[i] ;
48         if (fx<0 || fy<0 || fz<0 || fx>=n || fy>=n || fz>=n || v[fz][fx][fy] == 1 || map[fz][fx][fy] == ‘X‘)
49             continue ;
50
51         t.x = fx ;
52         t.y = fy ;
53         t.z = fz ;
54         t.step = now.step + 1 ;
55         v[fz][fx][fy] = 1 ;
56         q.push(t) ;
57         }
58     }
59
60     return 0 ;
61 }
62
63 int main ()
64 {
65     //freopen("in.txt","r",stdin) ;
66     char s[10] ;
67     while (scanf("%s %d" , s , &n) !=EOF)
68     {
69         int i , j , k ;
70         for (i = 0 ; i < n ; i ++)
71             for (j = 0 ; j < n ; j ++)
72                   {
73                       scanf("%s" , map[i][j]) ;
74                   }
75         scanf("%d %d %d" , &sx,&sy,&sz) ;
76         scanf("%d %d %d" , &ex,&ey,&ez) ;
77         scanf("%s" , s) ;
78
79         ans = 0 ;
80         if (bfs())
81             printf("%d %d\n" , n , ans) ;
82         else
83             printf("NO ROUTE\n") ;
84     }
85
86     return 0 ;
87 }

时间: 2024-10-07 06:33:50

hdu 1240 3维迷宫 求起点到终点的步数的相关文章

hdu1598find the most comfortable road(并查集+枚举,求起点到终点的边中最大边减最小边差值最小)

Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超级空中漫游结构)进行交流,每条SARS都对行驶在上面的Flycar限制了固定的Speed,同时XX星人对 Flycar的"舒适度"有特殊要求,即乘坐过程中最高速度与最低速度的差越小乘坐越舒服 ,(理解为SARS的限速要求,flycar必须瞬间提速/降速,痛苦呀 ), 但XX星人对时间却没那么多要求.要你找出一条城市间的最舒适的路径.(

Key Vertex (hdu 3313 SPFA+DFS 求起点到终点路径上的割点)

Key Vertex Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1347    Accepted Submission(s): 305 Problem Description You need walking from vertex S to vertex T in a graph. If you remove one vert

hdu 1253 3维迷宫 在规定时间内能否出迷宫 (3维BFS)

题意:有一个人要在魔王回来之前逃出城堡.1表示墙,0表示路.魔王将在T分钟后回到城堡 起点可以是墙,但是人能走出.而终点也可以是墙,那自然就走不出了,但是要判断. 剪枝:如果终点是门或者从起点到终点的最短时间都大于t ,直接输出 -1. Sample Input13 3 4 20 //a b c T0 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0 Sample Output11 1 # include <cstdio>

HDU 1240 (简单三维广搜) Asteroids!

给出一个三维的迷宫以及起点和终点,求能否到大终点,若果能输出最短步数 三维的问题无非就是变成了6个搜索方向 最后强调一下xyz的顺序,从输入数据来看,读入的顺序是map[z][x][y] 总之,这是很基础的一道题 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <queue> 6 #include <algorithm

(hdu step 4.2.4)A strange lift(求从起点到终点的最小步数,限制条件是:在一维的情况下)

题目: A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 709 Accepted Submission(s): 348   Problem Description There is a strange lift.The lift can stop can at every floor as you want, a

(hdu step 3.2.1)Max Sum(简单dp:求最大子序列和、起点、终点)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1390 Accepted Submission(s): 542   Problem Descrip

hdu 2262 高斯消元求期望

Where is the canteen Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1070    Accepted Submission(s): 298 Problem Description After a long drastic struggle with himself, LL decide to go for some

hdu 4418 高斯消元求期望

Time travel Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1480    Accepted Submission(s): 327 Problem Description Agent K is one of the greatest agents in a secret organization called Men in B

HDU 1240 Asteroids! (三维BFS)

Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4749    Accepted Submission(s): 3048 Problem Description You're in space. You want to get home. There are asteroids. You don't want to