hdu 1240 Asteroids!

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

      本题是简单的广搜没有任何技巧而言,只要对这个地图进行广搜即可

代码:

 

  
    
1 #include < stdio.h >
2 #include < queue >
3   using namespace std;
4 typedef struct node
5 {
6 int x,y,s;
7 int time;
8 }NODE;
9   int dir[ 6 ][ 3 ] = {{ - 1 , 0 , 0 },{ 1 , 0 , 0 },{ 0 , - 1 , 0 },{ 0 , 1 , 0 },{ 0 , 0 , - 1 },{ 0 , 0 , 1 }};
10   int main()
11 {
12 NODE cur,next; char map[ 15 ][ 15 ][ 15 ];
13 int i,j,k,n,sx,sy,ss,ex,ey,es,mark;
14 char str1[ 10 ],str2[ 5 ];
15 while (scanf( " %s%d " ,str1, & n) != EOF)
16 {
17 mark = 0 ;
18 for (i = 0 ;i < n;i ++ )
19 {
20 for (j = 0 ;j < n;j ++ )
21 {
22 getchar();
23 for (k = 0 ;k < n;k ++ )
24 scanf( " %c " , & map[i][j][k]);
25 }
26 }
27 scanf( " %d%d%d " , & sx, & sy, & ss);
28 scanf( " %d%d%d " , & ex, & ey, & es);
29 scanf( " %s " ,str2);
30 queue < NODE > qu;
31 cur.x = sx;
32 cur.y = sy;
33 cur.s = ss;
34 cur.time = 0 ;
35 qu.push(cur);
36 while ( ! qu.empty ())
37 {
38 cur = qu.front ();
39 qu.pop();
40 if (cur.x == ex && cur.y == ey && cur.s == es)
41 {
42 mark = 1 ;
43 break ;
44 }
45 for (i = 0 ;i < 6 ;i ++ )
46 {
47 next.x = cur.x + dir[i][ 0 ];
48 next.y = cur.y + dir[i][ 1 ];
49 next.s = cur.s + dir[i][ 2 ];
50 if (next.x >= 0 && next.x < n && next.y < n && next.y >= 0 && next.s >= 0 && next.s < n && map[next.s][next.x][next.y] != ' X ' )
51 {
52 map[next.s][next.x][next.y] = ' X ' ;
53 next.time = cur.time + 1 ;
54 qu.push(next);
55 }
56 }
57 }
58 if (mark == 0 )
59 printf( " NO ROUTE\n " );
60 else if (mark == 1 )
61 printf( " %d %d\n " ,n,cur.time);
62 }
63 return 0 ;
64 }
65
66
67
68
69  

 

 

你可能感兴趣的:(HDU)