#1答案对,但是wa,关o2后全re
查看原帖
#1答案对,但是wa,关o2后全re
1290717
ridbor001楼主2024/3/18 22:49
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<algorithm>
#include<cstring>
#include<queue>

using namespace std;

typedef pair<int, int> PII;
queue<PII>q;

int n;
int st[1010][1010];
int arr[1010][1010];
int start[2];
int end1[2];

int bfs(int x1, int y1) {
	q.push({ x1,y1 });
	int x[] = { 0,1,0,-1 };
	int y[] = { 1,0,-1,0 };
	while (!q.empty()) {
		PII head = q.front();
		for (int i = 0; i < 4; i++) {
			int x_now = head.first + x[i];
			int y_now = head.second + y[i];
			if (x_now < 1 || y_now < 1 || x_now > n || y_now > n)
				continue;
			if (arr[x_now][y_now])
				continue;
			if (st[x_now][y_now])
				continue;
			q.push({ x_now,y_now });
			st[x_now][y_now] = st[head.first][head.second] + 1;
			if (x_now == end1[0] && y_now == end1[1]) {
				return st[end1[0]][end1[1]];
			}
		}
		q.pop();
	}
	return st[end1[0]][end1[1]];
}


int main() {
	cin >> n;
	cin.ignore(); // 忽略换行符

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			char temp;
			cin.get(temp);
			arr[i][j] = temp - '0';
		}
		cin.ignore(); // 忽略换行符
	}
	scanf("%d%d%d%d", start, start + 1, end1, end1 + 1);
	st[start[0]][start[1]] = 0;
	int ans = bfs(start[0], start[1]);


	printf("%d", ans);

}

为啥啊..

2024/3/18 22:49
加载中...