The urban planning of Baidu's real problem (number of connected domains)

Source: Internet
Author: User

Topic:

A two-dimensional matrix is given, the matrix element is 1 or 0, and the number of independent non-connected regions consisting of 1 is calculated, and the bevel is 1 also connected. such as the following matrix:

0 1 0 0 0 0

0 1 0 1 0 0

0 0 0 0 1 1

The number of connected domains is 2.

Analysis:

Set up a collection list saves the coordinates that have been marked, loops through each point, marks the point with a value of 1, and marks the point adjacent to the point (using recursion, you can mark all points connected to that point).

The following code:

 Packageproject001;Importjava.util.ArrayList;Importjava.util.List; Public classMain09 {//find the number of 1 non-connected regions in the matrix, and diagonal is also connected     Public Staticlist<int[]> markedlist =Newarraylist<int[]>();  Public Static voidMain (string[] args) {int[] Maze = {{0,0,0,0,0},                        {0,1,0,0,0},                        {0,0,0,1,0},                        {1,1,0,0,0},                        {0,0,0,1,1}}; Doublet =System.currenttimemillis ();            System.out.println (Getl (FILLM (Maze))); }     Public Static intGetl (int[] M) {        intCount = 0; introw =m.length; intCol = m[0].length;  for(inti = 0;i<row;i++){             for(intj=0;j<col;j++){                //when the point is marked, and is 1 o'clock, all points connected to that point are marked                if(M[i][j]==1 &&!)ismarked (I,J))                    {Markpoint (i,j,m); Count++; }            }        }        returncount; }         Public Static int[] FILLM (int[] M) {        introw =m.length; intCol = m[0].length; int[] M2 =New int[Row+2] [Col+2];  for(inti = 1;i<=row;i++){             for(intj=1;j<=col;j++) {M2[i][j]=m[i-1][j-1]; }        }        returnM2; }        //determine if the point is marked     Public Static BooleanIsmarked (intIintj) {        intSize =markedlist.size (); if(size<=0)return false;  for(intk=0;k<size;k++){            int[] m =Markedlist.get (k); if(M[0]==I&AMP;&AMP;M[1]==J)return true; }        return false; }    //Mark Point (I,J) and mark all areas connected to that point     Public Static voidMarkpoint (intIintJint[] M) {        if(ismarked (I,J))return ; //mark the point, which is placed in a list        int[] m =New int[]{i,j};                Markedlist.add (m); //marks a point that is adjacent to a point (I,J) and is not marked        if(!ismarked (i-1,j-1) &&m[i-1][j-1]==1) Markpoint (i-1,j-1, M); if(!ismarked (I-1,J) &&m[i-1][j]==1) Markpoint (i-1, j,m); if(!ismarked (i-1,j+1) &&m[i-1][j+1]==1) Markpoint (i-1,j+1, M); if(!ismarked (i,j-1) &&m[i][j-1]==1) Markpoint (i,j-1, M); if(!ismarked (i,j+1) &&m[i][j+1]==1) Markpoint (i,j+1, M); if(!ismarked (i+1,j-1) &&m[i+1][j-1]==1) Markpoint (i+1,j-1, M); if(!ismarked (I+1,J) &&m[i+1][j]==1) Markpoint (i+1, j,m); if(!ismarked (i+1,j+1) &&m[i+1][j+1]==1) Markpoint (i+1,j+1, M); }    //Print Matrix     Public Static voidPRINTM (int[] m) {        introw =m.length; intCol = m[0].length;  for(inti=0;i<row;i++){             for(intj=0;j<col;j++) {System.out.print (M[i][j]+" "); } System.out.println (""); }    }}

The urban planning of Baidu's real problem (number of connected domains)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.