渗透模型(计数节点域)Hoshen-Kopelman算法没有得到期望的输出



我正在尝试编写一个渗流模型,我在下面的代码中使用Hoshen-Kopelman算法 问题是程序正在计数的一个节点域一个计数被遗漏,而之前的所有其他计数都从原始输出自动增加一个 在这种情况下,缺少 8

在这个问题之后,我想引入回溯功能,所以它是不完整的,对不起,我只想要帮助

original output:
1   0   2   2   0   0   3   3   3   3   3   3   3   3   0
0   0   2   0   0   4   0   0   3   3   3   0   0   0   5
0   2   2   2   2   0   0   0   0   3   0   5   5   5   5
6   0   2   2   0   7   0   0   0   0   5   5   0   0   5
0   0   2   2   0   0   0   0   5   0   0   0   8   0   5
9   0   2   0   10  0   0   0   5   0   0   5   0   5   5
9   9   0   0   10  0   5   5   5   5   5   5   5   5   0
9   0   0   10  10  10  0   0   5   5   5   0   0   5   5
9   9   0   10  10  10  0   11  0   5   0   0   12  0   0
0   0   13  0   0   10  10  0   5   5   0   12  12  0   0
output that come:
1   0   2   2   0   0   3   3   3   3   3   3   3   3   0   
0   0   2   0   0   4   0   0   3   3   3   0   0   0   5   
0   2   2   2   2   0   0   0   0   3   0   5   5   5   5   
6   0   2   2   0   7   0   0   0   0   5   5   0   0   5   
0   0   2   2   0   0   0   0   5   0   0   0   9   0   5   
10   0   2   0   11   0   0   0   5   0   0   5   0   5   5   
10   10   0   0   11   0   5   5   5   5   5   5   5   5   0   
10   0   0   11   11   11   0   0   5   5   5   0   0   5   5   
10   10   0   11   11   11   0   12   0   5   0   0   13   0   0   
0   0   14   0   0   11   11   0   5   5   0   13   13   0   0   
package percolate;
public class Count
{
int i,j,count=0;
/*int[][] matrix = {  {1,1,1,0,0,0,0,1,1},
{1,1,1,1,0,1,1,1,1},
{0,1,1,0,0,1,1,0,0},
{1,1,0,1,0,0,1,1,0},
{1,1,1,0,1,0,1,1,0},
{0,0,1,1,0,0,1,1,1},
{0,1,1,1,1,1,0,0,0},
{0,0,0,1,0,0,1,1,1},
{1,0,0,1,0,1,1,1,0}};*/
int[][] matrix = {
{1  ,0  ,1  ,1  ,0  ,0  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,0},
{0  ,0  ,1  ,0  ,0  ,1  ,0  ,0  ,1  ,1  ,1  ,0  ,0  ,0  ,1},
{0  ,1  ,1  ,1  ,1  ,0  ,0  ,0  ,0  ,1  ,0  ,1  ,1  ,1, 1},
{1  ,0  ,1  ,1  ,0  ,1  ,0  ,0  ,0  ,0  ,1  ,1, 0,  0,  1},
{0  ,0  ,1  ,1  ,0  ,0  ,0  ,0  ,1  ,0  ,0, 0,  1,  0,  1},
{1  ,0  ,1  ,0  ,1  ,0  ,0  ,0  ,1  ,0  ,0  ,1, 0   ,1, 1},
{1  ,1  ,0  ,0  ,1  ,0  ,1  ,1  ,1  ,1  ,1  ,1, 1   ,1  ,0},
{1  ,0  ,0  ,1  ,1  ,1  ,0  ,0  ,1  ,1  ,1  ,0, 0   ,1, 1},
{1  ,1  ,0  ,1  ,1  ,1  ,0  ,1  ,0  ,1  ,0  ,0  ,1, 0,  0},
{0  ,0  ,1  ,0  ,0  ,1  ,1  ,0  ,1  ,1  ,0  ,1, 1   ,0, 0},
};
int row =10,col =15;
int[][] label = new int [row][col];
private void operation(int i,int j)
{
// TODO Auto-generated method stub
if(i==0 && j==0)
{
count=count+1;
label[i][j]=count;
}
else if (((i-1)>=0) && j==0) 
{
left(i,j);  
}
else if (((j-1)>=0)&& i==0)
{
above(i,j);
}
else 
{
aboveleft(i,j); 
}

}
private void check() 
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
if(matrix[i][j]==0)
{
label[i][j]=matrix[i][j];
}
else
{
operation(i,j);
}
}   
}
}
private void left(int a,int b)
{
// TODO Auto-generated method stub
if(matrix[a-1][b]!=0)
{
label[a][b]=label[a-1][b];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void above(int a,int b) 
{
// TODO Auto-generated method stub
if (matrix[a][b-1]!=0)
{
label[a][b]=label[a][b-1];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void aboveleft(int a,int b)
{
// TODO Auto-generated method stub
if (matrix[a][b-1]!=0 && matrix[a-1][b]==0)
{
label[a][b]=label[a][b-1];
}
else if (matrix[a-1][b]!=0 && matrix[a][b-1]==0)
{
label[a][b]=label[a-1][b];
}
else if (matrix[a][b-1]==0 && matrix[a-1][b]==0)
{
count=count+1;
label[a][b]=count;
}
else
{
checklabel(a, b);
}
}
private void checklabel(int a, int b)
{
// TODO Auto-generated method stub
if(label[a-1][b]>label[a][b-1])
{
label[a][b]=label[a][b-1];
int neww=label[a][b-1];
int old=label[a-1][b];
nonzero(old,neww);
count=count-1;
}
else if (label[a-1][b]==label[a][b-1])
{
label[a][b]=label[a-1][b];
}
else
{
label[a][b]=label[a-1][b];
int neww=label[a-1][b];
int old=label[a][b-1];
nonzero(old,neww);
count=count-1;
}
}
private void nonzero(int ol,int nw)
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
if (label[i][j]==ol)
{
label[i][j]=nw;
backtrace(i,j);
}
}
}
}
private void backtrace(int a,int b)
{
// TODO Auto-generated method stub
for (int i = a; i < row; i++) 
{
for (int j = b; j < col; j++)
{
}   
}
}
private void output() 
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
System.out.print(label[i][j]+"   ");
}
System.out.println();
}
}
public static void main(String[] args) 
{
Count a=new Count();
a.check();
a.output();
}
}

我已经通过编码函数回溯并在正确的位置调用它来解决这个问题,检查下面的代码:-
包渗透;

import java.awt.geom.GeneralPath;
public class Count
{
int i,j,count=0;
int row =4,col =5;
int[][] label = new int [row][col];
int[][] matrix = new int [row][col];
/*int[][] matrix = {  {1,1,1,0,0,0,0,1,1},
{1,1,1,1,0,1,1,1,1},
{0,1,1,0,0,1,1,0,0},
{1,1,0,1,0,0,1,1,0},
{1,1,1,0,1,0,1,1,0},
{0,0,1,1,0,0,1,1,1},
{0,1,1,1,1,1,0,0,0},
{0,0,0,1,0,0,1,1,1},
{1,0,0,1,0,1,1,1,0}};*/
/*int[][] matrix = {
{1  ,0  ,1  ,1  ,0  ,0  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,1  ,0},
{0  ,0  ,1  ,0  ,0  ,1  ,0  ,0  ,1  ,1  ,1  ,0  ,0  ,0  ,1},
{0  ,1  ,1  ,1  ,1  ,0  ,0  ,0  ,0  ,1  ,0  ,1  ,1  ,1, 1},
{1  ,0  ,1  ,1  ,0  ,1  ,0  ,0  ,0  ,0  ,1  ,1, 0,  0,  1},
{0  ,0  ,1  ,1  ,0  ,0  ,0  ,0  ,1  ,0  ,0, 0,  1,  0,  1},
{1  ,0  ,1  ,0  ,1  ,0  ,0  ,0  ,1  ,0  ,0  ,1, 0   ,1, 1},
{1  ,1  ,0  ,0  ,1  ,0  ,1  ,1  ,1  ,1  ,1  ,1, 1   ,1  ,0},
{1  ,0  ,0  ,1  ,1  ,1  ,0  ,0  ,1  ,1  ,1  ,0, 0   ,1, 1},
{1  ,1  ,0  ,1  ,1  ,1  ,0  ,1  ,0  ,1  ,0  ,0  ,1, 0,  0},
{0  ,0  ,1  ,0  ,0  ,1  ,1  ,0  ,1  ,1  ,0  ,1, 1   ,0, 0},
};*/

private void operation(int i,int j)
{
// TODO Auto-generated method stub
if(i==0 && j==0)
{
count=count+1;
label[i][j]=count;
}
else if (((i-1)>=0) && j==0) 
{
left(i,j);  
}
else if (((j-1)>=0)&& i==0)
{
above(i,j);
}
else 
{
aboveleft(i,j); 
}

}
private void rand() 
{
// TODO Auto-generated method stub

for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
matrix[i][j] = (int)(Math.random()*2);
}
}
}
private void check() 
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
if(matrix[i][j]==0)
{
label[i][j]=matrix[i][j];
}
else
{
operation(i,j);
}
}   
}
}
private void left(int a,int b)
{
// TODO Auto-generated method stub
if(matrix[a-1][b]!=0)
{
label[a][b]=label[a-1][b];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void above(int a,int b) 
{
// TODO Auto-generated method stub
if (matrix[a][b-1]!=0)
{
label[a][b]=label[a][b-1];
}
else
{
count=count+1;
label[a][b]=count;
}
}
private void aboveleft(int a,int b)
{
// TODO Auto-generated method stub
if (matrix[a][b-1]!=0 && matrix[a-1][b]==0)
{
label[a][b]=label[a][b-1];
}
else if (matrix[a-1][b]!=0 && matrix[a][b-1]==0)
{
label[a][b]=label[a-1][b];
}
else if (matrix[a][b-1]==0 && matrix[a-1][b]==0)
{
count=count+1;
label[a][b]=count;
}
else
{
checklabel(a, b);
}
}
private void checklabel(int a, int b)
{
// TODO Auto-generated method stub
if(label[a-1][b]>label[a][b-1])
{
label[a][b]=label[a][b-1];
int neww=label[a][b-1];
int old=label[a-1][b];
nonzero(old,neww);
backtrace(old);
count=count-1;
}
else if (label[a-1][b]==label[a][b-1])
{
label[a][b]=label[a-1][b];
}
else
{
label[a][b]=label[a-1][b];
int neww=label[a-1][b];
int old=label[a][b-1];
nonzero(old,neww);
backtrace(old);
count=count-1;
}
}
private void nonzero(int ol,int nw)
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
if (label[i][j]==ol)
{
label[i][j]=nw;
}
}
}
}
private void backtrace(int a)
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++) 
{
for (int j = 0; j < col; j++)
{
if(label[i][j]>a )
{
label[i][j]=(label[i][j])-1;
}
}   
}
}
private void output() 
{
// TODO Auto-generated method stub
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
if (label[i][j]<10)
{
System.out.print(label[i][j]+"   ");
}
else
System.out.print(label[i][j]+"  ");
}
System.out.println();
}
}
public static void main(String[] args) 
{
Count a=new Count();
a.rand();
a.check();
a.output();
}
}

最新更新