蒟蒻 60pts 求调!玄两关
查看原帖
蒟蒻 60pts 求调!玄两关
902351
Little_x_starTYJ楼主2024/9/5 11:15

TLE on #3,#9,#10,#11

#include <bits/stdc++.h>
using namespace std;
int n, m;
bool vis[1010][1010];
char c[1010][1010];
int a[1010][1010];
struct node {
	int x, y;
};
queue<node> q;
int dx[] = {0, -1, 0, 1};
int dy[] = {-1, 0, 1, 0};
inline int bfs(int x, int y) {
	int res = 1;
	q.push({x, y});
	vis[x][y] = 1;
	while(!q.empty()) {
		node t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];
			if (xx >= 1 && xx <= n && yy >= 1 && yy <= n && (c[xx][yy] == '0' && c[t.x][t.y] == '1' || c[xx][yy] == '1' && c[t.x][t.y] == '0') && !vis[xx][yy]) {
				q.push({xx, yy});
				res++;
				vis[xx][yy] = 1;
			}
		}
	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			if (vis[i][j])
				a[i][j] = res;
			vis[i][j] = false;
		}
	}
	return res;
}
int main() {
	ios::sync_with_stdio(false);
	ios_base::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++)
			cin >> c[i][j];
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (!a[i][j])
				a[i][j] = bfs(i, j);
	while (m--) {
		int x, y;
		cin >> x >> y;
		cout << a[x][y] << "\n";
	}
}
2024/9/5 11:15
加载中...