Question: (Graph Traversal) Given a n*n grid of square; each square can be 1 (land) or 0 (water). An island is a maximally connected land square;

(Graph Traversal)

Given a n*n grid of square; each square can be 1 (land) or 0 (water). An island is a maximally connected land square; two land square are connected if they have common edges.

Find the number of islands for the given grid of squares

// Starter Code

#include

#include

#include

#include

usingnamespacestd;

intmain()

{

intn;

cin>>n;

cout<<"n="<

vector>grid(n,vector(n));

for(inti=0;i

for(intj=0;j

cin>>grid[i][j];

}

}

intcnt=0;

//nowcounthowmanyislands!

cout<

return0;

}

Please help complete the above code using graph traversal. Please include commenting and explanations of the code to help me understand how the solution was reached. Thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!