LeeBlog

导航

HDU 1010 Tempter of the Bone

这题如果是神搜的一基本题目,每次都向四个方向神搜就行了,不过要考虑剪枝,要用奇偶剪枝,否则TLE,还有用一个标记f,标记当前是否已找到来剪枝。如果还想减少时间就可以在输入时做一个标记,看当前有多少个可以走的点,如果可以走的点小于总时间t那么肯定是NO,这样要减少很多饿。。。。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
int n,m,t,sx,sy,ex,ey,f;
int maz[10][10],des[10][10];
void DFS( int y,int x,int num )
{
     if( des[y][x] ||f|| maz[y][x] == 'X'||num > ( n * m ) )
         return ;
     if( maz[y][x] == 'D'&&num == t )
     {
         f = 1;//看是否找到
         return ;
     }
     if( abs( t - num ) % 2 != abs(abs( y - ey ) + abs( x - ex )) % 2 )
         return ;//奇偶剪枝
     des[y][x] = 1;
     DFS( y - 1,x,num+1 );
     DFS( y,x - 1, num+1 );
     DFS( y + 1, x, num+1 );
     DFS( y,x + 1, num + 1 );
     des[y][x] = 0;
 }
int main( )
{
    while( scanf( "%d%d%d%*c",&n,&m,&t ),n||m||t )
    {     
          int ans = 0;
           memset( des,0,sizeof( des ) );
           for( int i = 0; i < 10; ++i )
                for( int j = 0; j < 10; ++j )
                     maz[i][j] = 'X';
           for( int i = 1; i <= n; ++i )
           {
                for( int j = 1; j <= m; ++j )
                {
                     scanf( "%c",&maz[i][j] );
                     if( maz[i][j] == 'S' )
                         sy = i,sx = j;
                    else if( maz[i][j] == 'D' )
                         ey = i,ex = j;
                    else if(maz[i][j]=='.')
                         ans++;//标记当前可走的点
                     } 
                getchar();
           }
           f = 0;
           if( ans < t )
               printf("NO\n");
           else
           {
               DFS( sy,sx,0 );
               puts( f ? "YES":"NO" );
           }
           }
    return 0;
}

posted on 2011-05-11 09:49  LeeBlog  阅读(220)  评论(0编辑  收藏  举报