putixiaosheng @ 2024-08-22 16:20:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX 1001
char arr[MAX][MAX];
int mark[MAX][MAX];
int n;
int count = 0;
void dfs(int x,int y){
mark[x][y] = 1;
count++;
if(x - 1>=0&&mark[x-1][y]==0&&arr[x-1][y]+arr[x][y] - '0' - '1'==0){
dfs(x-1,y);
}
if(x + 1<n&&mark[x+1][y]==0&&arr[x+1][y]+arr[x][y] - '0' - '1'==0){
dfs(x+1,y);
}
if(y - 1>=0&&mark[x][y-1]==0&&arr[x][y-1]+arr[x][y] - '0' - '1'==0){
dfs(x,y-1);
}
if(y + 1<n&&mark[x][y+1]==0&&arr[x][y+1]+arr[x][y] - '0' - '1'==0){
dfs(x,y+1);
}
}
int main(){
int m;
scanf("%d %d\n",&n,&m);
int ar[m];
memset(arr,0,sizeof(arr));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%c",&arr[i][j]);
}
getchar();
}
for(int i=0;i<m;i++){
int x,y;
scanf("%d %d",&x,&y);
memset(mark,0,sizeof(mark));
dfs(x-1,y-1);
ar[i] = count;
count = 0;
}
for(int i=0;i<m;i++){
printf("%d\n",ar[i]);
}
return 0;
}
by putixiaosheng @ 2024-08-22 22:13:24
https://www.luogu.com.cn/record/174247025 改成这样了,有超时
by putixiaosheng @ 2024-08-22 22:18:31
前面会报RE是因为输入的问题,改成scanf("%d",arr[i])就可以了,不知道为什么