Asteroids!(三维BFS)

来源:互联网 发布:移动云商城 源码下载 编辑:程序博客网 时间:2024/05/01 18:30

LINK:http://acm.hdu.edu.cn/showproblem.php?pid=1240


Problem:

Asteroids!

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


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 1O0 0 00 0 0ENDSTART 3XXXXXXXXXOOOOOOOOOXXXXXXXXX0 0 12 2 1ENDSTART 5OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOXXXXXXXXXXXXXXXXXXXXXXXXXOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO0 0 04 4 4END
 

Sample Output
1 03 4NO ROUTE
 

Source
South Central USA 2001

hdu oj AC  code(不知为何在hdu1240能AC,在poj2225上却是wa,求大神指点一下,感激不尽可怜


#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
char map[11][11][11];
int vis[11][11][11],n;
int sx,sy,sz,ex,ey,ez;
int d[6][3]={{0,0,1},{0,1,0},{1,0,0},{0,0,-1},{0,-1,0},{-1,0,0}};
struct node
{
int x;
int y;
int z;
int sp;
};
queue<node>Q;
int bfs()
{
int i,xi,yi,zi;
node now,next;
now.x=sx;
now.y=sy;
now.z=sz;
now.sp=0;
Q.push(now);
vis[sx][sy][sz]=1;
while(!Q.empty())
{
now=Q.front();
if(now.x==ex&&now.y==ey&&now.z==ez)
return now.sp;
Q.pop();
for(i=0;i<6;i++)
{
xi=now.x+d[i][0];
yi=now.y+d[i][1];
zi=now.z+d[i][2];
if(xi>=0&&xi<n&&yi>=0&&yi<n&&zi>=0&&zi<n&&map[xi][yi][zi]=='O'&&!vis[xi][yi][zi])
{
vis[xi][yi][zi]=1;
next.sp=now.sp+1;
next.x=xi;
next.y=yi;
next.z=zi;
Q.push(next);
}
}
}
return -1;
}
int main()
{
char s[20];
int i,j,k,fg;
while(scanf("%s %d",s,&n)!=EOF)
{
for(k=0;k<n;k++)
for(i=0;i<n;i++)
{
getchar();
for(j=0;j<n;j++)
scanf("%c",&map[i][j][k]); 
}
scanf("%d%d%d",&sx,&sy,&sz);
scanf("%d%d%d",&ex,&ey,&ez);
scanf("%s",s);
memset(vis,0,sizeof(vis));
while(!Q.empty())
Q.pop();
fg=bfs();
if(fg==-1)
printf("NO ROUTE\n");
else
printf("%d %d\n",n,fg);
}
return 0;
}



附上网上poj AC代码,求大牛们解释下上面我的代码与这代码的区别,我不懂为什么这样能过,上面代码却在poj过不了。。。


#include<stdio.h>
#include<queue>
#include<limits.h>
using namespace std;
typedef struct n
{
    int x,y,z,times;
}Node;

char mass[11][11][11],c1[11];
int flag[11][11][11],N,SC,SH,SL,EH,EL,EC,tot,dic[6][3]={{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};
void BFS()
{
    Node n1,n2;
    queue<Node>qu;
    int a,b,c,i,j,k;
    n1.x=SH,n1.y=SL,n1.z=SC,n1.times=0;
    flag[SC][SH][SL]=1;
    qu.push(n1);
    memset(flag,0,sizeof(flag));
    while(!qu.empty())
    {
        n1=qu.front();
        for(i=0;i<6;++i)
        {
            n2.x=n1.x+dic[i][0];n2.y=n1.y+dic[i][1];n2.z=n1.z+dic[i][2];n2.times=n1.times+1;
            if(flag[n2.z][n2.x][n2.y]||n2.x<0||n2.y<0||n2.z<0||n2.x>=N||n2.y>=N||n2.z>=N)//越界了或者是访问过了
            {
                continue;
            }
            if(n2.x==EH&&n2.y==EL&&n2.z==EC)
            {
                printf("%d %d\n",N,n2.times);
                return;
            }
            if(mass[n2.z][n2.x][n2.y]=='X')
                continue;
            flag[n2.z][n2.x][n2.y]=1;
            qu.push(n2);
        }
        qu.pop();
    }
    printf("NO ROUTE\n");
}
int main()
{
    int a,b,c,i,j,k;
    while(scanf("%s%d",c1,&N)!=EOF)
    {
        tot=INT_MAX;
        for(i=0;i<N;++i)
        {
            for(j=0;j<N;++j)
                scanf("%s",mass[i][j]);
        }
        scanf("%d%d%d%d%d%d",&SL,&SH,&SC,&EL,&EH,&EC);
        scanf("%s",c1);
        if(SL==EL&&SH==EH&&SC==EC)
        {
            printf("%d %d\n",N,0);
                continue;
        }
        BFS();
    }

}



0 0
原创粉丝点击