Skip to content

Commit

Permalink
Image Segmentation #1 Solution Hackerrank
Browse files Browse the repository at this point in the history
  • Loading branch information
ramesh-chandra-saini authored Mar 4, 2017
1 parent 6eb11a1 commit 2e17c93
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Image Segmentation #1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
bool isBoundary(int i,int j,int N,int M){
return i<0 || i>=N || j<0 || j>=M;
}
void solve(int i,int j,vector<string>&image){
int N = image.size();
int M = image[0].length();
for(int a=-1;a<=1;a++){
for(int b=-1;b<=1;b++){
if((a==0 && b==0 )||(abs(a)==1 && abs(b)==1))
continue;
if(!isBoundary(i+a,j+b,N,M) && image[i+a][j+b]=='1'){
image[i+a][j+b]='0';
solve(i+a,j+b,image);
}
}
}
}
int main() {
vector<string> image={
"000110001010",
"111011110001",
"111010010010",
"100000000100"
};

int segments = 0;
for(int i=0;i<image.size();i++){
for(int j=0;j<image[i].length();j++){
if(image[i][j]=='0')
continue;
segments++;
image[i][j]='0';//just to ensure
solve(i,j,image);
}
}
printf("%d\n",segments);
return 0;
}

0 comments on commit 2e17c93

Please sign in to comment.