ZOJ's largest island (DFS)

4386: Largest Island

Analysis:
Classic DFS seeks regional issues.

Code:

#include <bits/stdc++.h>
using namespace std;
char sea[510][510] = {
    
     0 };
int walkx[3] = {
    
     -1, 0, 1 }, walky[3] = {
    
     -1, 0, 1 }, ans1, ans2, temp;

void dfs(int x, int y)
{
    
    
	temp++;
	sea[x][y] = '0';
	for (int i = 0; i<3; i++){
    
    
		for (int j = 0; j<3; j++){
    
    
			if (sea[x + walkx[i]][y + walky[j]] == '1')
				dfs(x + walkx[i], y + walky[j]);
		}
	}
	ans2 = max(ans2, temp);
}
int main()
{
    
    
	int n, m, t;
	cin >> n >> m >> t;
	for (int i = 1; i <= n; i++){
    
    
		for (int j = 1; j <= m;){
    
    
			char a = getchar();
			if (a == '0' || a == '1'){
    
    
				sea[i][j] = a;
				j++;
			}
		}
	}
	for (int i = 1; i <= n; i++){
    
    
		for (int j = 1; j <= m; j++){
    
    
			if (sea[i][j] == '1'){
    
    
				ans1++;
				dfs(i, j);
				temp = 0;
			}
		}
	}
	cout << ans1 << ' ' << ans2*t;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_43700916/article/details/88651253