Improving the greedy algorithm

I am working on an abstract chess algorithm using Haskell (trying to expand my understanding of various paradigms), and I came across a problem that I have been thinking about for several weeks.

Here's the problem:

Given the board (represented by a list of integers; an integer represents the subsequent value of the point) with dimensions nxn, determine the path that provides the largest number of points. If there is a tie for a better way, return any of them.

Here are the specifics:

A = [[5,4,3,1],[10,2,1,0],[0,1,2,0],[2,3,4,20]] 

which is displayed as:

R1: 5  4  3  1, R2: 10 2 1 0, R3: 0 1 2 0, R4: 2 3 4 20.

Rules:

  • You can start work on the top line.

  • You can move one square at a time, either directly down, down-left (diagonally), or down-right (diagonally).

  • The output should be a tuple of integers.

, , - . . (5) ( 20- ). ([1,2,3,4], 29).

, Haskell, -. , r1 ; 3. , .

? , . !

+5
4

, .
, , , .

:
1. m = A
2. n = ( A)
3. , :
- 0, ,
- ,

generate_path :: [[Int]] -> [[Int]]
generate_path [] = [[]] 
generate_path A =  ... -- You have to put something here
        where 
              m = length A
              n = length (head A)

-,

move pos0 count0
    | count0 == 0 =   
        | pos0 == 0 = move (down count) ++ move (right count)  
        | otherwise = move (left count) ++ move (down count) ++ move (right count)  
            where 
                count = count0 - 1
                down  = position0 
                left  = position0 - 1
                right = position0 + 1

, (!!), . , A + list + +,

[A !! x !! y | x <- [1..2], y <- [0..2]] -- I take random range 

:

[[A !! x !! y | x <- [1..2]] | y <- [0..2]]] -- I take random range 

, n = length (head A), 0 (n-1) (n-1), , , m, 0 (m-1).

, . .

+3

, .

( )

[([1],5), ([2],4), ([3],3), ([4],1)]

, , 1 2 , 1 , 3 , 2 , 4, , 3 ,

[([1,1],15), ([1,2],7), ([2,3],5), ([3,4],3)]

, [0,1,2,0], , 1 , , 2 , , 3 ,

[([1,1,1],15), ([1,1,2],16), ([1,2,3],9), ([2,3,4],5)]

[2,3,4,20] , 2 , , 3 ,

[([1,1,2,1],18), ([1,1,2,2],19), ([1,1,2,3],20), ([1,2,3,4],29)]

, , .

:

c. , c-1, c, c+1 , c .

+3

- , , :

import Data.Function
import Data.List

-- All elements of Board are lists of equal lengths
-- valid b = 1 == length (group (map length b))
type Value = Int
type Board = [[Value]]
type Index = Int
type Result = ([Index], Value)

p :: Board
p = [[5,4,3,1],[10,2,1,0],[0,1,2,0],[2,3,4,20]] 

best_from :: Board -> Result
best_from [] = undefined
best_from xs | any null xs = undefined
best_from b = best_of . best_list $ b

best_list :: Board -> [Result]
best_list b = foldr1 layer (map label b)
  where label = zipWith (\index value -> ([index],value)) [1..]
        layer new rest =  zipWith (\(i1,v1) (i2,v2) -> (i1++i2, v1+v2)) new best
          where temp = head rest : map best_pair (zip rest (tail rest))
                best = map best_pair (zip temp (tail rest)) ++ [last temp]

best_pair :: (Result,Result) -> Result
best_pair (a@(_,a1), b@(_,b1)) | a1 >=b1 = a
                               | otherwise = b

best_of :: [Result] -> Result
best_of = maximumBy (compare `on` snd)

main = do
  print (best_from p)

, . , [#].

rest puzzel new, new - best rest ( , , ) new.

It makes foldr, or here a foldr1natural structure.

+3
source

I chose a different path, not a pun. I have listed valid combinations of indexes and compared them with them. Perhaps someone can find a way to generalize it to a board of any size.

import Data.List
import Data.Ord
import Data.Maybe

a = [[5,4,3,1],[10,2,1,0],[0,1,2,0],[2,3,4,20]]
r1 = a !! 0
r2 = a !! 1
r3 = a !! 2
r4 = a !! 3

i = [0,1,2,3]
index_combinations = [[a,b,c,d] | a <- i, b <- i, c <- i, d <- i, 
                      abs (b-a) < 2, abs (c-b) < 2, abs (d-c) < 2]

mapR xs = [r1 !! (xs !! 0), r2 !! (xs !! 1), 
           r3 !! (xs !! 2), r4 !! (xs !! 3)]

r_combinations = map mapR index_combinations
r_combinations_summed = zip r_combinations $ map (foldr (+) 0) r_combinations

result = maximumBy (comparing snd) r_combinations_summed
path = index_combinations !! fromJust (elemIndex result r_combinations_summed)
0
source

All Articles