hdu 1240 Asteroids! (bfs)

Asteroids!

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


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.

 

 

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
 

 

Source
 
水题
裸bfs
  1 /*************************************************************************
  2     > File Name: code/hdu/1240.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年10月05日 星期一 18时44分48秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21                  
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 using namespace std;
 26 const int dx6[6]={1,0,0,-1,0,0};
 27 const int dy6[6]={0,-1,1,0,0,0};
 28 const int dz6[6]={0,0,0,0,-1,1};
 29 typedef long long LL;
 30 typedef double DB;
 31 const int inf = 0x3f3f3f3f;
 32 const int N=11;
 33 char sta[30],en[30];
 34 char maze[N][N][N];
 35 bool vis[N][N][N];
 36 int n ;
 37 struct node
 38 {
 39     int x,y,z;
 40     int d;
 41 
 42     bool ok()
 43     {
 44     if (x>=0&&y>=0&&z>=0&&x<n&&y<n&&z<n&&!vis[z][x][y]&&maze[z][x][y]!='X')
 45         return true;
 46     return false;
 47     }
 48     
 49 
 50 }s,tar;
 51 
 52 int ans;
 53 bool bfs()
 54 {
 55     vis[s.z][s.x][s.y] = true;
 56     s.d = 0 ;
 57     queue<node>q;
 58     q.push(s);
 59     while (!q.empty())
 60     {
 61     node pre=q.front();q.pop();
 62 //    cout<<"x:"<<pre.x<<" y:"<<pre.y<<" z:"<<pre.z<<" d:"<<pre.d<<endl;
 63 
 64     if (pre.x==tar.x&&pre.y==tar.y&&pre.z==tar.z)
 65     {
 66         ans = pre.d;
 67         return true;
 68     }
 69 
 70     for ( int i = 0 ; i < 6 ; i++)
 71     {
 72         node next;
 73         next.x = pre.x + dx6[i];
 74         next.y = pre.y + dy6[i];
 75         next.z = pre.z + dz6[i];
 76         next.d = pre.d + 1;
 77         if (next.ok())
 78         {
 79         vis[next.z][next.x][next.y] = true;
 80         q.push(next);
 81         }
 82 
 83     }
 84     }
 85     
 86     return false;
 87 }
 88 int main()
 89 {
 90   #ifndef  ONLINE_JUDGE 
 91    freopen("in.txt","r",stdin);
 92   #endif
 93    while(scanf("%s",sta)!=EOF)
 94    {
 95        ms(vis,false);
 96        scanf("%d",&n);
 97        for ( int k = 0 ; k < n ; k++)
 98        for ( int  i = 0 ; i < n ; i++)
 99         scanf("%s",maze[k][i]);
100        scanf("%d %d %d",&s.x,&s.y,&s.z);
101        scanf("%d %d %d",&tar.x,&tar.y,&tar.z);
102        scanf("%s",en);
103        if (bfs())
104        {
105        printf("%d %d\n",n,ans);
106        }
107        else
108        {
109        puts("NO ROUTE");
110        }
111      
112    }
113   
114    
115  #ifndef ONLINE_JUDGE  
116   fclose(stdin);
117   #endif
118     return 0;
119 }
View Code

 

 
posted @ 2015-10-05 19:10  111qqz  阅读(177)  评论(0编辑  收藏  举报