hdoj Asteroids!

来源:互联网 发布:泉州千域网络 编辑:程序博客网 时间:2024/05/21 18:26

                                                     Asteroids!

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1240

比较简单的广搜:不过注意一下x,y,z的顺序即可

#include<queue>#include<iostream>#include"cstdio"#include"cstring"using namespace std;char map[11][11][11];int v[11][11][11],x,y,z,x1,y1,z1,n;int nx[6]={0,0,0,0,1,-1},ny[6]={-1,0,1,0,0,0},nz[6]={0,-1,0,1,0,0};typedef struct node{    int x,y,z,s;}chao; //queue<chao>q;void bfs(){    chao from,to;    queue<chao>q;  memset(v,0,sizeof(v));      from.x=x;      from.y=y;      from.z=z;      from.s=0;      v[x][y][z]=1;       q.push(from);       while(!q.empty())       {            from=q.front();            q.pop();           if(from.x==x1&&from.y==y1&&from.z==z1)           {               printf("%d %d\n",n,from.s);               return ;           }            for(int i=0;i<6;i++)               {                   to.x=from.x+nx[i];                   to.y=from.y+ny[i];                   to.z=from.z+nz[i];                   if(to.x>=0&&to.x<n&&to.y>=0&&to.y<n&&to.z>=0&&to.z<n&&!v[to.x][to.y][to.z]&&map[to.x][to.y][to.z]=='O')                   {                       v[to.x][to.y][to.z]=1;                       to.s=from.s+1;                       q.push(to);                   }               }       }        printf("NO ROUTE\n");}int main(){       int i,j;       char ch[10],ch1;        while(scanf("%s %d",ch,&n)!=EOF)        {            for(i=0;i<n;i++)               {                   for(j=0;j<n;j++)                     {                            scanf("%s",map[i][j]);                            ch1=getchar();                     }               }                 scanf("%d%d%d",&z,&y,&x);                 scanf("%d%d%d",&z1,&y1,&x1);              scanf("%s",ch);              ch1=getchar();              bfs();        }    return 0;}



原创粉丝点击