渗透模型(计数节点域)hoshen-kopelman algo没有获得所需的输出

时间:2017-07-27 08:44:05

标签: java loops

我正在尝试编码一个渗透模型我在下面的代码中使用hoshen-kopelman算法问题是一个程序正在计算节点域一个计数得到错过而所有其他前面的数据自动从原始输出增加一个案件8缺少

在这个问题出现后我想介绍一下backtrace func所以它是不完整的抱歉,因为我只想要帮助

function check() {
  if (!document.getElementById('demodivclass').classList.contains('foo')) {
    window.location.assign('http://somesite.com');
  }
}

1 个答案:

答案 0 :(得分:0)

我通过对函数backtrace进行编码并通过在正确的位置调用它来解决它,检查下面的代码: -
 包装过滤;

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();
}
}