10分求助
  • 板块B3625 迷宫寻路
  • 楼主Mark_M
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/9/13 00:41
  • 上次更新2023/11/2 21:07:21
查看原帖
10分求助
365433
Mark_M楼主2023/9/13 00:41

我真的找不到我错在哪里,但就是10分

第一次:

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 105
using namespace std;
const int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int n, m;
bool vis[N][N];
char mat[N][N];
queue<pair<int, int>> q;
bool flag = false;

void bfs() {
	while (!q.empty() && flag == false) {
		pair<int, int> now;
		now = q.front();
		q.pop();
		int x = now.first;
		int y = now.second;
		vis[x][y] = true;

		for (int i = 0; i < 4; i++) {
			int nx = x + dir[i][0];
			int ny = y + dir[i][1];
			if (nx <= 0 || nx > n || ny <= 0 || ny > m || mat[nx][ny] == '#' || vis[nx][ny] == true) {
				continue;
			}
			q.push(make_pair(nx, ny));
			if (nx == n && ny == m) {
				flag = true;
			}
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mat[i][j];
		}
	}
	if (mat[n][m] == '#' || mat[1][1] == '#') {
		cout << "No" << endl;
		return 0;
	}

	q.push(make_pair(1, 1));
	bfs();

	if (flag) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}

1AC+9TLE

第二次:(31行处加了一个return,我个人觉得不影响)

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 105
using namespace std;
const int dir[4][2] = { {1,0},{0,1},{-1,0},{0,-1} };
int n, m;
bool vis[N][N];
char mat[N][N];
queue<pair<int, int>> q;
bool flag = false;

void bfs() {
	while (!q.empty() && flag == false) {
		pair<int, int> now;
		now = q.front();
		q.pop();
		int x = now.first;
		int y = now.second;
		vis[x][y] = true;

		for (int i = 0; i < 4; i++) {
			int nx = x + dir[i][0];
			int ny = y + dir[i][1];
			if (nx <= 0 || nx > n || ny <= 0 || ny > m || mat[nx][ny] == '#' || vis[nx][ny] == true) {
				continue;
			}
			q.push(make_pair(nx, ny));
			if (nx == n && ny == m) {
				flag = true;
				return;
			}
		}
	}
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> mat[i][j];
		}
	}
	if (mat[n][m] == '#' || mat[1][1] == '#') {
		cout << "No" << endl;
		return 0;
	}

	q.push(make_pair(1, 1));
	bfs();

	if (flag) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
	return 0;
}

1AC+9MLE

有没有大佬说说这是为什么?

2023/9/13 00:41
加载中...