hdu 1010 Tempter of the Bone

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010


题目大意:

在一张n*m的图(n,m<7)有一只小狗在‘S’位置,从第0步开始,要恰好在第t步(t<50)走到‘D’,‘.’可以走,‘X’不能走,且走过的点不能再走,也不能在点上停留。

判断小狗能否满足要求走到‘D’。


题目思路:

直接暴力深搜会TLE,所以要加剪枝

(1)判断当前点到‘D’所需要要的最小步数,如果该步数大于剩余步数,显然之后怎么走都不可达。

(2)奇偶剪枝,判断当前点到‘D’所需要的最小步数的奇偶性和剩余步数的奇偶性,如果不同那么显然也不可达。

那么为什么要和最小步数的奇偶性相同呢

其实画个图就很明朗了。

图1                图2

SX...D   S....D

......   ......

图1和图2到D点奇偶性是相等的。

因为绕过X需要多花费2步。

所以绕过n个X,就需要2*n步,显然不影响奇偶性。


代码:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll __int64
#define ls rt<<1
#define rs ls|1
#define lson l,mid,ls
#define rson mid+1,r,rs
#define middle (l+r)>>1
#define eps (1e-8)
#define clr_all(x,c) memset(x,c,sizeof(x))
#define clr(x,c,n) memset(x,c,sizeof(x[0])*(n+1))
#define MOD (1000000007)
#define inf (0x3f3f3f3f)
#define pi (acos(-1.0))
#define _max(x,y) (((x)>(y))? (x):(y))
#define _min(x,y) (((x)<(y))? (x):(y))
#define _abs(x) ((x)<0? (-(x)):(x))
#define getmin(x,y) (x= (x<0 || (y)<x)? (y):x)
#define getmax(x,y) (x= ((y)>x)? (y):x)
template <class T> void _swap(T &x,T &y){T t=x;x=y;y=t;}
int TS,cas=1;
const int M=7+5;
int n,m,t;
char maze[M][M];
bool ok;
int tx,ty,sx,sy;
void dfs(int x,int y,int c){
	if(x<1 || y<1 || x>n || y>m) return;
	if(ok || c>t) return;
	if(maze[x][y]=='D' && c==t){ok=1;return;}
	if(maze[x][y]!='.') return;
	int tmp=_abs(x-tx)+_abs(y-ty);
	if(tmp>t-c || tmp%2!=(t-c)%2) return;
	maze[x][y]='X';
	dfs(x-1,y,c+1),dfs(x+1,y,c+1);
	dfs(x,y-1,c+1),dfs(x,y+1,c+1);
	maze[x][y]='.';
}

void run(){
    int i,j;
	for(i=1;i<=n;i++)
		scanf("%s",maze[i]+1);
	int cnt=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			if(maze[i][j]=='S') sx=i,sy=j;
			else if(maze[i][j]=='D') tx=i,ty=j,cnt++;
			else if(maze[i][j]=='.') cnt++;
		}
	}
	ok=0;
	if(cnt>=t) maze[sx][sy]='.',dfs(sx,sy,0);
	printf("%s\n",ok? "YES":"NO");
}

void preSof(){
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    preSof();
    //run();
    while(~scanf("%d%d%d",&n,&m,&t) && (n||m||t)) run();
    //for(scanf("%d",&TS);cas<=TS;cas++) run();
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值