首页 文章

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

提问于
浏览
0

Task: 我需要制作一个方法,找到最大的方形子矩阵,边界为1,内部为0,来自矩阵(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);
}

Variables: x和y - 开始x和y坐标(p [x] [y])z - 方形子矩阵的长度

哪里出错了为什么我没有得到这个例子的x,y和z . 我已经测试了所有这些for循环,他们采取了他们应该的元素 . 如果你有一些建议,我想知道一些更好的方法 . 谢谢!

1 回答

  • 0

    我无法理解你的方法,但我认为它可能会过度使用它 . 例如,当有一个元素可以在一个地方完成时,你有几段代码检查元素是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}
    

    现在我想我可以尝试一下你的方法,但让我们把它写出来:

    largest_square = 0表示矩阵中的每个元素:如果它是正方形的一角,则找出square的大小设置为max_square到max(square,largest_square)end if return for return largest_square

    现在的问题是如何找出广场的大小 . 为此,我们可以启动单个零,然后检查方块的下一层是否存在 . 像这样:

    {0}检查下一层{0 0},{0 0}检查下一层{0 0 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++
    

    关于样式的最后一点注意事项:通常最好将空格分开,并将块的开头和结尾放在新行上,并为变量选择描述性名称 . 我不知道变量sm和bg是什么意思,而“酷”并不是特别具有描述性 .

    对不起任何错误,我希望这可以帮助您解决问题 .

相关问题