#2,#9,#10:TLE 本人太蒻了,不会手写队列。有提高STL队列效率的方法吗?
代码如下:
#include<iostream>
#include<cstring>
#include<queue>
using std::cin;
using std::cout;
using std::memset;
using std::queue;
struct Coordinates
{
int x,y;
};
short Xx[100010],Yy[100010];
bool a[1005][1005];
bool flag[1005][1005];
int n,m,ans;
const int dir[2][4]={{-1,0,1,0},{0,-1,0,1}};
queue<Coordinates> Q;
void BFS(int x,int y)
{
Coordinates t;
t.x=x;
t.y=y;
Q.push(t);
flag[x][y]=1;
while(!Q.empty())
{
Coordinates s;
s=Q.front();
Q.pop();
for(int i=0;i<4;++i)
{
if(s.x+dir[0][i]>0&&s.y+dir[1][i]>0&&s.x+dir[0][i]<=n&&s.y+dir[1][i]<=n&&!flag[s.x+dir[0][i]][s.y+dir[1][i]]&&a[s.x+dir[0][i]][s.y+dir[1][i]]==!a[s.x][s.y])
{
t.x=s.x+dir[0][i];
t.y=s.y+dir[1][i];
Q.push(t);
flag[t.x][t.y]=1;
++ans;
}
}
}
}
int main()
{
cin>>n>>m;
char ch;
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
{
cin>>ch;
if(ch=='1')
{
a[i][j]=1;
}
if(ch=='0')
{
a[i][j]=0;
}
}
}
for(int i=0;i<m;++i)
{
cin>>Xx[i]>>Yy[i];
}
for(int i=0;i<m;++i)
{
ans=0;
memset(flag,0,sizeof(flag));
BFS(Xx[i],Yy[i]);
++ans;
cout<<ans<<"\n";
}
return 0;
}