tabish527112

urban1

Sep 5th, 2020
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package com.mtk.problems.problemset;
  2.  
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Queue;
  9. import java.util.Set;
  10.  
  11. public class Test {
  12.   /**
  13.    *
  14.    *   11000
  15.    *   11000
  16.    *   00011
  17.    *   00011
  18.    *
  19.    * 0,0 , 0,1, 1,1, 1,0
  20.    * 0,0 , 0,1, 1,1, 1,0
  21.    *
  22.    */
  23.  
  24.   public static void main(String[] args) {
  25.  
  26.     int[][] arr = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}};
  27.     int res = findDistinctIslands(arr,4, 5);
  28.     System.out.println(res);
  29.   }
  30.  
  31.   private static int findDistinctIslands(int[][] arr,int m, int n) {
  32.  
  33.     List<String> sourceList = new LinkedList<>();
  34.     for(int i=0;i<m;i++){
  35.       for(int j=0;j<n;j++){
  36.         if(arr[i][j]==1){
  37.           sourceList.add(i+"_"+j);
  38.         }
  39.       }
  40.     }
  41.  
  42.     Set<String> pathSet = new HashSet<>();
  43.     Set<String> visited = new HashSet<>();
  44.     Queue<String> q = new LinkedList<>();
  45.  
  46.     int x,y, sourceX, sourceY;
  47.     String[] split;
  48.     StringBuilder stringBuilder;
  49.     String coord;
  50.     for(String source: sourceList){
  51.       stringBuilder = new StringBuilder();
  52.       q.clear();
  53.       q.add(source);
  54.  
  55.       split = source.split("[_]");
  56.       sourceX = Integer.parseInt(split[0]);
  57.       sourceY = Integer.parseInt(split[1]);
  58.  
  59.       while(!q.isEmpty()){
  60.         coord = q.remove();
  61.         if(visited.contains(coord)){
  62.           continue;
  63.         }
  64.         split = coord.split("[_]");
  65.         x = Integer.parseInt(split[0]);
  66.         y = Integer.parseInt(split[1]);
  67.         stringBuilder.append("#").append(x-sourceX).append("_").append(y-sourceY);
  68.         visited.add(coord);
  69.         if(x<m-1 && arr[x+1][y]==1){
  70.           q.add((x+1)+"_"+y);
  71.         }else if(x<m-1 && y<n-1 && arr[x+1][y+1]==1){
  72.           q.add((x+1)+"_"+(y+1));
  73.         }else if(y<n-1 && arr[x][y+1]==1){
  74.           q.add(x+"_"+(y+1));
  75.         }else if(x>0 && y<n-1 && arr[x-1][y+1]==1){
  76.           q.add((x-1)+"_"+(y+1));
  77.         }else if(x>0 && arr[x-1][y]==1){
  78.           q.add((x-1)+"_"+y);
  79.         }else if(x>0 && y>0 && arr[x-1][y-1]==1){
  80.           q.add((x-1)+"_"+(y-1));
  81.         }else if(y>0 && arr[x][y-1]==1){
  82.           q.add((x)+"_"+(y-1));
  83.         }else if(x<m-1 && y>0 && arr[x+1][y-1]==1){
  84.           q.add((x+1)+"_"+(y-1));
  85.         }
  86.       }
  87.       if(stringBuilder.length()>0) {
  88.         pathSet.add(stringBuilder.toString());
  89.       }
  90.       System.out.println(stringBuilder.toString());
  91.     }
  92.  
  93.     return pathSet.size();
  94.   }
  95.  
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment