1 条题解

  • 0
    @ 2023-11-5 10:52:27

    关于我的样例没过,但还是AC这件事... 用DFS会更简单

    AC:

    #include <bits/stdc++.h>
    using namespace std;
    int a[1010][1010],s,c;
    void dfs(int x,int y){
    	if(not a[x][y])return;
    	s++;
    	if(not a[x + 1][y]) c++;
    	else if(not a[x - 1][y]) c++;
    	else if(not a[x][y + 1]) c++;
    	else if(not a[x][y - 1]) c++;
    	
    	a[x][y] = 0;
    	dfs(x + 1,y);
    	dfs(x - 1,y);
    	dfs(x,y + 1);
    	dfs(x,y - 1);
    	
    }
    int main(){
    	int n;
    	cin >> n;	
    	for(int i = 1;i <= n;i++){
    		for(int j = 1;j <= n;j++){
    			cin >> a[i][j];
    		}
    	}
    	for(int i = 1;i <= n;i++){
    		for(int j = 1;j <= n;j++){
    			if(a[i][j] <= 50){
    				a[i][j] = 1;
    			}
    			else{
    				a[i][j] = 0;
    			}
    		}
    	}
    	for(int i = 1;i <= n;i++){
    		for(int j = 1;j <= n;j++){
    			if(a[i][j] == 1){
    				dfs(i,j);
    			}
    		}
    	}
    	cout << s << " " << c;
    }
    
    • 1

    信息

    ID
    1079
    时间
    1000ms
    内存
    128MiB
    难度
    8
    标签
    递交数
    44
    已通过
    7
    上传者