hd1010 Tempter of the Bone

本文解析了一道经典的搜索题目,该题源自某年浙江省的竞赛。文章详细介绍了如何使用广度优先搜索(BFS)来判断在给定时间内能否到达目标位置,并通过代码示例展示了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天重写了下这题,经典搜索题,同时也是某年的浙江省赛题.先分析下题目吧,是求在某一个时刻能不能达到一个指定的点,很显然可以用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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值