自己简单写了一遍bfs去做,有wa有tle,求助
#include<bits/stdc++.h>
using namespace std;
long long s,n,m,x,y;
bool labyrinth[1145][1145],qc[1145][1145];
int dx[4]={1,0,-1,0},dy[4]={0,-1,0,1};
string a;
struct point
{
long long x,y;
}now,temp;
queue<point> r;
long long startx,starty;
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>a;
for(int j=1;j<=n;j++) labyrinth[i][j]=a[j-1]-'0';
}
while(m--)
{
cin>>startx>>starty;
now.x=startx,now.y=starty;
r.push(now);
s=0;
while(!r.empty())
{
x=r.front().x;
y=r.front().y;
for(int i=0;i<4;i++)
{
int tx=x+dx[i],ty=y+dy[i];
if(tx<1||tx>n||ty<1||ty>n) continue;
if(labyrinth[tx][ty]==labyrinth[x][y]) continue;
if(qc[tx][ty]) continue;
qc[tx][ty]=1;
temp.x=tx,temp.y=ty;
r.push(temp);
s++;
}
r.pop();
}
cout<<s<<endl;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++) qc[i][j]=0;
}
}