ZOJ2110 dfs+剪枝

//题意:一只小狗要从起点S到终点D且恰好在第t秒到达,问是否可能。

分析:这题关键是剪枝。

剪枝技巧来自http://blog.csdn.net/zxy_snow/article/details/6177892

DFS。

 

之前一直TLE了,死活不会优化 = =。。就搜了下。

 

那个用距离判断的我也加了,还是TLE了 = =。。学到个这个,这一句很神奇。。。

 

 

if( d % 2 != (Time-t) % 2 )

return ;

 

 

刚才自己手动模拟了下,理解了。d是当前点与目标点的坐标差。Time-t即从当前点到达目标点需要走多少步。

 

这两个值的奇偶性是一致的。如果中间没有墙,那么两者应该是d + x == Time - t。d是从当前点到达目标点的最短路,而这个x,设想一下,往外扩展再走,增加的长宽和必定是偶数。而任何数加上偶数奇偶性不变。

 

神奇呀~! = =。。。搜索题还是应该多做啊。。。搜索很神奇。。。就多那么一句话,就让你的代码从TLE到AC。。。呜呜。。

// I'm the Topcoder
//C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
//C++
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cctype>
#include <stack>
#include <string>
#include <list>
#include <queue>
//#include <str>
#include <vector>
#include <deque>
#include <set>
using namespace std;

//*************************OUTPUT*************************
#ifdef WIN32
#define INT64 "%I64d"
#define UINT64 "%I64u"
#else
#define INT64 "%lld"
#define UINT64 "%llu"
#endif

//**************************CONSTANT***********************
#define INF 0x3f3f3f3f
#define eps 1e-8
#define PI acos(-1.)
#define PI2 asin (1.);
typedef long long LL;
//typedef __int64 LL;   //codeforces
typedef unsigned int ui;
typedef unsigned long long ui64;
#define MP make_pair
typedef vector<int> VI;
typedef pair<int, int> PII;
#define pb push_back
#define mp make_pair

//***************************SENTENCE************************
#define CL(a,b) memset (a, b, sizeof (a))
#define sqr(a,b) sqrt ((double)(a)*(a) + (double)(b)*(b))
#define sqr3(a,b,c) sqrt((double)(a)*(a) + (double)(b)*(b) + (double)(c)*(c))

//****************************FUNCTION************************
template <typename T> double DIS(T va, T vb)
{
    return sqr(va.x - vb.x, va.y - vb.y);
}
template <class T> inline T INTEGER_LEN(T v)
{
    int len = 1;
    while (v /= 10) ++len;
    return len;
}
template <typename T> inline T square(T va, T vb)
{
    return va * va + vb * vb;
}

// aply for the memory of the stack
//#pragma comment (linker, "/STACK:1024000000,1024000000")
//end

const int maxn = 1010;
char str[maxn][maxn];
int n,m,t;
int di,dj;
bool escape;
int dir[4][2]= {{0,1},{1,0},{0,-1},{-1,0}};

void dfs(int si,int sj,int cnt){
    int tmp;
    if(si>n||sj>m||si<=0||sj<=0) return;//边界
    if(si==di&&sj==dj&&cnt==t){//终点状态
        escape=1;
        return;
    }
    tmp=(t-cnt)-abs(si-di)-abs(sj-dj);
    if(tmp<0||tmp%2!=0)  return;//剩余的路程一定要大于理论剩余路程的最小值才到到达,剩余路程一定要跟理论剩余距离同奇或者同偶;
    for(int i=0; i<4; i++){
        if(str[si+dir[i][0]][sj+dir[i][1]]!='X'){
            str[si+dir[i][0]][sj+dir[i][1]]='X';
            dfs(si+dir[i][0],sj+dir[i][1],cnt+1);
            if(escape) return;
            str[si+dir[i][0]][sj+dir[i][1]]='.';
        }
    }
    return;
}

int main(){
    int si,sj;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF){
        if(n==0&&m==0&&t==0){
            break;
        }
        int wall=0;
        char tm;
        //scanf("%c",&tm);
        getchar();
        for(int i=1; i<=n; i++){
           // getchar();
            for(int j=1; j<=m; j++){
                scanf("%c",&str[i][j]);
                if(str[i][j]=='S'){//si,sj保存起始坐标
                    si=i;
                    sj=j;
                }
                else if(str[i][j]=='D'){//di,dj保存终点坐标
                    di=i;
                    dj=j;
                }
                else if(str[i][j]=='X'){//计算墙的个数,为方便剪枝
                    wall++;
                }
            }
            getchar();
            //scanf("%c",&tm);
        }
        if(n*m-wall<=t){//留下可走的步数比总的时间少
            printf("NO\n");
            continue;
        }
        escape=0;
        str[si][sj]='X';
        dfs(si,sj,0);
        if(escape)  printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

//3 4 5
//S...
//.X.X
//...D
//4 4 8
//.X.X
//..S.
//....
//DX.X
//4 4 5
//S.X.
//..X.
//..XD
//....

  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值