Tempter of the Bone HDU - 1010(搜索+剪枝)

传送门:QAQ

 

题意:问是否能在固定步数内到达终点

 

思路:简单的深搜,因为限定步数较少,加上奇偶路径判断即可(读入方式有点BUG,用%c一个个读就wa了)。

 

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
char ch[10][10];
int vis[10][10];
int dx[5] = { 1,0,-1,0 };
int dy[5] = { 0,1,0,-1 };
int X1, Y1;
int xx, yy;
int n, m, t;
int flag;
int fla;
void dfs(int x, int y, int tt) {
	if (tt>t) {
		//printf("%d %d %d************\n", x, y,tt);
		return;
	}
	if (ch[x][y] == 'D') {
		if (tt == t)
			fla = 1;
		return;
	}
	for (int i = 0; i < 4; i++) {
		int x2 = x + dx[i];
		int y2 = y + dy[i];
		if (x2 >= 0 && x2 < n&&y2 >= 0 && y2 < m&&ch[x2][y2] != 'X' && !vis[x2][y2]) {
			vis[x2][y2] = 1;
			dfs(x2, y2, tt + 1);
			vis[x2][y2] = 0;
			if (fla)
				return;
		}
	}
}
int main(void) {
	while (scanf("%d%d%d", &n, &m, &t) != EOF) {
		flag = 0;
		fla = 0;
		memset(vis, 0, sizeof(vis));
		if (n == 0 && m == 0 && t == 0) {
			break;
		}
		for (int i = 0; i < n; i++) {
			scanf("%s", &ch[i]);
			for (int z = 0; z < m; z++) {

				if (ch[i][z] == 'D') {
					X1 = i;
					Y1 = z;
				}
				if (ch[i][z] == 'S') {
					xx = i;
					yy = z;
				}
			}
		}
		if ((abs(X1 - xx) + abs(Y1 - yy)) % 2 != t % 2) {
			printf("NO\n");
			continue;
		}
		vis[xx][yy] = 1;
		dfs(xx, yy, 0);
		if (fla) {
			printf("YES\n");
		}
		else
			printf("NO\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值