A problem encountered many years ago, leave a memory
Original post address http://bbs.csdn.net/topics/380191378
There is a bug in the code written before, that is, the first point must be 1. Ha ha, modified it.
original title
Connectivity determination,Figure 1 is connected (any one [1] can be reached from one [1])
Figure 2 is disconnected (there are at least 2 [1], they cannot be reached between them)
Figure 1:
1 1 1 1 1 1 1
1 1 1 1 0 0 1
1 1 1 1 0 0 1
1 1 0 0 1 1 1
1 1 0 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 1
Figure 2:
1 1 1 1 1 1 1
1 0 0 0 0 0 1
1 0 1 1 1 0 1
1 0 1 1 1 0 1
1 0 1 1 1 0 1
1 0 0 0 0 0 1
1 1 1 1 1 1 1
public class Demo1 { static int a1[][] = { {0 ,0 ,1 ,1 ,1 ,1 ,1} ,{1 ,0, 1, 0, 0, 0, 1} ,{1 ,0, 1, 1, 1, 0, 1} ,{1 ,0, 1, 1, 1, 0, 1} ,{1 ,0, 1, 1, 1, 0, 1} ,{1 ,0 ,0 ,0 ,0 ,0, 1} ,{1 ,1, 1, 1, 1, 1, 1}}; static int m = a1.length;//Maximum length static int n = a1[0].length;//Maximum width /** * @param args */ public static void main(String[] args) { Demo1 ha = new Demo1(); boolean flag2 = false; for (int i = 0; i < a1.length; i++) { for (int j = 0; j < a1[i].length; j++) { if(a1[i][j]==1){//Find the first point int[][] list = {{i,j}}; a1[i][j]=2; ha.check(list); flag2 = true; break; } } if(flag2){ break; } } // a1 [0] [0] = 2; // System.out.println(list3[0]); boolean flag = true; for(int i=0;i<m;i++) { for(int j=0;j<n;j++) { System.out.print(a1[i][j]); if(a1[i][j]==1) { flag = false; } } System.out.println(); } if(flag) { System.out.println("is open"); } else { System.out.println("is closed"); } } /** * starting from the first point 0,0, find 8 points around him, * If the surrounding 8 points have 1, record it, * Next time, find all the points of these 1 points, and cover them continuously * Add 1 to the covered 1 point, and it becomes 2, 0 points are not covered * When there is no 1 in the surrounding 8 points of a point. * Jump out of the loop, indicating that all coverage has been completed * The last circular matrix, if there are still 1 points, * Prove that this matrix is not connected, otherwise it is connected * @param list */ public void check(int[][] list) { int[][] list1 = new int[100][]; int k=0; for(int i=0;i<list.length;i++) { int[] zuobiao = list[i]; int x = zuobiao[0];//abscissa int y = zuobiao[1];//ordinate /* * Start to find 8 points around * coordinate of */ int[][] eight = new int[8][2];// //upper left if(x>0 && y>0) { eight[0][0] = x-1; eight[0][1] = y-1; if(a1[x-1][y-1] ==1) { a1[x-1][y-1] +=1; list1[k] = eight[0]; k++; } } //superior if(y>0) { eight[1][0] = x; eight[1][1] = y-1; if(a1[x][y-1] ==1) { a1[x][y-1] +=1; list1[k] = eight[1]; k++; } } //upper right if(x<m-1 && y>0) { eight[2][0] = x+1; eight[2][1] = y-1; if(a1[x+1][y-1] ==1) { a1[x+1][y-1] +=1; list1[k] = eight[2]; k++; } } //Left if(x>0) { eight[3][0] = x-1; eight[3][1] = y; if(a1[x-1][y] ==1) { a1[x-1][y] +=1; list1[k] = eight[3]; k++; } } //right if(x<m-1) { eight[4][0] = x+1; eight[4][1] = y; if(a1[x+1][y] ==1) { a1[x+1][y] +=1; list1[k] = eight[4]; k++; } } //lower left if(x>0 && y<n-1) { eight[5][0] = x-1; eight[5][1] = y+1; if(a1[x-1][y+1] ==1) { a1[x-1][y+1] +=1; list1[k] = eight[5]; k++; } } //Down if(y<n-1) { eight[6][0] = x; eight[6][1] = y+1; if(a1[x][y+1] ==1) { a1[x][y+1] +=1; list1[k] = eight[6]; k++; } } // Bottom right if(x<m-1 && y<n-1) { eight[7][0] = x+1; eight[7][1] = y+1; if(a1[x+1][y+1] ==1) { a1[x+1][y+1] +=1; list1[k] = eight[7]; k++; } } } //Indicates that there is no point in list1, indicating that the coverage is complete if(list1[0] == null) { return; }else { int[][] list2 = null; int i=0; for(;i<list1.length;i++) { if(list1[i]==null) { list2 = new int[i][]; break; } } for(int j=0;j<i;j++) { list2[j]= list1[j]; } check(list2); } } }