First of all, welcome, to The Rice Fields!
The Rice Fields is, in fact, an enormous realm with both explored and unexplored areas. Until now, every square meter of it is either water (marked with 0) or plants (marked with 1).
Now, some citizen of The Rice Fields would like to know the area of the biggest possible square house that can be built in it. A square house must be built on plant area and each of its 4 walls must be parallel to either the x -axis or the y -axis.
The Rice Fields is described in this file: RiceFields.txt . The first two numbers at the top represent the number of rows and columns, in that order. The rest of the file is a matrix of zeros and ones.
Example: Consider the following 4 x 4 field
0 0 0 1 1 0 1 0 1 1 1 0 1 1 1 0
Two most optimal solutions, area in both cases is 4
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
In addition to the other solutions, I think this thoroughly explains the process. The program goes to every element of the 1000x1000 matrix. If a 1, it goes down that column of ones and tries to make squares going out to the right from that side created. It loks at the length of the row of 1s coming out and the different square sizes that can support with whatever length of the rows above and below it are.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
In Java: open the input file in BufferedReader
ipf
, then:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Output:
1 |
|
Explanation: Array
sq[x]
is the side of the greatest square whose bottom-right corner lies in the current row at position
x
.
If a tile contains water, it cannot be part of a square, so
sq[x] = 0
in that case.
If a tile contains plants, we look at the neighboring tiles above, left, and top-left. If they all are the bottom-right corner of a square of size
n
or more, then we have now found a square of size
n
+
1
. In other words, we take thee
minimum
value of
sq[...]
for these neighbors and add one. The variables
top
,
left
, and
topleft
provide the necessary bookkeeping. They allow us to process the entire situation without storing the entire array.
I imported the .txt file into a MS excel spreadsheet using the excel text import wizard. I then made a few lines of VBA code to search the sheet for the largest square area the contained all 1's. It is not an elegant solution but it gets the job done.
Function Col_Letter(lngCol As Integer) As String
Dim vArr
vArr = Split(Cells(1, lngCol).Address(True, False), "$")
Col_Letter = vArr(0)
End Function
Private Function chkblk(rw As Integer, cl As Integer, sd As Integer) As Integer
For trw = 0 To sd
For tcl = 0 To sd
If Worksheets("Data").Cells(rw + trw, cl + tcl).Value = 0 Then
chkblk = (tcl + 1)
Exit Function
End If
Next tcl
Next trw
chkblk = 0
End Function
Sub areafind()
Dim col As Integer
Dim row As Integer
Dim side As Integer
col = 1
row = 1
side = 2
While (row <= (1000 - side))
Worksheets("results").Range("C10").Value = row
While (col <= (1000 - side))
Worksheets("results").Range("C11").Value = col
Worksheets("results").Range("E11").Value = Col_Letter(col)
While (chkblk(row, col, side) = 0)
side = side + 1
Worksheets("results").Range("C4").Value = row
Worksheets("results").Range("C5").Value = col
Worksheets("results").Range("E5").Value = Col_Letter(col)
Worksheets("results").Range("C6").Value = side
Wend
col = col + chkblk(row, col, side)
Wend
row = row + 1
col = 1
Wend
End Sub
Lets define an dynamic Dij = sum of '1' from start to (i,j): Dij = D(i-1)j + Di(j-1) - D(i-1)(j-1) + Aij
Then let`s fix point i,j, than we can find the max square with binary search. Sum from (i,j) to (i2,j2) = Di2j2 - D(i-1)j2 - Di2(j-1) + D(i-1)(j-1)
Answer is max answer for all (i,j)
So, final complexity is O(n m log(max(n,m)))
Problem Loading...
Note Loading...
Set Loading...
Solution:
Representing a square in the matrix - for my solution, I used special way to describe square's position and side length, nothing too flashy, but quite useful. I will explain it on the example provided with the problem.
In the left picture, each number describes whether the current field is usable (plant) or unusable (water). In the right picture, each field is actually the lower-right point of square and the number in that field is square's side length. If the number in a field is zero, then no square fits in there, while for non-zero number it is the opposite.
Now, with such way of presenting a square, certain rules are created.
A number in matrix N x , y is always equal to 0 if the current field (x,y) is water.
A number in matrix N x , y = 1 + m i n ( N x − 1 , y , N x − 1 , y − 1 , N x , y − 1 ) if the current field is plant.
A number outside of the matrix is also always a null (such numbers are used in formula from rule 2 few times).
If these 3 rules are transformed in few if statements, "the final product" is acquired, where the biggest number in whole matrix is the side length of the biggest square. In 1 0 0 0 × 1 0 0 0 matrix that is described in the .txt file, the biggest square has side length 1 6 , meaning its area is 2 5 6 .