2D matrix with 0s and 1s. Try ... | CareerCup
0 For example: [[1,1,1,0] [1,1,0,0] [0,0,0,1]] return 3, because one for 1s, one for 0s, and one for the last one. another example: Country: - 0 This is my algo countries = 0 for( elements in matrix ) if element is not visited countries++ dfs from element and mark similar elements in path as visited print countries Comment hidden because of low score. Click to expand. 0 of 0 vote We can use DFS to find the connected components. We will not want to step on an alien element while doing a DFS. The number of distinct DFS forests will be the number of countries. bool visited[m][n]; int xi[]={0, 1, 1, 1, 0, -1, -1, -1}; int xj[]={1, 1, 0, -1, -1, -1, 0, 1}; int number_of_countries(int **country_map, int m, int n) { int i, j; int count=0; init(); for(i=0; iRead full article from 2D matrix with 0s and 1s. Try ... | CareerCup
No comments:
Post a Comment