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;
}


你可能感兴趣的:(HDU 1010 Tempter of the Bone(DFS+奇偶剪枝))