百练OJ-迷宫问题(记录BFS的路径,两个方法,最好用栈,不用栈可能会有bug,虽然也可以AC....)

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
int a[7][7];
struct node
{
	int x;
	int y;
	//int pastx;
	//int pasty;
};
int main() {
	memset(a,-1,sizeof(a));
	for (int i = 1; i <= 5; i++) {
		for (int j = 1; j <= 5; j++) {
			cin >> a[i][j];
		}
	}
	queue<node>q;
	node head;
	head.x = 1;
	head.y = 1;
	q.push(head);
	while (!q.empty()){
		node temp = q.front();
		node temp0;
		q.pop();
		if (temp.x == 5 && temp.y == 5) {
			break;
		}
		if (a[temp.x - 1][temp.y] == 0) {
			a[temp.x][temp.y] = 2;//2为向上移动
			temp0.x = temp.x - 1;
			temp0.y = temp.y;
			q.push(temp0);
		}
		if (a[temp.x + 1][temp.y] == 0) {
			a[temp.x][temp.y] = 3;//3为向下移动
			temp0.x = temp.x + 1;
			temp0.y = temp.y;
			q.push(temp0);
		}
		if (a[temp.x][temp.y + 1] == 0) {
			a[temp.x][temp.y] = 4;//4为向右移动
			temp0.x = temp.x;
			temp0.y = temp.y+1;
			q.push(temp0);
		}
		if (a[temp.x][temp.y-1] == 0) {
			a[temp.x][temp.y] = 5;//5为向左移动
			temp0.x = temp.x;
			temp0.y = temp.y-1;
			q.push(temp0);
		}
	}
	//cout << endl;
	//for (int i = 1; i <= 5; i++) {
	//	for (int j = 1; j <= 5; j++) {
	//		cout<<a[i][j]<<" ";
	//	}
	//	cout << endl;
	//}
	while (head.x != 5 || head.y != 5) {
		printf("(%d, %d)\n", head.x - 1, head.y - 1);
		if (a[head.x][head.y] == 2)
			head.x -= 1;
		else if (a[head.x][head.y] == 3)
			head.x += 1;
		else if (a[head.x][head.y] == 4)
			head.y += 1;
		else if(a[head.x][head.y] == 5)
			head.y -= 1;
	}
	printf("(%d, %d)\n", 4, 4);
	return 0;
}
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<stack>
using namespace std;
int a[7][7];
struct node
{
	int x;
	int y;
	//int pastx;
	//int pasty;
};
int main() {
	memset(a,-1,sizeof(a));
	for (int i = 1; i <= 5; i++) {
		for (int j = 1; j <= 5; j++) {
			cin >> a[i][j];
		}
	}
	queue<node>q;
	node head;
	head.x = 1;
	head.y = 1;
	q.push(head);
	while (!q.empty()){
		node temp = q.front();
		node temp0;
		q.pop();
		if (temp.x == 5 && temp.y == 5) {
			break;
		}
		if (a[temp.x - 1][temp.y] == 0) {
			a[temp.x-1][temp.y] = 2;//2为向上移动
			temp0.x = temp.x - 1;
			temp0.y = temp.y;
			q.push(temp0);
		}
		if (a[temp.x][temp.y + 1] == 0) {
			a[temp.x][temp.y+1] = 4;//4为向右移动
			temp0.x = temp.x;
			temp0.y = temp.y+1;
			q.push(temp0);
		}
		if (a[temp.x + 1][temp.y] == 0) {
			a[temp.x+1][temp.y] = 3;//3为向下移动
			temp0.x = temp.x + 1;
			temp0.y = temp.y;
			q.push(temp0);
		}
		if (a[temp.x][temp.y-1] == 0) {
			a[temp.x][temp.y-1] = 5;//5为向左移动
			temp0.x = temp.x;
			temp0.y = temp.y-1;
			q.push(temp0);
		}
	}
	cout << endl;
	for (int i = 1; i <= 5; i++) {
		for (int j = 1; j <= 5; j++) {
			cout<<a[i][j]<<" ";
		}
		cout << endl;
	}
	head.x = 5; head.y = 5;
	stack<node>result;
	result.push(head);
	while (head.x != 1 || head.y != 1) {
		//printf("(%d, %d)\n", head.x - 1, head.y - 1);
		if (a[head.x][head.y] == 2)
			head.x += 1;
		else if (a[head.x][head.y] == 3)
			head.x -= 1;
		else if (a[head.x][head.y] == 4)
			head.y -= 1;
		else if(a[head.x][head.y] == 5)
			head.y += 1;
		result.push(head);
	}
	while (!result.empty()) {
		head = result.top();
		result.pop();
		printf("(%d, %d)\n", head.x - 1, head.y - 1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值