MLE 0 分,玄关
  • 板块P1141 01迷宫
  • 楼主jinyanshao
  • 当前回复2
  • 已保存回复2
  • 发布时间2024/11/1 16:13
  • 上次更新2024/11/1 19:41:09
查看原帖
MLE 0 分,玄关
946416
jinyanshao楼主2024/11/1 16:13
#include<bits/stdc++.h>
using namespace std;
//#define int long long
int n, m, id;
int mapa[1005][1005];
int vis[1005][1005];
int sx, sy;
int ans[10005];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct node {
	int x, y;
};
void bfs(int x, int y) {
	queue<node> q;
	q.push({x, y});
	vis[x][y] = 1;
	while (!q.empty()) {
		node now = q.front();
		q.pop();
//		cout << now.x << " " << now.y << endl;
		for (int i = 0; i < 4; i++) {
			int xx = now.x + dx[i];
			int yy = now.y + dy[i];
			if (xx < 1 || xx > n || yy < 1 || yy > n || vis[xx][yy] == 1 || mapa[now.x][now.y] == mapa[xx][yy])continue;
			ans[id]++;
//			cout << xx << " " << yy << endl;
			vis[xx][yy] = id;
			q.push({xx, yy});
		}
	}
}
signed main() {
//	freopen("P1141_1.in", "r", stdin);
//	freopen("A.out", "w", stdout);
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
	cin >> n >> m;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			char c;
			cin >> c;
			mapa[i][j] = c - '0';
		}
	}
//	for (int i = 1; i <= n; i++) {
//		for (int j = 1; j <= m; j++) {
//			cout << mapa[i][j];
//		}
//		cout << endl;
//	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (vis[i][j] == 0) {
				id++;
				bfs(i, j);
			}
		}
	}
	while (m--) {
		cin >> sx >> sy;
//		cout << endl;
		cout << ans[vis[sx][sy]] + 1 << endl;
//		cout << endl;
	}
	return 0;
}
//3 3
//010
//101
//010
//1 1
//1 2
//3 1
2024/11/1 16:13
加载中...