HDU 1253(简单BFS)

 

 

很easy的一个BFS 注意强剪一下就过了~~6分钟敲完代码 调试5分钟  诶 自己还是太菜了~~~

 

以下是代码 HDU的服务器可能有问题 每次提交时间都不一样 最高的是765MS 还是小号 郁闷

 

当然还可以进一步优化 比如位运算处理 可能会快一倍以上时间吧 懒得写了 反正也不是太卡时间

 

 

#include <iostream>
#include 
<queue>
using namespace std;

typedef 
struct  
{
    
long mi,mj,mk;
    
long step;
}Node;

long hash[50][50][50];
long Map[50][50][50];
long A,B,C,T;
Node End;
Node Start;

inline 
void BFS()
{

    queue
<Node> q;
    
long cost=INT_MAX;
    fill(
&hash[0][0][0],&hash[50][0][0],INT_MAX);
    q.push(Start);

    Node t;
    
    
while (!q.empty())
    {
        t
=q.front();
        q.pop();

        
if (t.step>T)
        {
            
break;
        }

        
if (abs(t.mi-End.mi)+abs(t.mj-End.mj)+abs(t.mk-End.mk)>T)
        {
            
break;
        }

        
if (t.step>=hash[t.mi][t.mj][t.mk])
        {
            
continue;
        }
        
        hash[t.mi][t.mj][t.mk]
=t.step;
        
        
if (t.mi==End.mi&&t.mj==End.mj&&t.mk==End.mk)
        {
            
if (t.step<cost)
            {
                cost
=t.step;
                
break;
            }
            
continue;
        }
    
        Node n;

        
if (t.mi>0)
        {
            n.mi
=t.mi-1;
            n.mj
=t.mj;
            n.mk
=t.mk;

            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }            
        }

        
if (t.mi<A-1)
        {
            n.mi
=t.mi+1;
            n.mj
=t.mj;
            n.mk
=t.mk;
            
            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }
        }

        
if (t.mj>0)
        {
            n.mi
=t.mi;
            n.mj
=t.mj-1;
            n.mk
=t.mk;
            
            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }            
        }
        
        
if (t.mj<B-1)
        {
            n.mi
=t.mi;
            n.mj
=t.mj+1;
            n.mk
=t.mk;
            
            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }
        }

        
if (t.mk>0)
        {
            n.mi
=t.mi;
            n.mj
=t.mj;
            n.mk
=t.mk-1;
            
            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }            
        }
        
        
if (t.mk<C-1)
        {
            n.mi
=t.mi;
            n.mj
=t.mj;
            n.mk
=t.mk+1;
            
            
if (!Map[n.mi][n.mj][n.mk])
            {
                n.step
=t.step+1;
                
if(t.step<=T)
                {
                    q.push(n);
                }
            }
        }
    }

    
if (cost==INT_MAX)
    {
        puts(
"-1");
    }
    
else
    {
        printf(
"%ld\n",cost);
    }
        
}

int main()
{

    
long K;
    scanf(
"%ld",&K);
    Start.mi
=Start.mj=Start.mk=0;
    Start.step
=0;
    
while (K--)
    {
        scanf(
"%ld%ld%ld%ld",&A,&B,&C,&T);
        End.mi
=A-1;
        End.mj
=B-1;
        End.mk
=C-1;
        
long i,j,k;
        
for (i=0;i<A;++i)
        {
            
for (j=0;j<B;++j)
            {
                
for (k=0;k<C;++k)
                {
                    scanf(
"%ld",&Map[i][j][k]);
                }
            }
        }
        BFS();
    }
    
return 0;
}
posted @ 2008-09-07 23:36  Hdu-Lost  阅读(399)  评论(0编辑  收藏  举报