数unique island, 比如110000110001001101101100100000总共两个unique岛,不是四个
方法可以是记录每次新的岛屿搜索的路径,left,right,up,down, 作为标志是否相同的key,存hashset
1 package fbOnsite; 2 import java.util.*; 3 public class UniqueIsland { 4 public int countIsland(int[][] grid) { 5 HashSetset = new HashSet (); 6 7 for (int i=0; i =1 && grid[i-1][j]==1) dfs(grid, i-1, j, sb.append('u'));27 //right28 if (j =1 && grid[i][j-1]==1) dfs(grid, i, j-1, sb.append('l'));33 }34 35 /**36 * @param args37 */38 public static void main(String[] args) {39 // TODO Auto-generated method stub40 UniqueIsland sol = new UniqueIsland();41 int[][] grid = new int[][]{ {1,1,0,0,0,0},{1,1,0,0,0,1},{0,0,1,1,0,0},{1,0,1,1,0,0,},{1,0,0,0,0,0}};42 int res = sol.countIsland(grid);43 System.out.println(res);44 }45 46 }