3.1. 15이원준 ¶
#include<iostream> #include<vector> #include<algorithm> using namespace std; int arr[27][27] = { 0, }; int N, num = 0; vector<int> v; void search(int i, int j) { v[num]++; arr[i][j] = -1; if(arr[i+1][j] == 1) search(i + 1, j); if(arr[i-1][j] == 1) search(i - 1, j); if(arr[i][j+1] == 1) search(i, j + 1); if(arr[i][j-1] == 1) search(i, j - 1); } int main() { cin >> N; for (int i = 0; i<27; i++) { for (int j = 0; j<27; j++) { arr[i][j] = 0; } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { int tmp; scanf("%1d", &tmp); arr[i][j] = tmp; } } for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (arr[i][j] == 1) { v.push_back(0); search(i, j); num++; } } } sort(v.begin(), v.end()); cout<<v.size()<<endl; for (int i = 0; i<v.size(); i++) { cout << v[i] << endl; } }
3.2. 박인서 ¶
#include <iostream> #include <vector> #include <algorithm> char a[30][30]; bool visit[30][30]; int n; std::vector<int> res; int dfs (int i,int j){ int cnt=1; visit[i][j]=true; if (i!=1 && a[i-1][j]=='1' && !visit[i-1][j]) cnt+=dfs(i-1,j); if (i!=n && a[i+1][j]=='1' && !visit[i+1][j]) cnt+=dfs(i+1,j); if (j!=1 && a[i][j-1]=='1' && !visit[i][j-1]) cnt+=dfs(i,j-1); if (j!=n && a[i][j+1]=='1' && !visit[i][j+1]) cnt+=dfs(i,j+1); return cnt; } int main (){ std::cin>>n; for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++){ std::cin>>a[i][j]; } } for (int i=1;i<=n;i++){ for (int j=1;j<=n;j++){ if (a[i][j]=='1' && !visit[i][j]) res.push_back(dfs(i,j)); } } std::cout<<res.size()<<std::endl; std::sort(res.begin(),res.end()); for(int i=0;i<res.size();i++) std::cout<<res[i]<<std::endl; return 0; }