hdu1240 poj 2225 三维BFS

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

#include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char map[15][15][15];
typedef struct
{
    int x,y,z;
    int cont;
}node;
int main()
{
    char s[10];
    int n,i,j,sx,sy,sz,ex,ey,ez;
    while(scanf("%s%d",s,&n)!=EOF)
    {
        queue<node>q;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
                scanf("%s",map[i][j]);
        }
        scanf("%d%d%d",&sx,&sy,&sz);
        scanf("%d%d%d",&ex,&ey,&ez);
        scanf("%s",s);
        if(sx==ex&&sy==ey&&sz==ez)
        {
            cout<<n<<" "<<"0"<<endl;
            continue;
        }
        node tem;
        tem.x=sx;
        tem.y=sy;
        tem.z=sz;
        tem.cont=0;
        q.push(tem);
        map[sz][sx][sy]='X';
        bool flag=0;
        while(!q.empty())
        {
            node f=q.front();
            q.pop();
            int dx[6]={0,1,0,-1,0,0};//row
            int dy[6]={1,0,-1,0,0,0};//column
            int dz[6]={0,0,0,0,1,-1};//slice
            for(i=0;i<6;i++)
            {
                int tx=f.x+dx[i];
                int ty=f.y+dy[i];
                int tz=f.z+dz[i];
                tem.x=tx;
                tem.y=ty;
                tem.z=tz;
                tem.cont=f.cont+1;
                if(tx>=0&&tx<n&&ty>=0&&ty<n&&tz>=0&&tz<n)
                {
                    if(tx==ex&&ty==ey&&tz==ez)
                    {
                        cout<<n<<" "<<tem.cont<<endl;
                        flag=1;
                        break;
                    }
                    if(map[tz][tx][ty]=='O')
                    {
                        q.push(tem);//cout<<tz<<tx<<ty<<endl;
                        map[tz][tx][ty]='X';
                    }
                }
            }
            if(flag)
                 break;
         }
         if(!flag)
         cout<<"NO ROUTE"<<endl;
     }
     return 0;
}

开始写的while(scanf("%s%d",s,&n)&&n)过不了额


http://poj.org/problem?id=2225

输入的时候 x, y换下就行,hdu的输入是不是有问题

#include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char map[15][15][15];
typedef struct
{
    int x,y,z;
    int cont;
}node;
int main()
{
    char s[10];
    int n,i,j,sx,sy,sz,ex,ey,ez;
    while(scanf("%s%d",s,&n)!=EOF)
    {
        queue<node>q;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
                scanf("%s",map[i][j]);
        }
        scanf("%d%d%d",&sy,&sx,&sz);
        scanf("%d%d%d",&ey,&ex,&ez);
        scanf("%s",s);
        if(sx==ex&&sy==ey&&sz==ez)
        {
            cout<<n<<" "<<"0"<<endl;
            continue;
        }
        node tem;
        tem.x=sx;
        tem.y=sy;
        tem.z=sz;
        tem.cont=0;
        q.push(tem);
        map[sz][sx][sy]='X';
        bool flag=0;
        while(!q.empty())
        {
            node f=q.front();
            q.pop();
            int dx[6]={0,1,0,-1,0,0};//row
            int dy[6]={1,0,-1,0,0,0};//column
            int dz[6]={0,0,0,0,1,-1};//slice
            for(i=0;i<6;i++)
            {
                int tx=f.x+dx[i];
                int ty=f.y+dy[i];
                int tz=f.z+dz[i];
                tem.x=tx;
                tem.y=ty;
                tem.z=tz;
                tem.cont=f.cont+1;
                if(tx>=0&&tx<n&&ty>=0&&ty<n&&tz>=0&&tz<n)
                {
                    if(tx==ex&&ty==ey&&tz==ez)
                    {
                        cout<<n<<" "<<tem.cont<<endl;
                        flag=1;
                        break;
                    }
                    if(map[tz][tx][ty]=='O')
                    {
                        q.push(tem);//cout<<tz<<tx<<ty<<endl;
                        map[tz][tx][ty]='X';
                    }
                }
            }
            if(flag)
                 break;
         }
         if(!flag)
         cout<<"NO ROUTE"<<endl;
     }
     return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值