bfs WA45求调 玄关
  • 板块P2130 狂奔的Wzf
  • 楼主fly_x
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/10/10 14:05
  • 上次更新2024/10/10 16:39:24
查看原帖
bfs WA45求调 玄关
365113
fly_x楼主2024/10/10 14:05
#include <bits/stdc++.h>
using namespace std;
const int inf = 114514191;
int n, m, ans, s1[1005][1005], s2[1005][1005];
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
int base[10] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
char a[1005][1005]; bool v[1005][1005];
void bfs(int xs, int ys, int ss){
	queue<int> qx, qy, qs; v[xs][ys] = 1;
	qx.push(xs), qy.push(ys), qs.push(ss);
	while(!qx.empty()){
		int x = qx.front(), y = qy.front(), s = qs.front();
		qx.pop(), qy.pop(), qs.pop();
		if (a[x][y] == '#'){ans = s; return ;}
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 10; j++){
				int xx = x+dx[i]*base[j];
				int yy = y+dy[i]*base[j];
				if (xx < 1 || xx > n || yy < 1 || yy > m) continue;
				if (v[xx][yy]) continue;
				if (i < 2 && s2[x][y] == s2[xx][yy]){
					v[xx][yy] = 1;
					qx.push(xx), qy.push(yy), qs.push(s+1);
				}
				if (i > 1 && s1[x][y] == s1[xx][yy]){
					v[xx][yy] = 1;
					qx.push(xx), qy.push(yy), qs.push(s+1);
				}
			}
	}
}
int main() {
	scanf("%d%d", &n, &m); ans = inf;
	for (int i = 1; i <= n; i++) scanf("%s", a[i]+1);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++){
			s1[i][j] += s1[i][j-1];
			s2[i][j] += s2[i-1][j];
			if (a[i][j] == 'X')
				++s1[i][j], ++s2[i][j];
		}
	v[1][1] = 1;
	bfs(1, 1, 0);
	if (ans == inf) ans = -1;
	printf("%d", ans);
	return 0;
}

2024/10/10 14:05
加载中...