AC 了,但有疑问
查看原帖
AC 了,但有疑问
993779
FurippuWRY楼主2024/7/12 20:08

(以下为错误代码)为什么这个代码每次输出的结果都是随机的?

#include <bits/stdc++.h>
#define f first
#define s second
#define debug cout<<"IAKIOI"

using namespace std;

int n, m, x, y;
int a[1001][1001];
int dis[1001][1001];

int mx[] = {2, 2, 1, 1, -1, -1, -2, -2, 2, -2, 2, -2};
int my[] = {1, -1, 2, -2, 2, -2, 1, -1, -2, -2, 2, 2};

bool vis[1001][1001];
queue<pair<int, int>> p;

int bfs(int x, int y) {
	memset(dis, -1, sizeof dis);
	memset(vis, 0, sizeof vis);
	p.push({x, y});
	dis[x][y] = 0;
	while (!p.empty()) {
		auto t = p.front();
		p.pop();
		for (int i = 0; i < 12; ++i) {
			int q = t.f + mx[i], w = t.s + my[i];
			if (q < 1 || q > n || w < 1 || w > n || vis[q][w]) continue;
			if (q == 1 && w == 1) {
				debug;
				return dis[q][w];
			}
			dis[q][w] = dis[t.f][t.s] + 1;
			vis[q][w] = 1;
			p.push({q, w});
		}
	}
}

int main() {
	
	cin >> n >> m >> x >> y;
	cout << bfs(n, m) << '\n';
	while (!p.empty()) p.pop();
	cout << bfs(x, y);
}
2024/7/12 20:08
加载中...