hd1010 Tempter of the Bone

今天重写了下这题,经典搜索题,同时也是某年的浙江省赛题.先分析下题目吧,是求在某一个时刻能不能达到一个指定的点,很显然可以用bfs,因为题目只要求可不可达,而没要求最短步数.这题的话需要一些剪枝,我剪了很少,900+MS过的,汗...
#include  < iostream >
#include 
< string >
using   namespace  std;
int  dir[ 4 ][ 2 =   {{0,1},{0,-1},{1,0},{-1,0}} ;
int  m, n, t;
char  Map[ 9 ][ 9 ];
int  sx, sy, ex, ey;
bool  flag;
void  dfs( int  x,  int  y,  int  time)
{
    
int i;
    
if(time < 0 || abs(x-ex)+abs(y-ey)>time)
        
return;
    
if(flag)
        
return;
    
if(x==ex && y==ey)
    
{
        
if(time == 0)
            flag 
= true;
        
return;
    }

    
for (i=0; i<4++i)
    
{
        
int tx = x+dir[i][0];
        
int ty = y+dir[i][1];
        
if(tx>=0&&tx<&& ty>=0&&ty<&& Map[tx][ty] != 'X')
        
{
            
char temp = Map[tx][ty];
            Map[tx][ty] 
= 'X';
            dfs(tx, ty, time
-1);
            Map[tx][ty] 
=temp;
        }

            
    }

}

int  main()
{
    
int i, j;
    
    
while (scanf("%d%d%d",&m,&n,&t) != EOF)
    
{
        
if(m==0 && n==0 && t==0)
            
break;
        getchar();
        
for (i=0; i<m; ++i)
        
{
            
for (j=0; j<n; ++j)
            
{
                scanf(
"%c"&Map[i][j]);
                
if(Map[i][j] == 'S')
                
{
                    sx 
= i, sy = j;
                }

                
if(Map[i][j] == 'D')
                
{
                    ex 
= i, ey = j;
                }

            }

            getchar();
        }

        flag 
= false;
        Map[sx][sy] 
= 'X';
        
if(abs(abs(sx-ex)+abs(sy-ey)-t)%2 == 0)
            dfs(sx, sy, t);
        
if(flag)
            printf(
"YES");
        
else
            printf(
"NO");
        printf(
" ");
    }

    
return 0;
}

你可能感兴趣的:(c,IM)