int n, m, T;
char s[N];
int a[N][N];
bool vis[N][N];
ll dis[N][N];
queue<PII> q;
int ax[5] = {0, 0, -1, 1};
int ay[5] = {1, -1, 0, 0};
void dfs(int x, int y){
vis[x][y] = true;
dis[x][y] = 0;
q.push(PII(x,y));
for(int i = 0; i <= 3; ++ i){
int tx = x + ax[i], ty = y + ay[i];
if(a[x][y] == a[tx][ty] && !vis[tx][ty])
dfs(tx, ty);
}
}
void bfs(){
while(!q.empty()){
PII p = q.front();
q.pop();
int x = p.first, y = p.second;
for(int i = 0; i <= 3; ++ i){
int tx = x + ax[i], ty = y + ay[i];
if(a[tx][ty]!=-1 && dis[x][y] + 1 < dis[tx][ty]){
dis[tx][ty] = dis[x][y] + 1;
q.push(PII(tx, ty));
}
}
}
}
int main()
{
memset(a,-1,sizeof(a));
scanf("%d%d%d",&n,&m,&T);
for(int i = 1; i <= n; ++ i){
scanf("%s",s + 1);
for(int j = 1; j <= m; ++ j){
a[i][j] = s[j] - ‘0‘;
dis[i][j] = INF;
}
}
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j)
for(int k = 0; k <= 3; ++ k){
int x = i + ax[k];
int y = j + ay[k];
if(a[x][y] == a[i][j]) {
dfs(i, j);
break;
}
}
bfs();
ll x, y, p;
while(T --){
scanf("%lld%lld%lld",&x,&y,&p);
if(dis[x][y] >= p) printf("%d\n",a[x][y]);
else printf("%d\n", (p - dis[x][y]) % 2 ? a[x][y] ^ 1 : a[x][y]);
}
return 0;
}
Codeforces-1350 E.Orac and Game of Life
原文:https://www.cnblogs.com/A-sc/p/12884190.html