听灌佬多
  • 板块灌水区
  • 楼主yzx_yzx
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/9/8 21:16
  • 上次更新2024/9/9 12:12:44
查看原帖
听灌佬多
766148
yzx_yzx楼主2024/9/8 21:16

P1746 离开中山路

10分MLE

#include <bits/stdc++.h>
using namespace std;
long long n, x1, qy1, x2, y2;
char a[1050][1050];

struct nn {
	long long x, y, st;
};

void bfs() {
	queue<nn>p;
	p.push({x1, qy1, 0});
	while (!p.empty()) {
		nn now = p.front();
		long long x = now.x, y = now.y, st = now.st;
		p.pop();
		if (a[x][y] == '0') {
			if (x == x2 && y == y2) {
				cout << st;
				exit(0);
			}
			p.push({x + 1, y, st + 1});
			p.push({x - 1, y, st + 1});
			p.push({x, y + 1, st + 1});
			p.push({x, y - 1, st + 1});
		}


	}

}

int main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> a[i][j];
		}
	}
	cin >> x1 >> qy1 >> x2 >> y2;
	if (a[x2][y2] == '1' || a[x1][qy1] == '1') {
		cout << '0';
		return 0;
	}
	bfs();
	return 0;
}
2024/9/8 21:16
加载中...