HDU 1010 Tempter of the Bone(DFS)

http://acm.hdu.edu.cn/showproblem.php?pid=1010

分析:
简单的DFS题,很适合新手练习。
题意:
给一个图里面有”X”,”S”, ” .” ,”D”这几种,
问你能不能在T时刻从S到达D,
每个格最多只能走一次,
主要就是注意剪枝,
剪枝的好坏影响过题的时间,
不行的甚至会T。

//容易理解的写法
//1568KB    62MS
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>

typedef long long LL;
const double pi = acos(-1.0);
const long long mod = 1e9 + 7;
using namespace std;

int N,M,T;
int bx,by;
int ok;
char s[10][10];
int d[4][2] = {-1,0,0,-1,0,1,1,0};

void dfs(int x,int y,int T)
{
    if(ok || T < abs(x - bx) + abs(y - by) || (x + bx + y + by + T) % 2)//剪枝
        return;
    if(T == 0 && x == bx && y == by)
    {
        ok = 1;
        return;
    }
    for(int i = 0;i < 4;i++)
    {
        int nowx = x + d[i][0];
        int nowy = y + d[i][1];
        if(nowx >= 0 && nowx < N && nowy >= 0 && nowy < M && s[nowx][nowy] != 'X')
        {
            s[nowx][nowy] = 'X';
            dfs(nowx,nowy,T - 1);
            if(ok)
                return;
            s[nowx][nowy] = '.';
        }

    }
}

int main()
{
    //freopen("int.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int ax,ay;
    int num;
    while(scanf("%d %d %d",&N,&M,&T) && N + M + T)
    {
        num = 0;
        for(int i = 0;i < N;i++)
            scanf("%s",s[i]);
        for(int i = 0;i < N;i++)
            for(int j = 0;j < M;j++)
            {
                if(s[i][j] == 'S')
                {
                    ax = i;
                    ay = j;
                }
                else if(s[i][j] == 'D')
                {
                    bx = i;
                    by = j;
                }
                else if(s[i][j] == 'X')
                    num++;
            }
        if(M * N - num <= T)//剩下能走的格子数少于总时间
        {
            puts("NO");
            continue;
        }
        else
        {
            ok = 0;
            s[ax][ay] = 'X';
            dfs(ax,ay,T);
            puts(ok ? "YES" : "NO");
        }
    }
    return 0;
}
//代码量稍大的写法
//值得学习
//1572KB 0MS
#include <iostream>
#include <queue>
#include <cstdio>
#include <memory.h>
using namespace std;

const int SIZE=8;

inline int abs(int n) { return n>0?n:-n; }
inline int dis(int x,int y) { return abs(x)+abs(y); }

struct pos{
    int x,y;
};

int N,M,T,sx,sy,dx,dy,dir[][2]={{-1,0},{1,0},{0,-1},{0,1}};
char m[SIZE][SIZE];
bool vis[SIZE][SIZE];

bool isR(int sx,int sy){
    queue<pos> q;
    pos tmp={sx,sy};
    memset(vis,0,sizeof vis);
    q.push(tmp); vis[sx][sy]=1;
    while(!q.empty()){
        tmp=q.front(); q.pop();
        for(int i=0;i<4;++i){
            int tx=tmp.x+dir[i][0],ty=tmp.y+dir[i][1];
            if(tx==dx && ty==dy) return 1;
            if(tx<0 || ty<0 || tx>=N || ty>=M || vis[tx][ty] || m[tx][ty]=='X') continue;
            pos tmp2={tx,ty}; q.push(tmp2);
            vis[tx][ty]=1;
        }
    }
    return 0;
}

int leftS(int sx,int sy){
    int step=0;
    queue<pos> q;
    pos tmp={sx,sy};
    memset(vis,0,sizeof vis);
    q.push(tmp); vis[sx][sy]=1;
    while(!q.empty()){
        tmp=q.front(); q.pop();
        for(int i=0;i<4;++i){
            int tx=tmp.x+dir[i][0],ty=tmp.y+dir[i][1];
            if(tx<0 || ty<0 || tx>=N || ty>=M || vis[tx][ty] || m[tx][ty]=='X') continue;
            pos tmp2={tx,ty}; q.push(tmp2);
            vis[tx][ty]=1; ++step;
        }
    }
    return step;
}

bool DFS(int sx,int sy,int cur){
    if(sx==dx && sy==dy && !cur) return 1;
    int left=cur-dis(dx-sx,dy-sy);
    if(left&1 || left<0 || !isR(sx,sy) || leftS(sx,sy)<cur) return 0;
    for(int i=0;i<4;++i){
        int tx=sx+dir[i][0],ty=sy+dir[i][1];
        if(tx<0 || ty<0 || tx>=N || ty>=M || m[tx][ty]=='X') continue;
        m[tx][ty]='X';
        if(DFS(tx,ty,cur-1)) return 1;
        m[tx][ty]='.';
    }
    return 0;
}

int main(){
    while(scanf("%d %d %d",&N,&M,&T) && N+M+T){
        for(int i=0;i<N;++i){
            scanf("%s",m[i]);
            for(int j=0;j<M;++j){
                if(m[i][j]=='S') { sx=i; sy=j; }
                else if(m[i][j]=='D') { dx=i; dy=j; }
            }
        }
        m[sx][sy]='X';
        puts(DFS(sx,sy,T)?"YES":"NO");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值