하스켈로 개미노트 드릴게요. 2.
이번부터 Aray 해금.순환에도 사용할 수 있어요.개미책'2-1 Lake Counting'을 준다.
오늘의 표절품은 여기.입니다.
다음은 코드입니다.
-- Lake Counting (POJ No.2386)
-- https://lvs7k.github.io/posts/2018/11/pccb-easy-1/
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed (UArray)
import Data.Array.IArray
import Data.Char
import Data.STRef
import qualified Data.Set as S
import Data.Typeable
solve :: Int -> Int -> [String] -> Int
solve n m xss = runST $ do
    as <- newListArray ((1, 1), (n, m)) (concat xss) :: ST s (STUArray s (Int, Int) Char)
    refc <- newSTRef 0
    forM_ [1 .. n] $ \i -> do
        forM_ [1 .. m] $ \j -> do
            c <- readArray as (i, j)
            when (c == 'W') $ do
                dfs as (i, j)
                modifySTRef' refc (+ 1)
    readSTRef refc
    where
        dfs as (i, j) = do
            c <- readArray as (i, j)
            case c of
                '.' -> return ()
                _   -> do
                    writeArray as (i, j) '.'
                    mapM_ (dfs as) next
            where
                next = do
                    s <- [i - 1, i, i + 1]
                    t <- [j - 1, j, j + 1]
                    guard (inRange ((1, 1), (n, m)) (s, t))
                    return (s, t)
rInt :: String -> Int
rInt str = read str :: Int
main :: IO ()
main = do 
    n <- rInt <$> getLine
    m <- rInt <$> getLine
    inp <- getContents
    let garden = lines inp
    -- print (typeOf garden)
    print garden
    print $ solve n m garden
10
12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
stack runghc main.hs < data.txt
제가 알아볼게요
main.hs:27:9: error:
    • Non type-variable argument in the constraint: MArray a Char m
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        dfs :: forall (m :: * -> *) (a :: * -> * -> *).
               MArray a Char m =>
               a (Int, Int) Char -> (Int, Int) -> m ()
      In an equation for ‘solve’:
          solve n m xss
            = runST
                $ do as <- newListArray ((1, 1), (n, m)) (concat xss) ::
                             ST s (STUArray s (Int, Int) Char)
                     refc <- newSTRef 0
                     forM_ [1 .. n] $ \ i -> ...
                     ....
            where
                dfs as (i, j)
                  = do c <- readArray as ...
                       ....
                  where
                      next = ...
   |
27 |         dfs as (i, j) = do
   |         ^^^^^^^^^^^^^^^^^^...
solve :: Int -> Int -> [String] -> Int
solve n m xss = runST $ do
    as <- newListArray ((1, 1), (n, m)) (concat xss) :: ST s (STUArray s (Int, Int) Char)
    refc <- newSTRef 0
    forM_ [1 .. n] $ \i -> do
        forM_ [1 .. m] $ \j -> do
            c <- readArray as (i, j)
            when (c == 'W') $ do
                dfs as (i, j)
                modifySTRef' refc (+ 1)
    readSTRef refc
  where
    dfs as (i, j) = do
        c <- readArray as (i, j)
        case c of
            -- '.' -> return ()
            _   -> do
                -- writeArray as (i, j) '.'
                mapM_ (dfs as) next
      where
        next = do
            s <- [i - 1, i, i + 1]
            t <- [j - 1, j, j + 1]
            guard (inRange ((1, 1), (n, m)) (s, t))
            return (s, t)
"."안 될 것 같은데, 왜 그런가.
Reference
이 문제에 관하여(하스켈로 개미노트 드릴게요. 2.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://zenn.dev/forest1040/articles/aa8794b008e79ae6d1f2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)