萌新60分WA求助,求调
  • 板块P1141 01迷宫
  • 楼主bhr123456
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/5/19 13:04
  • 上次更新2024/5/19 15:19:56
查看原帖
萌新60分WA求助,求调
999563
bhr123456楼主2024/5/19 13:04
#include <bits/stdc++.h>
using namespace std;
int n, m, x, y;
char g[2000][2000];
bool vis[2000][200];
int col[2000][3], cnt[2000000], res = 1;
int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void dfs(int x, int y) {
	for (int i = 0; i < 4; i++) {
		int xx = x + dir[i][0];
		int yy = y + dir[i][1];
		if (vis[xx][yy] == 0 && xx <= n && yy <= n && xx > 0 && yy > 0 && g[xx][yy] != g[x][y]) {
			vis[xx][yy] = 1;
			cnt[res]++;
			col[xx][yy] = res;
			dfs(xx, yy);
		}
	}
}
int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			cin >> g[i][j];
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (vis[i][j] == 0) {
				vis[i][j] = 1;
				col[i][j] = res;
				cnt[res] = 1;
				dfs(i, j);
				res++;
			}
		}
	}
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		cout << cnt[col[x][y]] << endl;
	}
	return 0;
}

萌新60分WA求助,求调

2024/5/19 13:04
加载中...