HDU 2579 Dating with girls(2)

这题就是有一点要注意:要开一个三维数组来保存每个点的步数状态,因为那些障碍物会在第k的倍数消失,所以在该点来走过也是可以再走的,所以要开一个三维数组保存步数状态,以为每个时间段的步数是不同的。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
const int inf=0x7fffffff;
struct T
{
    int x,y;
    int step;     
}q[100024];
int hash[124][124][16],n,m,k,X,Y;
int d[4][2]={0,1,1,0,0,-1,-1,0};
char map[124][124];
void init( )
{
   for( int i=0;i<124;i++ )
     for( int j=0;j<124;j++ )
       for(int k=0;k<=15;k++ )
         hash[i][j][k]=inf;
   memset( map,0,sizeof( map ) ) ;      
}
void getxy( )
{
    for( int i=1;i<=n;i++ )
      for( int j=1;j<=m;j++ )
      {
         if( map[i][j]=='Y' )
         {
            X=i;
            Y=j;
            return ;
         }
      }     
}
bool judge( int x,int y ,int step )
{
    if( x<=0||x>n||y<=0||y>m ) 
       return false;
    if( map[x][y]=='#' && step%k!=0  ) 
          return false;
    return true;
              
}
int push( int x,int y,int step,int end )
{
    T t;
    t.x=x;t.y=y;
    t.step=step;
    q[end++]=t;
    return end;
}
int BFS( )
{
    int first=0,end=0;
    end=push( X,Y,0,end );
    hash[X][Y][0]=0;
    while( first<end )
    {
        for( int i=0;i<4;i++ )
        {
           int dx=q[first].x+d[i][0];
           int dy=q[first].y+d[i][1];
           int Step=q[first].step+1;
           if(  judge( dx,dy,Step )&&hash[dx][dy][Step%k]>Step )  //判断该点是否可行和该点步数是否大于该点曾经步数
           {
              if( map[dx][dy]=='G' )  
              {
                   return Step;
              }
              end=push( dx, dy,Step,end );
              hash[dx][dy][Step%k]=Step;       
           }    
        } 
        first++;      
    }  
    return -1; 
}
int main()
{
    int Case;
    scanf( "%d",&Case );
    while( Case-- )
    {
       init();
       scanf( "%d%d%d",&n,&m,&k );
       for( int i=1;i<=n;i++ )
       {
            scanf( "%s",map[i]+1 );
       }
       getxy( );
       int t=BFS( );
       printf( t==-1?"Please give me another chance!\n":"%d\n",t );       
    }
   return 0;    
}

 

posted @ 2011-11-21 19:15  wutaoKeen  阅读(447)  评论(0)    收藏  举报