lw393 @ 2024-06-24 22:01:22
#include<iostream>
using namespace std;
const int N = 1e4 + 5;
int n, g[N][N], d[N][N], vis[N][N], dir[4][2]{{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int cnt = 0;
void dfs(int x, int y){
d[x][y] = ++cnt;
for(int i = 0; i < 4; i++){
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(nx > 0 && nx <= n && ny > 0 && ny <= n && vis[nx][ny] == 0 && g[x][y] ^ g[nx][ny] == 1){
vis[nx][ny] = 1;
dfs(nx, ny);
}
}
}
void sfd(int x, int y){
d[x][y] = cnt;
for(int i = 0; i < 4; i++){
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if(nx > 0 && nx <= n && ny > 0 && ny <= n && vis[nx][ny] == 1 && g[x][y] ^ g[nx][ny] == 1){
vis[nx][ny] = 2;
sfd(nx, ny);
}
}
}
int main(){
int m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
char c;
cin >> c;
g[i][j] = c - '0';
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
cnt = 0;
if(!vis[i][j]){
vis[i][j] = 1;
dfs(i, j);
vis[i][j] = 2;
sfd(i, j);
}
}
}
for(int i = 0; i < m; i++){
int x, y;
cin >> x >> y;
cout << d[x][y] << '\n';
}
return 0;
}
评测记录https://www.luogu.com.cn/record/163025292
by Qiu101029 @ 2024-06-26 15:09:38
有没有一种可能,1e4+5=10005?
@lw393