仔细考虑题意:
一个格子会变化:其四周有与其相同颜色的格子。然后他们一起变化。下一轮他们还是一样,还会一起变化。
而对于其周围与它不同颜色的格子。再它颜色变化后,会变得与它颜色相同。也就是说,下一轮会他们会一起变化。
所以只要有一对相邻相同的格子,就会至多通过2000次传染,使得所有格子都变得每轮颜色取反。
我们开一各数字dis[x][y]表示,x,y在第几轮会开始发生变化,一个格子周围有与他相同的格子,初始值就为1,否则为INF。
只要以初始就变化的点开始BFS,更新完所有点即可。
询问时,要么与初始相同要么不同,判一下即可。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ls (o<<1)
#define rs (o<<1|1)
#define pb push_back
const double PI= acos(-1.0);
const int M = 1e5+7;
/*
int head[M],cnt;
void init(){cnt=0,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,val;}ee[M*2];
void add(int x,int y){ee[++cnt].nxt=head[x],ee[cnt].to=y,head[x]=cnt;}
*/
char s[1010][1010];
ll dis[1010][1010];//第i,j格子,经过几次迭代会开始变色
int d[8]={1,0,-1,0,0,1,0,-1};
struct node