문제 링크 - https://www.acmicpc.net/problem/1303
🌱 문제
🌱 코드
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int n,m;
char arr[100][100];
bool check[100][100];
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
vector<int> blue;
vector<int> white;
int num;
int our_white;
int your_blue;
int bfs(int x, int y,char team){
queue<pair<int,int>> q;
check[x][y]=true;
q.push(make_pair(x,y));
int cnt=1;
while(!q.empty()){
int cx=q.front().first;
int cy=q.front().second;
q.pop();
for(int i=0; i<4; i++ ){
int nx=cx+dx[i];
int ny=cy+dy[i];
if(nx>=0 && nx<m && ny>=0 && ny<n){
if(check[nx][ny]==false && arr[nx][ny]==team){
cnt++;
check[nx][ny]=true;
q.push(make_pair(nx,ny));
}
}
}
}
return cnt;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
cin>>arr[i][j];
}
}
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(check[i][j]==false && arr[i][j]=='W'){
num=bfs(i,j,'W');
white.push_back(num);
}else if(check[i][j]==false && arr[i][j]=='B'){
num=bfs(i,j,'B');
blue.push_back(num);
}
}
}
for(int i=0; i<blue.size(); i++){
your_blue+=(blue[i]*blue[i]);
}
for(int i=0; i<white.size(); i++){
our_white+=(white[i]*white[i]);
}
cout<<our_white<<" "<<your_blue<<"\n";
}