RE?求调
查看原帖
RE?求调
1416194
p168_0618楼主2024/8/11 21:46
#include<cstdio>
#include<sstream>
using namespace std;
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int n, m;
int a[108][108], b[108][108];
struct Tnode {
	int x, y;
	int step;
}qu[10000001];
int tail, head;
void push(int a, int b, int c) {
	qu[tail].x = a;
	qu[tail].y = b;
	qu[tail].step = c;
	tail++;
}
void pop() {
	head++;
}
Tnode top() {
	return qu[head];
}
int qsize() {
	return tail - head;
}
int bfs(int x0, int y0) {
	push(x0, y0, 0);
	a[x0][y0] = 0;
	int ans = 0;
	while (qsize() > 0) {
		int x = top().x;
		int y = top().y;
		pop();
		for (int d = 0; d < 4; d++) {
			int nx = x + dx[d];
			int ny = y + dy[d];
			if (a[nx][ny] == 1) {
				b[nx][ny] = b[x][y] + 1;
				a[nx][ny] = 0;
				push(nx, ny, b[x][y] + 1);
			}
		}
	}
	return b[n][m];
}
int main() {
	int t;
	scanf("%d", &t);
	for (int i = 1; i <= t; i++) {
		for (int j = 1; j <= t; j++) {
			char p;
			scanf("%c", &p);
			int x = p - '0';
			a[i][j] = x + 1;
		}
	}
	int x, y;
	scanf("%d%d%d%d", &x, &y, &n, &m);
	printf("%d", bfs(x, y));
	return 0;
}
2024/8/11 21:46
加载中...