60pts求调!!!
  • 板块B3625 迷宫寻路
  • 楼主ycx303
  • 当前回复0
  • 已保存回复0
  • 发布时间2025/1/5 21:35
  • 上次更新2025/1/6 18:46:33
查看原帖
60pts求调!!!
1439940
ycx303楼主2025/1/5 21:35

BFS写的,求解!!!!

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int N = 110;
typedef pair<int, int> PII;
int mark[N][N];
char map[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}, n, m, ans;
void bfs() {
	memset(mark, -1, sizeof mark);
	queue <PII> q;
	q.push({0, 0});
	mark[0][0] = 0;
	while (!q.empty()) {
		PII top = q.front();
		for (int i = 0; i < 4; i++) {
			int nex = top.first + dx[i], ney = top.second + dy[i];
			if (nex >= 0 && nex < n && ney >= 0 && ney < m && mark[nex][ney] == -1 && map[nex][ney] == 0) {
				mark[nex][ney] = mark[top.first][top.second] + 1;
				q.push({nex, ney});
			}
		}
		q.pop();
	}
	cout << "Yes";
}
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			scanf("%c", &map[i][j]);
		}
	}
	bfs();
	return 0;
}
2025/1/5 21:35
加载中...