第十届蓝桥杯 填空题之走迷宫(BFS DFS) 代码实现

010000
000100
001001
110000

01010101001011001001010110010110100100001000101010
00001000100000101010010000100000001001100110100101
01111011010010001000001101001011100011000000010000
01000000001010100011010000101000001010101011001011
00011111000000101000010010100010100000101100000000
11001000110101000010101100011010011010101011110111
00011011010101001001001010000001000101001110000000
10100000101000100110101010111110011000010000111010
00111000001010100001100010000001000101001100001001
11000110100001110010001001010101010101010001101000
00010000100100000101001010101110100010101010000101
11100100101001001000010000010101010100100100010100
00000010000000101011001111010001100000101010100011
10101010011100001000011000010110011110110100001000
10101010100001101010100101000010100000111011101001
10000000101100010000101100101101001011100000000100
10101001000000010100100001000100000100011110101001
00101001010101101001010100011010101101110000110101
11001010000100001100000010100101000001000111000010
00001000110000110101101000000100101001001000011101
10100101000101000000001110110010110101101010100001
00101000010000110101010000100010001001000100010101
10100001000110010001000010101001010101011111010010
00000100101000000110010100101001000001000000000010
11010000001001110111001001000011101001011011101000
00000110100010001000100000001000011101000000110011
10101000101000100010001111100010101001010000001000
10000010100101001010110000000100101010001011101000
00111100001000010000000110111000000001000000001011
10000001100111010111010001000110111010101101111000 

代码: 

BFS    代码

#include <iostream>
#include <algorithm>
#include <queue>
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
#define pb push_back
#define int long long
int n, m;
char s[60][60]; bool v[60][60];
struct node {
	int x, y;
	int step;
	string path;
}record[100000];
//D L R U
int dx[] = { 1,0,0,-1 };
int dy[] = { 0,-1,1,0 };

bool ok(int x, int y) {
	if (x >= 1 && x <= n && y >= 1 && y <= m && !v[x][y] && s[x][y] == '0')return true;
	return false;
}
char dir(int x, int y) {
	if (x == 1 && y == 0)return 'D';
	if (x == 0 && y == -1)return 'L';
	if (x == 0 && y == 1)return 'R';
	if (x == -1 && y == 0)return 'U';
}
int mnlen = 1e9, cnt;
queue<node>q;
void bfs() {

	while (q.size()) {
		node temp = q.front(); q.pop();
		if (temp.x == n && temp.y == m) {
			cout << temp.step << endl;
			//cout << temp.path.size() << endl;
			for (int i = 0; i < temp.path.size(); i++)cout << temp.path[i];
			return;
		}

		for (int i = 0; i < 4; i++) {
			int nowx = temp.x + dx[i];
			int nowy = temp.y + dy[i];
			string nowpath = temp.path;
			if (ok(nowx, nowy)) {
				//cout << nowx << " " << nowy << endl;
				v[nowx][nowy] = true;// for book
				node Next;
				Next.x = nowx; Next.y = nowy; Next.step = temp.step + 1; Next.path += nowpath+dir(dx[i], dy[i]);
				q.push(Next);
			}
		}
	}
}
signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> s[i] + 1;

	node fir;
	fir.x = 1; fir.y = 1; fir.step = 0; v[1][1] = true;
	q.push(fir);

	//cout << q.size() << endl;
	bfs();



	return 0;
}

DFS 代码 (会爆栈)数据范围大

#include <iostream>
#include <algorithm>
#define debug(x) cerr<<#x<<"="<<x<<endl
using namespace std;
#define pb push_back
#define int long long
int n, m;
char s[60][60]; bool v[60][60];
string path[1000]; int num; string temp; int ct[1000]; int nm;
//D L R U
int dx[] = { 1,0,0,-1 };
int dy[] = { 0,-1,1,0 };

bool ok(int x, int y) {
	if (x >= 1 && x <= n && y >= 1 && y <= m && !v[x][y] && s[x][y] == '0')return true;
	return false;
}
char dir(int x, int y) {
	if (x == 1 && y == 0)return 'D';
	if (x == 0 && y == -1)return 'L';
	if (x == 0 && y == 1)return 'R';
	if (x == -1 && y == 0)return 'U';
}
int mnlen = 1e9, cnt;

void dfs(int x, int y) {
	if (x == n && y == m) {
		ct[nm++] = cnt;
		path[num++] = temp;
		if (mnlen < cnt)mnlen = cnt;
		return;
	}

	for (int i = 0; i < 4; i++) {
		int nowx = x + dx[i];
		int nowy = y + dy[i];
		if (ok(nowx, nowy)) {
			cnt++;
			v[nowx][nowy] = true;
			temp += dir(dx[i], dy[i]);
			dfs(nowx, nowy);
			v[nowx][nowy] = false;
			temp.pop_back();
			cnt--;
		}
	}
}

signed main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++)cin >> s[i] + 1;

	/*for (int i = 1; i <= n; i++) {  //for check
		for (int j = 1; j <= m; j++) {
			if (ok(i,j))cout << 0;
			else cout << "1";
		}
		cout << endl;
	}*/

	/*cout << endl;
	for (int i = 1; i <= n; i++)  //for check
		cout << s[i] + 1 << endl;*/


	dfs(1, 1);
	v[1][1] = 1;
	for (int i = 0; i < num; i++) {
		cout << ct[i] << " ";
		for (int j = 0; j < path[i].size(); j++)cout << path[i][j];
		cout << endl;
	}

	return 0;
}

谢谢

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值