Sort by

recency

|

22 Discussions

|

  • + 0 comments

    In scala recursion does not work, it is too slow, neither forward recursion with local memo, only backward GLOBAL recursion. I have added some fancy OOP for more readability.

    import scala.io.StdIn
    import scala.collection.mutable
    
    object Solution {
      private final case class Cell(
          row: Int,
          col: Int
      ) {
        def left: Cell = this.copy(col = col - 1)
        def upward: Cell = this.copy(row = row - 1)
      }
    
      private final case class Dice(
          top: Int,
          front: Int,
          left: Int
      ) {
        def rotateDown: Dice = this.copy(
          top = 7 - front,
          front = top
        )
        def rotateRight: Dice = this.copy(
          top = left,
          left = 7 - top
        )
      }
    
      private final case class Score(
          value: Int = 0
      ) {
        def +(dice: Dice): Score = this.copy(value = value + dice.top)
        def <(other: Score): Boolean = this.value < other.value
        def toInt: Int = value
      }
      private object Score {
        def apply(dice: Dice): Score = Score(dice.top)
        implicit val scoreOrdering: Ordering[Score] = Ordering.by(_.value)
        implicit def toInt(score: Score): Int = score.toInt
      }
    
      private val startCell = Cell(1, 1)
      private val initialDice = Dice(1, 2, 3)
      private val memo = mutable.Map[Cell, mutable.Map[Dice, Score]](
        startCell -> mutable.Map[Dice, Score](
          initialDice -> Score(initialDice)
        )
      )
    
      private def maximalPath(m: Int, n: Int): Int = {
        if ((m < 1) || (n < 1))
          return -1
    
        def calculate(cell: Cell): Unit = {
          if (!memo.contains(cell)) {
            memo.addOne(cell, mutable.Map.empty)
            if (1 < cell.row) {
              val upwardCell = cell.upward
              if (!memo.contains(upwardCell))
                calculate(upwardCell)
              memo(upwardCell).map { case (upwardDice, upwardScore) =>
                val dice = upwardDice.rotateDown
                val score = upwardScore + dice
                memo(cell).addOne(dice -> score)
              }
            }
            if (1 < cell.col) {
              val leftCell = cell.left
              if (!memo.contains(leftCell))
                calculate(leftCell)
              memo(leftCell).map { case (leftDice, leftScore) =>
                val dice = leftDice.rotateRight
                val score = leftScore + dice
                if (!memo(cell).contains(dice))
                  memo(cell).addOne(dice -> score)
                else if (memo(cell)(dice) < score)
                  memo(cell)(dice) = score
              }
            }
          }
        }
    
        val targetCell = Cell(m, n)
        calculate(targetCell)
        return memo(targetCell).values.max
      }
    
      def main(args: Array[String]): Unit = {
        val in = Iterator
          .continually(StdIn.readLine())
          .takeWhile(null != _)
          .map(_.trim)
        val t = in.next().toInt
        (1 to t)
          .map(_ => in.next().split(' ').map(_.toInt))
          .map(mn => maximalPath(mn.head, mn.last))
          .foreach(println)
      }
    }
    
  • + 0 comments

    This is my haskell solution, but this doesn't work for test case 5 and 6 Time Complexity - O(t.m.n), Space Complexity - O(m.n) Please suggest some edits to get my program to pass all the test cases.

    import Data.Array
    
    find :: Int -> Int -> Int 
    find m n = findPaths (1, 1, 1, 3, 2)
        where
            dp = array ((1,1,1,1,1), (m, n, 6, 6, 6))
                 [(c , findPaths c) | c <- (,,,,) <$> [1..m] <*> [1..n] <*> [1..6]  <*> [1..6] <*> [1..6]]
                 
            get c
                | inRange (bounds dp) c = dp ! c
                | otherwise = -1
        
            findPaths (i,j,t,l,f)
                | i == m && j == n = t
                | otherwise = t + max path1 path2
                    where
                        path1 = get (i, j+1, l, 7-t, f)
                        path2 = get (i+1, j, 7-f, l, t)
         
    main :: IO()
    main = do
        raw <- getContents
        mapM_ print $ map helper $ (map . map) (read :: String -> Int) $ map words $ tail $ lines raw
        where helper (m : n : _) = find m n 
    
  • + 0 comments

    come and join to my site viralQQ and GencarQQ

  • + 0 comments

    poker online bandar poker situs bola tangkas situs betting bola

  • + 0 comments

    Daftar situs judi bola online terbaik dan terpercaya di Indonesia. yang menyediakan beragam permainan taruhan online seperti judi bola tangkas , poker online, live casino dan lainnya dalam 1 user ID.

    Nikmati juga bonus deposit new member 10% hanya di situs MENUBOLA

    Menubola