This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int N = 1e3+5;
int grid[N][15], visited[N][15];
int n,m, ans = 0;
void dfs(int x, int y){
visited[x][y] = 1;
bool ver = 0, hor = 0;
for(int dx : {-1,0,1}){
for(int dy : {-1,0,1}){
if(abs(dx)==abs(dy)) continue;
if(x+dx<0||x+dx>=n||y+dy<0||y+dy>=m) continue;
if(!grid[x+dx][y+dy]) continue;
if(dx) ver = 1;
if(dy) hor = 1;
if(visited[x+dx][y+dy]) continue;
dfs(x+dx,y+dy);
}
}
if(ver&&hor){
ans++;
}
}
signed main(){
fastio
cin >> n >> m;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
char c;
cin >> c;
if(c=='#') grid[i][j] = 1;
}
}
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(!visited[i][j]&&grid[i][j]) ans++, dfs(i,j);
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |