이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int h, w, ans = 0;
vector<vector<char>> v;
vector<vector<bool>> visited(h, vector<bool> (w, 0));
void dfs(vector<vector<char>> &v, int i, int j, char k) {
if(i < 0 or j < 0 or i >= h or j >=w or v[i][j] == '.' or (v[i][j] != '#' and v[i][j] != k) or visited[i][j])return;
else {
if(v[i][j]!='#')
v[i][j] = '#';
else {
v[i][j] = '.';
}
visited[i][j] = true;
dfs(v, i+1, j, k);
dfs(v, i-1, j, k);
dfs(v, i, j+1, k);
dfs(v, i, j-1, k);
}
}
int main () {
cin >> h >> w;
v.resize(h, vector<char> (w));
for(int i = 0;i<h;i++) {
for(int j = 0;j<w;j++) {
cin >> v[i][j];
}
}
for(int i = 0;i<h;i++) {
for(int j = 0;j<w;j++) {
if(v[i][j] != '#' and v[i][j] != '.') {
visited.assign(h, vector<bool> (w, 0));
dfs(v, i, j, v[i][j]);
ans++;
}
}
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |