From 2e17c9358da4f18c5e7a4b1ecb1372fbd2e970c6 Mon Sep 17 00:00:00 2001 From: Ramesh Chandra Date: Sat, 4 Mar 2017 11:47:02 +0530 Subject: [PATCH] Image Segmentation #1 Solution Hackerrank --- Image Segmentation #1.cpp | 44 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Image Segmentation #1.cpp diff --git a/Image Segmentation #1.cpp b/Image Segmentation #1.cpp new file mode 100644 index 0000000..c233756 --- /dev/null +++ b/Image Segmentation #1.cpp @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include +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&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 image={ + "000110001010", + "111011110001", + "111010010010", + "100000000100" + }; + + int segments = 0; + for(int i=0;i