hdu1010 Tempter of the Bone

#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <cstring>
#define N 8
using namespace std;
int row;
int col;
int step;
int sx,sy,ex,ey;
bool can;
char maze[N+1][N+1];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
void DFS(int x,int y,int cnt)
{
    if(can)
        return ;
    if(x==ex&&y==ey&&cnt == step)
    {
        can = true;
        return ;
    }
    int tmp = abs(ex-x)+abs(ey-y)-abs(cnt-step);
    if(tmp>0||tmp%2)
        return ;
    for(int i=0;i<4;i++)
    {
        int nx = x+dir[i][0];
        int ny = y+dir[i][1];
        if(nx>=0&&nx<row&&ny>=0&&ny<col&&maze[nx][ny]=='.')
        {
            maze[nx][ny] = 'X';
            DFS(nx,ny,cnt+1);
            maze[nx][ny] = '.';
        }
    }
    
}
int main()
{
    while(scanf("%d%d%d",&row,&col,&step)!=EOF&&(row||col||step))
    {
        can = false;
        memset(maze,0,sizeof(maze));
        for(int i=0;i<row;i++)
            scanf("%s",maze[i]);
        for(int i=0;i<row;i++)
            for(int j=0;j<col;j++)
            {
                if(maze[i][j]=='S')
                {
                    sx = i;
                    sy = j;
                    maze[i][j] = '.';
                }
                if(maze[i][j]=='D')
                {
                    ex = i;
                    ey = j;
                    maze[i][j] = '.';
                }
            }
        maze[sx][sy] = 'X';
        DFS(sx,sy,0);
        if(can)
            printf("YES\n");
            else
                printf("NO\n");
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值