HDU2102 A计划分别用BFS和DFS实现

**

BFS

**

//BFS
#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m, T;
typedef struct Node {
	int x, y, z;
	int t;
}Node;
int A[2][15][15];	
int ans[2][15][15];	//存储状态
int vis[2][15][15];	//是否走过
int sx, sy, sz, ex, ey, ez;
int arr[4][2] = { {0,-1},{0,1},{1,0},{-1,0} };	//四个方向
queue<Node>q;
bool check(int x, int y, int z) {
	if (x >= 0 && x < 2 && y >= 0 && y < n&&z >= 0 && z < m) {
		//不能是墙,时空机后面不能是时空机,且没有走过
		if (A[x][y][z] && !vis[x][y][z] && A[x][y][z] != 2)return 1;	
		else return 0;
	}
	return 0;
}
void update_ans(Node &s) {
	//更新状态
	if (ans[s.x][s.y][s.z] < 0 || s.t < ans[s.x][s.y][s.z])ans[s.x][s.y][s.z] = s.t;
}
void BFS() {
	Node start;
	start.x = sx; start.y = sy; start.z = sz;
	start.t = 0;
	vis[sx][sy][sz] = 1;
	q.push(start);
	while (!q.empty()) {
		Node v = q.front(); q.pop();
		int i;
		update_ans(v);		
		if (ans[ex][ey][ez] >= 0)return;
		for (i = 0; i < 4; i++) {
			Node t = v;
			t.y += arr[i][0]; t.z += arr[i][1];
			t.t++;
			if (A[t.x][t.y][t.z] == 2) { t.x = (t.x + 1) % 2; }
			if (check(t.x, t.y, t.z)) {
				vis[t.x][t.y][t.z] = 1;
				q.push(t);
			}
		}
	}
}
int main() {
	int C;
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	scanf("%d", &C);
	while (C--) {
		scanf("%d%d%d", &n, &m, &T);
		int i, j, k;
		while (!q.empty())q.pop();
		memset(A, 0, sizeof(A));
		for (i = 0; i < 2; i++) {
			for (j = 0; j < n; j++) {
				for (k = 0; k < m; k++) {
					char ch;
					scanf("%c", &ch);
					while (ch == '\n' || ch == ' ' || ch == '\0' || ch == '?')scanf("%c", &ch);
					if (ch == 'S') {
						A[i][j][k] = 1; sx = i;
						sy = j; sz = k;
					}
					else if (ch == 'P') {
						A[i][j][k] = 9; ex = i;
						ey = j; ez = k;
					}
					else if (ch == '*')A[i][j][k] = 0;
					else if (ch == '#')A[i][j][k] = 2;
					else if (ch == '.')A[i][j][k] = 1;
				}
			}
		}
		memset(ans, -1, sizeof(ans));
		memset(vis, 0, sizeof(vis));
		BFS();
		if (ans[ex][ey][ez] >= 0 && ans[ex][ey][ez] <= T)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

DFS

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int n, m, T;
int A[2][15][15];
int vis[2][15][15];
int sx, sy, sz, ex, ey, ez;
int arr[4][2] = { {0,-1},{0,1},{1,0},{-1,0} };
bool check(int x, int y, int z) {
	if (x >= 0 && x < 2 && y >= 0 && y < n&&z >= 0 && z < m) {
		if (A[x][y][z] && !vis[x][y][z] && A[x][y][z] != 2)return 1;
		else return 0;
	}
	return 0;
}
int DFS(int x, int y, int z,int cur) {
	if (cur > T)return 0;
	if (A[x][y][z] == 9 && cur <= T)return 1;
	else {
		int i;
		for (i = 0; i < 4; i++) {
			int xx, yy, zz;
			yy = y + arr[i][0];
			zz = z + arr[i][1];
			xx = x;
			if (A[xx][yy][zz] == 2)xx = (x + 1) % 2;
			if (check(xx,yy,zz)) {
				vis[xx][yy][zz] = 1;
				int flg = DFS(xx, yy, zz, cur + 1);
				if (flg)return flg;
				vis[xx][yy][zz] = 0;
			}
		}
	}
	return 0;
}
int main() {
	int C;
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	scanf("%d", &C);
	while (C--) {
		scanf("%d%d%d", &n, &m, &T);
		int i, j, k;
		memset(A, 0, sizeof(A));
		for (i = 0; i < 2; i++) {
			for (j = 0; j < n; j++) {
				for (k = 0; k < m; k++) {
					char ch;
					scanf("%c", &ch);
					while (ch == '\n' || ch == ' ' || ch == '\0' || ch == '?')scanf("%c", &ch);
					if (ch == 'S') {
						A[i][j][k] = 1; sx = i;
						sy = j; sz = k;
					}
					else if (ch == 'P') {
						A[i][j][k] = 9; ex = i;
						ey = j; ez = k;
					}
					else if (ch == '*')A[i][j][k] = 0;
					else if (ch == '#')A[i][j][k] = 2;
					else if (ch == '.')A[i][j][k] = 1;
				}
			}
		}
		memset(ans, -1, sizeof(ans));
		memset(vis, 0, sizeof(vis));
		int flg = DFS(sx, sy, sz, 0);
		if (flg)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、付费专栏及课程。

余额充值