Xlon_Rainfi @ 2023-12-13 23:08:35
#include <bits/stdc++.h>
using namespace std;
int n,m,a[1010][1010];
bool book[1010][1010];
int Next[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int dfs(int x,int y){
int r=0;
book[x][y]=1;
for(int k=0;k<4;k++){
int tx=x+Next[k][0],ty=y+Next[k][1];
if((a[x][y]==1&&a[tx][ty]==0 || a[x][y]==0&&a[tx][ty]==1)&&book[tx][ty]==0)
r+=dfs(tx,ty);
}
return r+1;
}
int main(){
memset(a,-1,sizeof(a));
cin>>n>>m;
for(int i=1;i<=n;i++){
string s;
cin>>s;
for(int j=1;j<=n;j++)
a[i][j]=s[j-1]-'0';
}
for(int i=1;i<=m;i++){
int startx,starty;
cin>>startx>>starty;
cout<<dfs(startx,starty)<<endl;
memset(book,0,sizeof(book));
}
}
by Xlon_Rainfi @ 2023-12-13 23:09:10
提交记录
by c______ @ 2023-12-14 00:29:34
dfs 要剪枝