Push Box(C++)

待施工

#include <iostream>
using namespace std;

const int M = 5;
const int N = 4;

int dir[4][2] = {
	0,1,
	0,-1,
	1,0,
	-1,0
};
char walk_dir[4] = { 'E','W','S','N' };
char push_dir[4] = { 'E','W','S','N' };

//位置
struct Pos {
	int x;
	int y;
};
Pos box = { 2,1 };
Pos man = { 2,0 };
Pos ending = { 4,1 };


struct Node {
	Pos box;
	Pos man;
	int step;//箱子走的步数

	int prev;
	char push_dir;//推的方向
	int walk_step;//man走的步数
	char walk_dir[20];//
};

struct Mm {
	int x;
	int y;
	int prev;
	char dir;
};

int visited[M][N] = { 0 };
int visit[M][N][4] = { 0 };
int map[M][N] = { 0 };
Mm queuem[50] = { 0 };
Mm push;
char ans[50];
int ans_len;

Node queue[100];



void clear() {
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < N; j++) {
			visited[i][j] = 0;
		}
	}
}

int arrive(int pos, int mani, int manj) {//看man能不能到push的位置,并保存了每一步的方向的倒序和步数
	int head = 0;
	int tail = 0;
	int id;
	Mm init = { mani,manj,-1 };
	queuem[tail] = init;
	tail = (tail + 1) % 50;
	visited[mani][manj] = 1;

	while (head != tail) {
		id = head;
		Mm cur = queuem[head];
		head = (head + 1) % 50;
		if (cur.x == push.x && cur.y == push.y) {
			int k = 0;
			while (queuem[id].prev != -1) {
				queue[pos].walk_dir[k++] = queuem[id].dir;
				id = queuem[id].prev;
			}
			queue[pos].walk_step = k;
			return 1;
		}
		for (int i = 0; i < 4; i++) {
			int x = cur.x + dir[i][0];
			int y = cur.y + dir[i][1];

			if (x < 0 || x >= M || y < 0 || y >= N) continue;
			if (map[x][y] == '#') continue;
			if (visited[x][y] == 1) continue;
			if (x == box.x && y == box.y) continue;

			Mm next = { x,y,id,walk_dir[i] };
			queuem[tail] = next;
			tail = (tail + 1) % 50;
			visited[x][y] = 1;
		}
	}
	return 0;
}

void solve() {
	int head = 0;
	int tail = 0;
	int id;
	int min;

	Node init = { box,man,0,-1,push_dir[0],0 };
	queue[tail] = init;
	tail = (tail + 1) % 50;

	while (head != tail) {
		id = head;
		Node cur = queue[head];
		head = (head + 1) % 50;
		if (cur.box.x == ending.x && cur.box.y == ending.y) {
			min = cur.step;
			int k = 0;
			while (queue[id].prev != -1) {//从后往前一共走的步数
				ans[k++] = queue[id].push_dir;
				for (int i = 0; i < queue[id].walk_step; i++) {
					ans[k++] = queue[id].walk_dir[i];
				}
				id = queue[id].prev;
			}
			ans_len = k;
			break;
		}
		for (int i = 0; i < 4; i++) {
			Node next;
			next.box.x = cur.box.x + dir[i][0];
			next.box.y = cur.box.y + dir[i][1];
			next.man = cur.box;
			next.step = cur.step + 1;

			if (next.box.x < 0 || next.box.x >= M || next.box.y < 0 || next.box.y >= N) continue;
			if (map[next.box.x][next.box.y] == '#') continue;
			if (visit[next.box.x][next.box.y][i] == 1) continue;

			push.x = cur.box.x - dir[i][0];
			push.y = cur.box.y - dir[i][1];
			if (push.x < 0 || push.x >= M || push.y < 0 || push.y >= N) continue;
			if (map[push.x][push.y] == '#') continue;
			box = cur.box;
			clear();
			if (arrive(tail, cur.man.x, cur.man.y) == 0) continue;

			queue[tail].box = next.box;
			queue[tail].man = next.man;
			queue[tail].push_dir = push_dir[i];
			queue[tail].prev = id;
			queue[tail].step = next.step;
			tail = (tail + 1) % 50;
			visit[next.box.x][next.box.y][i] = 1;
		}
	}
}

int main() {
	map[1][0] = '#';
	map[1][2] = '#';
	map[3][2] = '#';
	map[3][3] = '#';
	map[4][2] = '#';
	map[4][3] = '#';

	solve();
	for (int i = ans_len; i >= 0; i--) {
		cout << ans[i] << endl;
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

河时有雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值