Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 最大的方子矩阵,内部0,外部1_Java_Arrays_Multidimensional Array_Sub Array - Fatal编程技术网

Java 最大的方子矩阵,内部0,外部1

Java 最大的方子矩阵,内部0,外部1,java,arrays,multidimensional-array,sub-array,Java,Arrays,Multidimensional Array,Sub Array,任务:我需要制作一种方法,从矩阵(2d数组)中找到最大的方子矩阵,该矩阵可以是正方形,但不需要是正方形。矩阵的所有元素都是1和0 这是我的密码 static void sub(int[][] p){ int sm=(p[0].length<p.length)?p[0].length:p.length; int bg=(p[0].length<p.length)?p.length:p[0].length; if(p.length==p[0].length){

任务:我需要制作一种方法,从矩阵(2d数组)中找到最大的方子矩阵,该矩阵可以是正方形,但不需要是正方形。矩阵的所有元素都是1和0

这是我的密码

static void sub(int[][] p){
    int sm=(p[0].length<p.length)?p[0].length:p.length;
    int bg=(p[0].length<p.length)?p.length:p[0].length;
    if(p.length==p[0].length){
        sm=p.length;bg=p.length;
    }
    int t=0;
    boolean cool=false;

    z:for(int z=sm;z>2;z--){
        x:for(int x=0,l=0;x<sm-z;x++,l++){
            y:for(int y=0,m=0;y<bg-z;y++,m++){
                for (int i=y;i<=z+m;i++){
                    if(p[x][i]!=1){cool=false; continue x;}
                    else cool=true;
                    if(p[z][i]!=1){cool=false; continue x;}
                    else cool=true;
                }
                int n=0;
                for(int j=0;j<z-1;j++)
                for(int i=y+1;i<z+m;i++){
                    if(p[x+n][i]!=0){cool=false; continue x;}
                    else cool=true;
                    if(i==z+m-1)n++;
                }
                for (int i=x+1;i<z+l;i++){
                    if(p[i][y]!=1){cool=false; continue x;}
                    else cool=true;
                }
                for(int i=x+1;i<=z-1;i++){
                    if(p[i][z+t]!=1) continue x;
                }
                if(cool){
                    System.out.println(x+" "+y+" "+z);
                }
                t++;
            }
            t=0;
        }
    }
}
public static void main(String[] args) {
    int[][] p = {
            {1,1,1,1,1,1,1,1,1},
            {1,0,0,0,1,1,1,1,1},
            {1,0,0,0,1,1,1,1,1},
            {1,0,0,0,1,1,1,1,1},
            {1,0,0,0,1,1,1,1,1},
            {1,0,0,0,1,1,1,1,1},
            {1,1,1,1,1,1,1,1,1}
    };
    sub(p);
}
static void sub(int[]p){

int sm=(p[0]。length我不能真正理解您的方法,但我认为这可能有点过头了。例如,当一个元素可以在一个地方完成时,您有几段代码检查它是0还是1

它可能会帮助您后退一步,重新评估代码。它应该做什么

“我需要制定一个方法,找到边界为1,内部为0的最大方子矩阵” 举个例子:

    {1,1,1,1,1,1,1,1,1},
    {1,0,0,0,1,1,1,1,1},
    {1,0,0,0,1,1,1,1,1},
    {1,0,0,0,1,1,1,1,1},
    {1,0,0,0,1,1,1,1,1},
    {1,0,0,0,1,1,1,1,1},
    {1,1,1,1,1,1,1,1,1}
现在我想我可以大致了解你的方法,但让我们把它写出来:

最大平方=0

对于矩阵中的每个元素:

如果它是正方形的一角

看看广场有多大

将最大平方设置为最大(平方,最大平方)

如果结束

结束

返回天安门广场

现在的问题是如何找出正方形的大小。要做到这一点,我们可以从一个零开始,然后检查正方形的下一层是否在那里。 像这样:

{0} 检查下一层

{0}

{0} 检查下一层

{0}

{0}

{0} 检查下一层

等等

为此,我们可以:

while matrix[i][j]==0:
    //check row below bottom of square
    for j =start_j to i:
        if matrix[i][j]!=0:
            break
        i++

    // if the side is short than the current side length
    if i<j:
        break

    //check column to right of square
    for j= start_j to i:
        if matrix[i][j]!=0:
            break
        j++
    //the row below and column to the right have all 0's so add to the square
    biggest_square++
当矩阵[i][j]==0时:
//检查正方形底部下方的行
对于j=开始_j至i:
如果矩阵[i][j]!=0:
打破
我++
//如果边短于当前边长度

如果iWell sm是行或列中较小的长度,那么bg是较大的。我制作了这些beas,因为我想从最大的正方形检查较小的。第一个应该是最大的。Cool是一个变量,它需要为true,直到y的末尾,命名为循环。在它里面,我检查正方形的所有边界元素,如果它们都是1,其中一个是for在元素内部检查它们是否都是零。如果cool为真,则在末尾打印cool square的起始x和y坐标。我尝试将一些大小的square向右移动到末尾(在本例中为右)向下移动一个,然后重复直到结束。有多少个方块?有没有保证方块外没有零,或者边界外没有一个?唯一重要的是,方块是最大的,里面至少有一个零(被一包围)。边界上有一个,零(s)内部。最大可能。在您的示例中,有一个0的矩形,但没有一个0的正方形。天哪:)您是对的,如果我更改了它,我会检查代码是否有效。我只是将那些粘贴在右侧。真丢脸。不幸的是,没有,我只设置了一个这样的正方形,它找不到它。