Lazy_make_name @ 2024-11-09 21:10:01
我的程序函数有一点点多,请见谅
#include<bits/stdc++.h>
using namespace std;
int n,m,d[1002][1002],z,l1,l2;
queue<int> a;
queue<int> b;
string l;
void Handle_strings(int a){
for(int i=1;i<=l.size();i++){
d[a][i]=l[i]-'0';
}
}
void Input(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>l;
Handle_strings(i);
}
}
void Count_the_number(){
z=b.size();
while(!b.empty()){
d[b.front()/2000][b.front()%2000]=0-z;
b.pop();
}
}
void Judge_above(){
if(d[l1][l2]+d[l1+1][l2]==1){
a.push(a.front()+1000);
}
}
void Judge_below(){
if(d[l1][l2]+d[l1-1][l2]==1){
a.push(a.front()-1000);
}
}
void Judge_the_left(){
if(d[l1][l2]+d[l1][l2-1]==1){
a.push(a.front()-1);
}
}
void Judge_the_right(){
if(d[l1][l2]+d[l1][l2+1]==1){
a.push(a.front()+1);
}
}
void Judge(){
l1=a.front()/2000;
l2=a.front()%2000;
d[l1][l2]=-1;
Judge_above();
Judge_below();
Judge_the_left();
Judge_the_right();
}
void bfs(){
while(!a.empty()){
Judge();
b.push(a.front());
a.pop();
}
Count_the_number();
}
void Deal_with_one(int i,int j){
if(d[i][j]>=0){
a.push(i*2000+j);
z=0;
bfs();
}
}
void traverse(){
memset(d,-1,sizeof(d));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(d[i][j]>-1)
Deal_with_one(i,j);
}
}
}
void answer(){
for(int i=1;i<=m;i++){
cin>>l1>>l2;
cout<<0-d[l1][l2]<<endl;
}
}
int main(){
Input();
traverse();
answer();
return 0;
}