HDU 2216(简单BFS)

使用四维数组进行状态判重  注意要用scanf("%s“)或者gets  用cin莫名其妙WA  我查了30分钟错都没查出 改一下就好了 FUCK~~~

 

这道DK有更有效率的BFS 所以这次2个代码都贴上 任君瞻仰

 

 

 

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

char Map[30][30];

typedef 
struct  
{
    
long zi,zj;
    
long si,sj;
    
long step;
}Node;

queue
<Node> q;
long hash[30][30][30][30];

Node Start;
long N,M;

inline 
void BFS()
{
    
long i,j,k,l;
    
    
for (i=0;i<30;++i)
    {
        
for (j=0;j<30;++j)
        {
            
for (k=0;k<30;++k)
            {
                
for (l=0;l<30;++l)
                {
                    hash[i][j][k][l]
=INT_MAX;
                }
            }
        }
    }

    Start.step
=0;

    
while (!q.empty())
    {
        q.pop();
    }
    
    q.push(Start);

    
long cost=INT_MAX;
    Node t;
    
while (!q.empty())
    {
        t
=q.front();
        q.pop();
        
        
if (t.step>=hash[t.si][t.sj][t.zi][t.zj])
        {
            
continue;
        }

        hash[t.si][t.sj][t.zi][t.zj]
=t.step;

        
if  (abs(t.si-t.zi)+abs(t.sj-t.zj)<=1)
        {
            
if (t.step<cost)
            {
                cost
=t.step;
            }
            
continue;
        }
        
        Node n;

        
if (t.si>0&&Map[t.si-1][t.sj]!='X')
        {
            n.si
=t.si-1;
            n.sj
=t.sj;

            
if (t.zi<N-1)
            {
                
if(Map[t.zi+1][t.zj]!='X')
                {
                    n.zi
=t.zi+1;
                    n.zj
=t.zj;
                }
                
else
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj;
                }
            }
            
else
            {
                n.zi
=t.zi;
                n.zj
=t.zj;
            }

            n.step
=t.step+1;
            q.push(n);
        }

        
if (t.si<N-1&&Map[t.si+1][t.sj]!='X')
        {

            n.si
=t.si+1;
            n.sj
=t.sj;
            
            
if (t.zi>0)
            {
                
if(Map[t.zi-1][t.zj]!='X')
                {
                    n.zi
=t.zi-1;
                    n.zj
=t.zj;
                }
                
else
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj;
                }
            }
            
else
            {
                n.zi
=t.zi;
                n.zj
=t.zj;
            }
            
            n.step
=t.step+1;
            q.push(n);
        }

        
if (t.sj>0&&Map[t.si][t.sj-1]!='X')
        {

            n.si
=t.si;
            n.sj
=t.sj-1;

            
if (t.zj<M-1)
            {
                
if(Map[t.zi][t.zj+1]!='X')
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj+1;
                }
                
else
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj;
                }
            }
            
else
            {
                n.zi
=t.zi;
                n.zj
=t.zj;
            }
            
            n.step
=t.step+1;
            q.push(n);

        }

        
if (t.sj<M-1&&Map[t.si][t.sj+1]!='X')
        {

            n.si
=t.si;
            n.sj
=t.sj+1;
            
            
if (t.zj>0)
            {
                
if(Map[t.zi][t.zj-1]!='X')
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj-1;
                }
                
else
                {
                    n.zi
=t.zi;
                    n.zj
=t.zj;
                }
            }
            
else
            {
                n.zi
=t.zi;
                n.zj
=t.zj;
            }
            
            n.step
=t.step+1;
            q.push(n);
        }

    }

    
if (cost==INT_MAX)
    {
        puts(
"Bad Luck!");
    }
    
else
    {
        printf(
"%ld\n",cost);
    }
        
}


int main()
{
    
    
while (scanf("%ld%ld",&N,&M)!=EOF)
    {
        
long i,j;

        getchar();
        
for (i=0;i<N;++i)
        {
            gets(Map[i]);
            
for (j=0;j<M;++j)
            {
                
if (Map[i][j]=='Z')
                {
                    Start.si
=i;
                    Start.sj
=j;
                }

                
if (Map[i][j]=='S')
                {
                    Start.zi
=i;
                    Start.zj
=j;
                }

            }
        }

        BFS();
    }
    
return 0;
}

 

 

 

#include<iostream>
using namespace std;
int m,n;
char mat[20][20];
int x1,x2,y1,y2;
struct node
{
    
int x,y,p,q;
    node(
int a=0,int b=0,int c=0,int d=0):x(a),y(b),p(c),q(d){}
}s[
1000000];
int dis[20][20][20][20];
const int inf=1000000000;
inline 
bool ok(int x,int y)
{
    
if(x<0||y<0||x>=m||y>=n)
        
return 1;
    
return mat[x][y]=='X';
}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int dx2[4]={-1,0,1,0};
int dy2[4]={0,-1,0,1};

int solve()
{
    
for(int i=0;i<m;i++)
        
for(int j=0;j<n;j++)
            
for(int p=0;p<m;p++)
                
for(int q=0;q<n;q++)
                    dis[i][j][p][q]
=inf;
    dis[x1][y1][x2][y2]
=0;
    
int h=0,r=0;
    s[
0]=node(x1,y1,x2,y2);
    
while(h<=r)
    {
        node t
=s[h++];
        
for(int i=0;i<4;i++)
        {
            
int tx=t.x+dx[i];
            
int ty=t.y+dy[i];
            
if(ok(tx,ty))continue;
            
int tx2=t.p+dx2[i];
            
int ty2=t.q+dy2[i];
            
if(ok(tx2,ty2))
            {
                
if(dis[tx][ty][t.p][t.q]<inf)continue;
                dis[tx][ty][t.p][t.q]
=dis[t.x][t.y][t.p][t.q]+1;
                
if(abs(tx-t.p)+abs(ty-t.q)==1||abs(tx-t.p)+abs(ty-t.q)==0)return dis[tx][ty][t.p][t.q];
                s[
++r]=node(tx,ty,t.p,t.q);
            }
            
else
            {
                
if(dis[tx][ty][tx2][ty2]<inf)continue;
                dis[tx][ty][tx2][ty2]
=dis[t.x][t.y][t.p][t.q]+1;
                
if(abs(tx-tx2)+abs(ty-ty2)==1||abs(tx-tx2)+abs(ty-ty2)==0)return dis[tx][ty][tx2][ty2];
                s[
++r]=node(tx,ty,tx2,ty2);
            }
        }
    }
    
return -1;
}
int main()
{
    
//freopen("a.txt","r",stdin);
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        
for(int i=0;i<m;i++)
            scanf(
"%s",mat[i]);
        
for(int i=0;i<m;i++)
            
for(int j=0;j<n;j++)
            {
                
if(mat[i][j]=='Z')
                {
                    x1
=i,y1=j;
                    mat[i][j]
='.';
                }
                
if(mat[i][j]=='S')
                {
                    x2
=i;y2=j;
                    mat[i][j]
='.';
                }
            }
        
if(abs(x1-x2)+abs(y1-y2)==1)
        {
            puts(
"0");
            
continue;
        }
        
int res=solve();
        
if(res<0)
            puts(
"Bad Luck!");
        
else
            printf(
"%d\n",res);
    }
}
posted @ 2008-09-06 20:16  Hdu-Lost  阅读(437)  评论(0编辑  收藏  举报