zju 1483 Asteroids!(三维BFS)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=438

#include <stdio.h>
#include <string.h>

#define MAXSIZE 12

struct coor
{
    int x,y,z;
    int time;
}que[MAXSIZE*MAXSIZE*MAXSIZE];

const int dir[6][3]={ {0,0,1},{0,0,-1},{0,-1,0},{0,1,0},{1,0,0},{-1,0,0} };

int N,sx,sy,sz,ex,ey,ez;
char g[MAXSIZE][MAXSIZE][MAXSIZE];
bool visited[MAXSIZE][MAXSIZE][MAXSIZE];

inline bool check(int x,int y,int z)
{
    if(x<0 || y<0 || z<0 || x>=N || y>=N || z>=N) return false;
    if(g[x][y][z]=='X' || visited[x][y][z]) return false;
    return true;
}

bool bfs()
{
    int i,j,k,l,front=-1,rear=-1;
    coor out,in;
    in.x=sx;
    in.y=sy;
    in.z=sz;
    in.time=0;
    que[++rear]=in;
    visited[sx][sy][sz]=true;
    while(front<rear)
    {
        out=que[++front];
        if(out.x==ex && out.y==ey && out.z==ez)
        {
            printf("%d %d\n",N,out.time);
            return true;
        }
        for(l=0;l<6;l++)
        {
            i=out.x+dir[l][0];
            j=out.y+dir[l][1];
            k=out.z+dir[l][2];
            if(!check(i,j,k)) continue;
            visited[i][j][k]=true;
            in.x=i;
            in.y=j;
            in.z=k;
            in.time=out.time+1;
            que[++rear]=in;
        }
    }
    return false;
}

int main()
{
    int i,j;
    char str[10];
    while(scanf("%s",str)!=EOF)
    {
        memset(visited,false,sizeof(visited));
        scanf("%d",&N);
        for(i=0;i<N;i++)
        {
            for(j=0;j<N;j++)
            {
                scanf("%s",g[i][j]);
            }
        }
        scanf("%d %d %d",&sz,&sy,&sx);//attention!!!!
        scanf("%d %d %d",&ez,&ey,&ex);
        scanf("%s",str);
   /*     printf("----------------------\n");
        for(i=0;i<N;i++)
        {
            for(j=0;j<N;j++)
            {
                printf("%s\n",g[i][j]);
            }
       //     printf("\n");
        }
        printf("(%d,%d,%d) (%d,%d,%d)\n",sx,sy,sz,ex,ey,ez);*/
        if(!bfs()) printf("NO ROUTE\n");
    }
    return 0;
}

 

posted @ 2010-08-27 22:58  菜到不得鸟  阅读(138)  评论(0)    收藏  举报