迷宫问题(搜索与图论、bfs)

输入一个 n X n 二维数组

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入:
一个n × n的二维数组,表示一个迷宫。数据保证有唯一解。

输出:
左上角到右下角的最短路径,格式如样例所示。

样例输入:

5
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

输出:

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

 样例输入:

10
0 1 0 0 0 0 0 1 0 1
0 1 0 0 0 0 0 0 1 0 
0 1 0 0 1 0 1 1 0 0
0 0 1 0 0 0 1 1 0 0
0 0 1 0 0 1 1 0 0 1
0 0 0 1 0 0 0 1 1 1
0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 1 0 1 1
1 1 0 1 0 0 0 0 0 0 
0 0 0 0 0 0 1 1 0 0

输出:

(0, 0)
(1, 0)
(2, 0)
(3, 0)
(4, 0)
(5, 0)
(6, 0)
(7, 0)
(7, 1)
(7, 2)
(7, 3)
(7, 4)
(8, 4)
(8, 5)
(8, 6)
(8, 7)
(8, 8)
(9, 8)
(9, 9)

用一个pre数组记录前继节点即可,最后要倒叙输出路径

#include<iomanip>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);

const int N = 1e3 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
using pii = pair<int, int>;

int n, m, g[N][N];
pii pre[N][N];
int st[N][N];

void init() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> g[i][j];
		}
	}
	return;
}

void bfs() {
	queue<pii> qu;
	qu.push({1, 1});
	st[1][1] = 1;
	while (qu.size()) {
		int x = qu.front().first;
        int y = qu.front().second;
		//cout << x << " " << y << endl;
		qu.pop();
		//if (x == n && y == n) {
		//	cout << st[x][y] << "\n";
		//	return;
		//}
		for (int i = 0; i < 4; i++) {
			int nx = x + dir[i][0], ny = y + dir[i][1];
			if (!nx || !ny || nx > n || ny > n || g[nx][ny] || st[nx][ny]) continue;
			st[nx][ny] = st[x][y] + 1;
			qu.push({nx, ny});
			pre[nx][ny] = { x, y };
		}
	}
}

void solve() {
	bfs();
	vector<pii> step;
	pii ed = { n, n };
	while (ed.first || ed.second) {
		step.push_back({ed.first - 1, ed.second - 1});
		ed = pre[ed.first][ed.second];
	}
	for (auto rre = step.rbegin(); rre != step.rend(); rre++) {
		cout << "(" << rre->first << ", " << rre->second << ")\n";
	}
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
	int TT = 1;
	//cin >> TT;
	for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}
	return 0;
}

#include<bits/stdc++.h>
using namespace std; using ll = long long;
int dir[4][2] = { 1, 0, -1, 0, 0, 1, 0, -1 };
//using lll = __int128; template <class T> istream& read(T& x, istream& cin = std::cin) { T num = 0; bool f = 0; char ch = 0; while (!isdigit(ch)) { f |= ch == '-'; if (!cin.get(ch)) return cin; }while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch ^ 48); if (!cin.get(ch)) break; }x = f ? -num : num; return cin; }template <class T> ostream& write(T x, ostream& cout = std::cout) { if (x < 0) cout.put('-'), x = -x; if (x > 9) write(x / 10); cout.put(x % 10 + '0'); return cout; }ostream& operator<<(ostream& cout, lll x) { write(x); return cout; }istream& operator>>(istream& cin, lll& x) { return read(x); }bool check(int i, int j);
bool check(int i, int j);

const int N = 1e3 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f;
using pii = pair<int, int>;

int n, m, g[N][N];
pii pre[N][N];
int st[N][N];

void init() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> g[i][j];
		}
	}
	return;
}

void bfs() {
	queue<pii> qu;
	qu.emplace(1, 1);
	st[1][1] = 1;
	while (qu.size()) {
		auto [x, y] = qu.front();
		//cout << x << " " << y << endl;
		qu.pop();
		//if (x == n && y == n) {
		//	cout << st[x][y] << "\n";
		//	return;
		//}
		for (int i = 0; i < 4; i++) {
			int nx = x + dir[i][0], ny = y + dir[i][1];
			if (!nx || !ny || nx > n || ny > n || g[nx][ny] || st[nx][ny]) continue;
			st[nx][ny] = st[x][y] + 1;
			qu.emplace(nx, ny);
			pre[nx][ny] = { x, y };
		}
	}
}

void solve() {
	bfs();
	vector<pii> step;
	pii ed = { n, n };
	while (ed.first || ed.second) {
		step.emplace_back(ed.first - 1, ed.second - 1);
		ed = pre[ed.first][ed.second];
	}
	for (auto rre = step.rbegin(); rre != step.rend(); rre++) {
		cout << "(" << rre->first << ", " << rre->second << ")\n";
	}
	return;
}

int main(void) {
	ios::sync_with_stdio(0); cin.tie(0); cout << setprecision(6) << fixed;
	int TT = 1;
	//cin >> TT;
	for (int ii = 1; ii <= TT; init(), solve(), ii++, cout << "\n") {}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值