Saturday, January 24, 2015

Number of Countries

Question: 2D matrix with 0s and 1s. Try to find out how many countries in this matrix?
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:
[[1,1,1,1]
[0,0,0,0]
[1,0,0,1]]
return 4

Idea: Breadth-first search. For each untagged block, start breadth-first-search and marked the visited block with a specific TAG. When all the board has been marked, return the result.

Time: O(n^2) Space: O(n)

Code: 
 public class CountryNumber {  
      public CountryNumber()  
      {  
           int[][] input1={{1,1,1,0},{1,1,0,0},{0,0,0,1}};  
           int[][] input2={{1,1,1,1},{0,0,0,0},{1,0,0,1}};  
           printMatrix(input2);  
           System.out.println(solution(input2));  
      }  
      public void printMatrix(int[][] matrix)  
      {  
           for(int i=0;i<matrix.length;i++)  
           {  
                for(int j=0;j<matrix[0].length;j++)  
                {  
                     System.out.print(matrix[i][j]);  
                     System.out.print(' ');  
                }  
                System.out.print("\n");  
           }  
      }  
      public class Block{  
           int x;  
           int y;  
           public Block(int x,int y)  
           {  
                this.x=x;  
                this.y=y;  
           }  
      }  
      public int solution(int[][] board)  
      {  
           int TAG=Integer.MIN_VALUE;  
           int counter=0;  
           for(int i=0;i<board.length;i++)  
           {  
                for(int j=0;j<board[0].length;j++)  
                {  
                     if(board[i][j]!=TAG)  
                     {  
                          bfs(board,i,j);  
                          counter+=1;  
                     }  
                }  
           }  
           return counter;  
      }  
      public void bfs(int[][] board,int startX,int startY)  
      {  
           int TAG1=Integer.MIN_VALUE;  
           int TAG2=board[startX][startY];  
           int m=board.length;  
           int n=board[0].length;  
           Queue<Block> queue=new LinkedList<Block>();  
           queue.offer(new Block(startX,startY));  
           while(queue.isEmpty()==false)  
           {  
                int queueSize=queue.size();  
                for(int i=0;i<queue.size();i++)  
                {  
                     Block cur=queue.poll();  
                     board[cur.x][cur.y]=TAG1;  
                     if(cur.x+1<m&&board[cur.x+1][cur.y]==TAG2)  
                          queue.offer(new Block(cur.x+1,cur.y));  
                     if(cur.y+1<n&&board[cur.x][cur.y+1]==TAG2)  
                          queue.offer(new Block(cur.x,cur.y+1));  
                     if(cur.x-1>=0&&board[cur.x-1][cur.y]==TAG2)  
                          queue.offer(new Block(cur.x-1,cur.y));  
                     if(cur.y-1>=0&&board[cur.x][cur.y-1]==TAG2)  
                          queue.offer(new Block(cur.x,cur.y-1));  
                }  
           }  
      }  
 }  

No comments:

Post a Comment