Skip to content

Instantly share code, notes, and snippets.

@Sharma96 Sharma96/q.cpp
Created Sep 10, 2017

Embed
What would you like to do?
#include <iostream>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <utility>
#include <cstdio>
#define n 1001
using namespace std;
///char arr[1001][1001];
bool is_valid(int x,int y,int n1){
if(x<0||y<0||x>=n1||y>=n1){
return false;
}
return true;
}
int cal_(int i,int j,char arr[n][n],int n1){
pair<int,int> p(i,j);
queue<pair<int,int> > q;
//for()
q.push(p);
int ans_ = 0;
//visited
bool visited[n1][n1] = {{0}};
//visited[][]
int diff[][2] = {{-1,0},{0,1},{1,0},{0,-1}};
while(!q.empty()){
pair<int,int> curr = q.front();
q.pop();
int x = curr.first;
int y = curr.second;
//cout<<"x and y"<<x<<" "<<y<<endl;
visited[x][y] = 1;
for(int k=0;k< 4;k++){
int newx = x + diff[k][0];
int newy = y + diff[k][1];
//cout<<newx<<" "<<newy<<" arr->"<<" "<<arr[newx][newy]<<endl;
if(is_valid(newx,newy,n1)){
if( arr[newx][newy] == '.' && visited[newx][newy] == 0){
visited[newx][newy] = 1;
//cout<<newx<<" "<<newy<<endl;
ans_++;
pair<int,int> p(newx,newy);
q.push(p);
}
}
}
}
//cout<<ans_+1<<endl;
//if(ans_!=0)
return ans_+1;
//else
// return 0;
}
int main(){
//cout << "Hello World!" << endl;
std::ios::sync_with_stdio(false);
int n1;
//char arr[n][n];
//char arr_copy[n][n];
cin>>n1;
// rtrt = n1;
char arr[n][n];
char arr_copy[n][n];
//scanf("%d",&n1);
int i,j;
for(i=0;i<n1;i++){
for(j=0;j<n1;j++){
char ch;
cin>>ch;
//fflush(stdin);
//scanf(" %c",&ch);
arr[i][j] = ch;
arr_copy[i][j] = ch;
}
}
if(n1 == 1){
cout<<0;
//printf("0");
return 0;
}
int ans_ = 0;
for(i=0;i<n1;i++){
for(j=0;j<n1;j++){
if(arr[i][j] == '*'){
// flag = 1;
ans_ += cal_(i,j,arr_copy,n1);
//arr[i][j] = '-';
//cout<<1<<" ";
}
}
}
cout<<ans_;
//printf("%d",ans_);
//cal_(arr,)
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.