12分求解
查看原帖
12分求解
1238315
aibianchendeyangyang楼主2025/7/23 21:37
#include<bits/stdc++.h>
using namespace std;
int n,m,sx,sy,ans,sum;
bool vis[110][110];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
bool in(int x,int y){
	return 0<=x&&x<n&&0<=y&&y<m;
}
bool dfs(int x,int y){
	vis[x][y]=true;
	for(int i=0;i<4;i++){
		int tx=x+dir[i][0];
		int ty=y+dir[i][1];
		if(in(tx,ty)&&!vis[tx][ty]){
			if(dfs(tx,ty)){
				return true;
			}else{
                return false;
            }
		}
	}
	return false;
}
int main(){
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>sx>>sy;
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(!vis[i][j]){
				dfs(i,j);
				sum++;
			}
		}
	}
	cout<<sum;
	return 0;
}
2025/7/23 21:37
加载中...