简体   繁体   English

动态编程:查找最大的菱形(菱形)

[英]Dynamic programming: Find largest diamond (rhombus)

I have a small program to do in Java. 我有一个用Java做的小程序。 I have a 2D array filled with 0 and 1, and I must find the largest rhombus (as in square rotated by 45 degrees) and their numbers. 我有一个用0和1填充的2D数组,我必须找到最大的菱形(如旋转45度的正方形)和它们的数字。

Example: 例:

0 1 0 0 0 1

1 0 1 1 1 0

1 0 1 1 1 1

0 1 1 1 1 1

0 0 1 1 1 1

1 1 1 1 1 1

Result: 结果:

1    

    1 1 1  

  1 1 1 1 1

    1 1 1  

      1

The problem is similar to this SO question . 问题类似于此SO问题

If you have any idea, post it here. 如果您有任何想法,请在此处发布。

This too long for a comment. 这个评论太久了。 I'll post my solution later on if you can't solve it but here's how I've done it (in less than 15 lines of code): I first created a second array (a little big bigger [n+2][n+2]) and did n/2 pass: 如果您无法解决,我将在稍后发布我的解决方案,但是这是我的解决方法(少于15行代码):我首先创建了第二个数组(稍大[n + 2] [ n + 2]),并通过了n / 2:

0 0 0 0 0 0 0 0 
0 0 1 0 0 0 1 0 
0 1 0 1 1 1 0 0 
0 1 0 1 2 2 1 0 
0 0 1 2 2 2 1 0 
0 0 0 1 2 2 1 0 
0 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 

0 0 0 0 0 0 0 0 
0 0 1 0 0 0 1 0 
0 1 0 1 1 1 0 0 
0 1 0 1 2 2 1 0 
0 0 1 2 3 2 1 0 
0 0 0 1 2 2 1 0 
0 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0

Where a non-zero number x means "I'm the center of a rhombus of size x" (I'm expressing the size in relation with the length of the diagonals [which are both equal in your case] of the rhombus). 其中非零数字x表示“我是大小为x的菱形的中心”(我表示大小与菱形的对角线长度(在您的情况下都相等)有关)。 You can find if you have the center of a rhombus of size (k+1) by checking if {top,right,down,left} are all the centers of rhombus of size k. 您可以通过检查{top,right,down,left}是否都是大小为k的菱形中心来找到大小为(k + 1)的菱形中心。

The advantage of first creating a bigger array is that it really simplifies your logic but I could do it in place, with a more convoluted logic, by modifying the original array or by using a second array of the same size as the input (once again, it's way easier to simply put a safe "fence" of all-zeroes around your input). 首先创建一个更大的数组的好处是,它确实简化了您的逻辑,但是我可以使用更复杂的逻辑,通过修改原始数组或使用与输入大小相同的第二个数组来进行处理(再次,只需在输入周围放一个全零的安全“栅栏”,就更容易了。

If you don't "surround" your array with a fence, you have a lot of additional if/else checks: this would be prone to errors, lead to bigger code and lead to uglier code. 如果您不使用围栅“包围”您的数组,那么您将有很多其他的if / else检查:这将容易出错,导致代码更大,代码更丑陋。

Short tutorial: 简短教程:

How would you solve the problem if it was a 1x1 -field? 如果是1x1您将如何解决该问题?
How could you formulate the problem recursively? 您如何递归地提出问题?
How could you remember intermediate results and use them? 您如何记住中间结果并使用它们?
Do it. 做吧

void rhombus()
{
    maxr=0;
    for (int i=n-1;i>=0;i--)
    {
        for (int j=n-1;j>=0;j--)
        {
            if (b[i][j]>0)
            {
                if ((i==n-1) || (j==n-1) || (i==0) || (j==0)) b[i][j]=1;
                else {
                    b[i][j]=min4(b[i][j+1],b[i][j-1],b[i+1][j],b[i-1][j])+1;
                    if (b[i][j]==maxr) nrr++;
                    else if (b[i][j]>maxr) {
                        nrr=1;
                        maxr=b[i][j];
                    }
                }
            }
        }
    }
}

Did it,it works,this is my function,where maxr is the max size of the rhombus,and nrr is the number of max sized rhombus.Not sure how it works on huge arrays.(i loop this function n/2 times) 做到了,它起作用了,这是我的函数,其中maxr是菱形的最大大小,而nrr是最大菱形的数量。不确定它如何在巨大的数组上工作。(我将此函数循环n / 2次)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM