简体   繁体   中英

Triangles algorithm

I have this problem to solve:

The input is a number and a triangle, for example:

5
#-##----#
 -----#-
  ---#-
   -#-
    -

The number is count of rows of the triangle.

And I have to print the largest "triangle area" - the largest triangle made of - . For this one the answer is 9.

The triangle can also be upside down:

4
#-#-#--
 #---#
  ##-
   -

For this, the output is 4.

I need some help with the algorithm. Please give me just a little help, not the whole algorithm, because I want to try to solve it by myself, I just need a direction.

Hint

I assume that all triangles are of the form:

--- 
 - 

And not like:

 -    or  -    or    -
---       --        --
          -          -

Remark that a 2-units triangles is made up of three 1-unit triangles. A 3-unit triangles is made up of 3 overlapping 2-unit triangles, and so on.

The next figure is an example of a 3-unit triangles made up of three 2-units triangles, themselves made up of three 1-unit triangles

  - -+ -+* +* *            --- +++ ***
     -  +  *      ==>       -   +   *
        o                       o

Spoiler: full algorithm follow, don't read it

 /!\ spoiler alert /!\

 /!\ spoiler alert /!\

 /!\ spoiler alert /!\

The main algorithm

You can do a first pass to compute all unit sized triangles (having exactly 1 - inside). Maintain a table where T[x,y] is the size of the triangle (length of its sides). In this pass you initialize every cell with a - to 1.

Then you can go from top to bottom and try to build more complex triangles.

When at position [x,y], you should consider the triangles whose down head is at:

  • [x-1,y-1]
  • [x ,y-1]
  • [x+1,y-1]

The size of the new triangles will be 1 plus the minimum size of any of the 3 above triangles. Then you update the table T[x,y]

T[x,y+1] = 1 + min(T[x-1,y], T[x,y], T[x+1,y])

At the end, just find the biggest sized triangles in your table T and compute the corresponding triangle area. (formula left as an exercise to the reader)

Complexity is O(n²) .

If efficiency doesn't matter that much:

Have one loop which searchs for candidates (it traverses each row from left to right). If you find a - , interrupt the search and try to compute this candidate .

Therefore first move to the right checking where it ends ( # ). Then you know the base of your triangle (indices) and directly know where it must continue in the next line, check that and repeat until you've explored that candidate fully. If left indix was li and right ri of row row , then the triangle must continue in the next row * row + 1 at indices li + 1 and ri - 1 .

Store its size and continue with the search part again. Terminate once the search visited all rows .


You can slightly improve its efficiency by ignoring - which are already part of an discovered triangle. But only if they are part of the triangle base row .

I am not an expert in algorithms, but as you only asked for some help and not the answer I believe I can submit an answer.

You need to figure out a way to test a space to determine if it is within a triangle. Once you have that I would design a brute force approach (run the "triangle test" against every space).

Then, when you have a brute force solution (which won't be an optimum approach) try and make it more efficient. Eg don't worry about being efficient or clever until you have a working solution. Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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