// countIslands.cpp : Defines the entry point for the console application. //
#include "stdafx.h" const int rows = 5; const int cols = 6; bool visited[rows][cols] = {0}; int arr[rows][cols] = { 0,0,0,0,1,1, 0,1,0,0,0,1, 1,1,0,0,0,0, 1,0,0,0,1,1, 0,0,1,0,1,0}; void initialize() { for (int i=0; i<rows;i++) { for (int j=0; j<cols;j++) { if (arr[i][j] != 1) visited[i][j]=1; } } } void coverNeighbours( int x, int y) { int neighbours[8][2] = {{x-1,y}, {x-1,y+1},{x,y+1},{x+1,y+1}, {x+1,y},{x+1,y-1},{x,y-1},{x-1,y-1}}; for (int i=0;i<8;i++) if ((neighbours[i][0] >=0) && (neighbours[i][0] <rows) && (neighbours[i][1] >=0) && (neighbours[i][1] <cols) && (arr[neighbours[i][0]][neighbours[i][1]] == 1) && (visited[neighbours[i][0]][neighbours[i][1]] == false)) { visited[neighbours[i][0]][neighbours[i][1]] = true; coverNeighbours(neighbours[i][0], neighbours[i][1]); } } int countIslands() { int result = 0; if ((rows <0) || (cols <0)) return 0; if ((rows == 0) && (cols == 0)) return arr[0][0]; initialize(); for (int i=0;i<rows;i++) { for (int j=0;j<cols;j++) { if ((arr[i][j] == 1) && (visited[i][j] !=1)) { result++; visited[i][j] = true; //don't increase result for the neighbours coverNeighbours(i,j); } } } return result; } int _tmain(int argc, _TCHAR* argv[]) { int result=countIslands(); return 0; } Best Regards Ashish Goel "Think positive and find fuel in failure" +919985813081 +919966006652 On Tue, Jan 10, 2012 at 1:17 PM, Ashish Goel <ashg...@gmail.com> wrote: > http://www.janaganamana.net/onedefault.aspx?bid=276 > > did not get it > > > Best Regards > Ashish Goel > "Think positive and find fuel in failure" > +919985813081 > +919966006652 > > > On Tue, Jan 10, 2012 at 1:15 PM, Ashish Goel <ashg...@gmail.com> wrote: > >> this is a single island, sorry for that >> >> Best Regards >> Ashish Goel >> "Think positive and find fuel in failure" >> +919985813081 >> +919966006652 >> >> >> On Tue, Jan 10, 2012 at 9:24 AM, atul anand <atul.87fri...@gmail.com>wrote: >> >>> @Ashish Goel : didnt get it :( >>> >>> >>> 1 1 0 0 >>> 1 1 0 0 >>> 0 0 1 1 >>> >>> >>> 1-1-1 diagonal is one island ...where is another?? >>> >>> 1 1 0 0 >>> 1 1 0 0 >>> 0 0 1 1 >>> >>> these 1 will be considered one island of 2 island.?? >>> >>> >>> >>> On Tue, Jan 10, 2012 at 7:36 AM, Ashish Goel <ashg...@gmail.com> wrote: >>> >>>> row, col, diag all >>>> >>>> 1-1-1 is a single island :) >>>> >>>> >>>> 1 1 0 0 >>>> 1 1 0 0 >>>> 0 0 1 1 >>>> >>>> this has only 2 islands >>>> >>>> >>>> Best Regards >>>> Ashish Goel >>>> "Think positive and find fuel in failure" >>>> +919985813081 >>>> +919966006652 >>>> >>>> >>>> >>>> On Tue, Jan 10, 2012 at 7:29 AM, Ankur Garg <ankurga...@gmail.com>wrote: >>>> >>>>> Can you give an example >>>>> >>>>> Say matrix is >>>>> >>>>> 1 1 0 0 >>>>> 1 1 0 0 >>>>> 0 0 1 1 >>>>> >>>>> Has it got 3 islands i.e 1-1 be in same row or they can be column wise >>>>> also i.e. 5 >>>>> >>>>> >>>>> >>>>> On Tue, Jan 10, 2012 at 7:09 AM, Ashish Goel <ashg...@gmail.com>wrote: >>>>> >>>>>> there is a matrix of 1 and 0 >>>>>> 1 is a island and 0 is water >>>>>> 1-1 together makes one island >>>>>> calculate total no of islands >>>>>> >>>>>> Best Regards >>>>>> >>>>>> Ashish Goel >>>>>> "Think positive and find fuel in failure" >>>>>> >>>>>> -- >>>>>> You received this message because you are subscribed to the Google >>>>>> Groups "Algorithm Geeks" group. >>>>>> To post to this group, send email to algogeeks@googlegroups.com. >>>>>> To unsubscribe from this group, send email to >>>>>> algogeeks+unsubscr...@googlegroups.com. >>>>>> For more options, visit this group at >>>>>> http://groups.google.com/group/algogeeks?hl=en. >>>>>> >>>>> >>>>> -- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "Algorithm Geeks" group. >>>>> To post to this group, send email to algogeeks@googlegroups.com. >>>>> To unsubscribe from this group, send email to >>>>> algogeeks+unsubscr...@googlegroups.com. >>>>> For more options, visit this group at >>>>> http://groups.google.com/group/algogeeks?hl=en. >>>>> >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Algorithm Geeks" group. >>>> To post to this group, send email to algogeeks@googlegroups.com. >>>> To unsubscribe from this group, send email to >>>> algogeeks+unsubscr...@googlegroups.com. >>>> For more options, visit this group at >>>> http://groups.google.com/group/algogeeks?hl=en. >>>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Algorithm Geeks" group. >>> To post to this group, send email to algogeeks@googlegroups.com. >>> To unsubscribe from this group, send email to >>> algogeeks+unsubscr...@googlegroups.com. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.