HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)

第一次用奇偶剪纸,记录一下吧

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
class NODE
{
public:
    
    NODE(int x,int y,int times):x(x),y(y),times(times){};
    NODE(){};
    NODE(const NODE &a)
    {
        x=a.x;
        y=a.y;
        times=a.times;
    }
    bool operator == (const NODE &a)
    {
        if(x==a.x&&y==a.y&×==a.times)
            return true;
        else
            return false;
    }
    int x,y,times;
};

int row,col,times;
NODE END;
const char wall ='X';
const char road ='.';
const char start ='S';
const char end = 'D';
char map[10][10];

int s_x,s_y;
int dir_x[]={0,1,0,-1};
int dir_y[]={1,0,-1,0};

bool canThrough(int x,int y)
{
    if(x>=0&&x<row&&y>=0&&y<col&&map[x][y]==road)
        return true;
    else
        return false;
}
bool _find;
void dfs(int x,int y,int t)
{
    if(_find==true|| t>=times)
        return ;
    int cut=times-t-abs(x-END.x)-abs(y-END.y);
    if(cut<0 || cut&1 )
        return ;
    for(int i=0;i<4;i++)
    {
        int now_x=x+dir_x[i];
        int now_y=y+dir_y[i];
        int tt=t+1;
        if(NODE(now_x,now_y,tt)==END)
        {
            _find=true;
            return;
        }
        if(canThrough(now_x,now_y))
        {
            map[now_x][now_y]=wall;
            dfs(now_x,now_y,tt);
            map[now_x][now_y]=road;
        }
    }
}

int main()
{
    //freopen("in.txt","r",stdin);
    while(cin>>row>>col>>times,row,col,times)
    {
        _find=false;
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                cin>>map[i][j];
                if(map[i][j]==start)
                {
                    s_x=i;
                    s_y=j;
                    continue;
                }
                if(map[i][j]==end)
                {
                    END=NODE(i,j,times);
                    continue;
                }
            }
        }
        dfs(s_x,s_y,0);
        if(_find)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值