HDU1240Asteroids!三维广搜bfs

Asteroids!
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4200    Accepted Submission(s): 2718
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
South Central USA 2001
 
Recommend
zf   |   We have carefully selected several similar problems for you:  1242 1072 1372 1180 1238  
题意:先输入一行START N,“START”为字符串,表示开始,忽略即可。N是地图大小,为N*N*N。然后输入两个坐标(列k,行j,层i)的顺序要注意,表示起点和终点,从起点到终点计算至少要多少步

思路:类似于 hdu 1253:胜利大逃亡(基础广搜BFS)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
int a1,b1,c1,a2,b2,c2,n;
char map[15][15][15];
int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
struct nodes
{
    int i,j,k,m;
};
nodes node1,node2;
int bfs()
{
    queue<nodes> q;
    while(!q.empty())
    q.pop();
    node1.i=c1;
    node1.j=b1;
    node1.k=a1;
    node1.m=0;
    q.push(node1);
    while(!q.empty())
    {
        node1=q.front();
        q.pop();
        if(node1.i==c2&&node1.j==b2&&node1.k==a2)
        {
           // cout<<node1.m;
            return node1.m;
        }
        for(int i=0;i<6;i++)
        {
                node2.i=node1.i+dir[i][0];
                node2.j=node1.j+dir[i][1];
                node2.k=node1.k+dir[i][2];
                if(node2.i>=0&&node2.i<n&&node2.j>=0&&node2.j<n&&node2.k>=0&&node2.k<n&&map[node2.i][node2.j][node2.k]=='O')
                {
                    node2.m=node1.m+1;
                    map[node2.i][node2.j][node2.k]='X';
                    q.push(node2);
                }
        }
    }
    return -1;
}
int main()
{
    char str[10],en[10];
    while(~scanf("%s %d",str,&n))
    {

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                for(int k=0;k<n;k++)
                {
                    //scanf("%c",&map[i][j][k]);
                    cin>>map[i][j][k];
                }
            }
        }
        scanf("%d%d%d%d%d%d%d%d",&a1,&b1,&c1,&a2,&b2,&c2);
        scanf("%s",en);
        map[c1][b1][a1]='X';//根据题意c1--i,b1----j,c1----k
        //map[a1][b1][c1]='X';
        int ans=bfs();
        //cout<<bfs()<<endl;
        if(ans>=0)
        printf("%d %d\n",n,ans);
        else
        printf("NO ROUTE\n");

    }
}

 

posted @ 2015-08-22 15:48  Lincy*_*  阅读(865)  评论(0编辑  收藏  举报