10分求助
查看原帖
10分求助
1038956
Lin14514楼主2024/7/28 10:51
#include <bits/stdc++.h>
using namespace std;

const int N = 1010;
char a[N][N];
int st[N][N];

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

struct node{
	int x, y, z;
};

int main(){
	int n;
	cin >> n;
	
	for (int i = 0; i < n; i++)	cin >> a[i];
	
	int sx, sy, ex, ey;
	cin >> sx >> sy >> ex >> ey;
	sx --;
	sy --;
	ex --;
	ey --;
	
	queue<node>q;
	q.push({sx, sy, 0});
	st[sx][sy] = 1;
	while(q.size()) {
		int x = q.front().x, y = q.front().y, z = q.front().z;
		q.pop();
		if (x == ex && y == ey) {
			cout << z << endl;
			return 0;
		}
		for (int i = 0; i < 4; i++) {
			int tx = x + dx[i], ty = y + dy[i];
			if (tx < 0 || tx >= n || ty < 0 || ty >= n)	continue;
			if (a[tx][ty] == '1')	continue;
			if (st[tx][ty])	continue;
			st[tx][sy] = 1;
			q.push({tx, ty, z + 1});
		}
	}
	return 0;
}

2024/7/28 10:51
加载中...