Created
July 22, 2020 12:58
-
-
Save vipul43/9ea89ec0eebc4f75f0a4940d4d6e1e57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #include<bits/stdc++.h> | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
#include <cstring> | |
#include <cstdlib> | |
#include <numeric> | |
#include <cmath> | |
using namespace std; | |
typedef long long ll; | |
typedef long double ld; | |
#define FOR(i, a, b) for(int i=a; i<(b); i++) | |
void readArray(int arr[], int n); | |
void dfs(int v, vector<int> &components, bool visited[], vector<int> graph[]); | |
const char nl = '\n'; | |
void solution(){ | |
//write your code here | |
int n, m; | |
cin >> n >> m; | |
char matrix[n][m]; | |
FOR(i, 0, n){ | |
FOR(j, 0, m){ | |
char ch; | |
cin >> ch; | |
matrix[i][j]=ch; | |
} | |
} | |
vector<int> graph[n*m]; | |
int count=0; | |
FOR(i, 0, n){ | |
FOR(j, 0, m){ | |
bool right=true, left=true, up=true, down=true; | |
if(matrix[i][j]=='.'){ | |
if(i+1<n && matrix[i+1][j]=='.'){ | |
graph[(i*m)+j].push_back(((i+1)*m)+j); | |
down=false; | |
} | |
if(i-1>=0 && matrix[i-1][j]=='.'){ | |
graph[(i*m)+j].push_back(((i-1)*m)+j); | |
up=false; | |
} | |
if(j+1<m && matrix[i][j+1]=='.'){ | |
graph[(i*m)+j].push_back(((i)*m)+j+1); | |
right=false; | |
} | |
if(j-1>=0 && matrix[i][j-1]=='.'){ | |
graph[(i*m)+j].push_back(((i)*m)+j-1); | |
left=false; | |
} | |
if(right && left && up && down){ | |
count+=1; | |
} | |
} | |
} | |
} | |
bool visited[n*m]; | |
memset(visited, false, sizeof(visited)); | |
vector<int> components; | |
FOR(i, 0, n*m){ | |
if(!visited[i]){ | |
components.clear(); | |
dfs(i, components, visited, graph); | |
if(components.size()>1){ | |
count+=1; | |
} | |
} | |
} | |
cout << count << nl; | |
} | |
int main() { | |
//fast i/o | |
ios_base::sync_with_stdio(false);cin.tie(NULL); | |
int T=1; | |
// cin >> T; | |
while(T--){ | |
solution(); | |
} | |
return 0; | |
} | |
/* | |
ASCII OF NUMBERS 0-9 ==> [48, 57] | |
ASCII OF UPPERCASE ALPHABETS A-Z ==> [65, 90] | |
ASCII OF LOWERCASE ALPHABETS a-z ==> [97, 122] | |
ASCII OF WHITESPACE ==> [32] | |
ASCII OF SPECIAL CHARACTERS I ==> [33, 47] | |
ASCII OF SPECIAL CHARACTERS II ==> [58, 64] | |
ASCII OF SPECIAL CHARACTERS III ==> [91, 96] | |
ASCII OF SPECIAL CHARACTERS IV ==> [123, 126] | |
SPECIAL CHARATERS I ==> [!,",#,$,%,&,',(,),*,+...,/] | |
*/ | |
void readArray(int arr[], int n){ | |
for(int i=0; i<n; ++i){ | |
cin >> arr[i]; | |
} | |
} | |
void dfs(int v, vector<int> &components, bool visited[], vector<int> graph[]){ | |
visited[v]=true; | |
components.push_back(v); | |
FOR(i, 0, graph[v].size()){ | |
if(!visited[graph[v][i]]){ | |
dfs(graph[v][i], components, visited, graph); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment