我对Haskell类有这个任务,但是我觉得这很困难。如果你能帮上忙。 您将得到一个迷宫棘手的haskell问题
maze = ["x xxx",
"x x",
"x x x",
"x x ",
"xxxxx"]
,你可以通过空格只能步行。你从(0,1)开始,该函数必须返回与方向的字符串逃过这是迷宫:
f - forward
r- turn right
l - turn left
如果你有你总是喜欢选择正确转发,并转发到左。 对于当前的例子,答案是事先
data Direction = N | W | S | E deriving (Show,Eq)
maze = ["x xxx",
"x x",
"x x x",
"x x ",
"xxxxx"]
d = 's'
pos = (0,1)
fpath d pos | fst pos == (length maze - 1) = ""
| snd (pos) ==0 || (snd (pos) == ((length (maze!!0))-1)) = ""
| rightPossible d pos = "r" ++ (fpath (rightRotate d) pos)
| forwardPossible d pos = "f" ++ (fpath d (nstep d pos))
| True = "l" ++ fpath (leftRotate d) pos
where nstep :: Direction -> (Int, Int) -> (Int, Int) {-next step-}
nstep N (x,y) = (x-1,y)
nstep W (x,y) = (x,y-1)
nstep S (x,y) = (x+1,y)
nstep E (x,y) = (x,y+1)
rightPossible :: Direction -> (Int, Int) -> Bool
rightPossible N (x,y)= (maze !! x)!! (y+1) == ' '
rightPossible W (x,y)= (maze !! (x-1))!! y == ' '
rightPossible S (x,y)= (maze !! x)!! (y-1) == ' '
rightPossible E (x,y)= (maze !! (x+1))!! y == ' '
rightRotate :: Direction -> Direction
rightRotate N = E
rightRotate W = N
rightRotate S = W
rightRotate E = S
forwardPossible :: Direction -> (Int, Int) -> Bool
forwardPossible N (x,y)= ((maze !! (x-1))!! y) == ' '
forwardPossible W (x,y)= ((maze !! x)!! (y-1)) == ' '
forwardPossible S (x,y)= ((maze !! (x+1))!! y) == ' '
forwardPossible E (x,y)= ((maze !! x)!! (y+1)) == ' '
leftRotate :: Direction -> Direction
leftRotate N = W
leftRotate W = S
leftRotate S = E
leftRotate E = N
现在,哪里是问题吗?你已经做了什么?你卡在哪里? – Howard
使用[“墙壁追随者”](http://en.wikipedia.org/wiki/Maze_solving_algorithm);) – Jacob
我在Haskell开始工作,我以前有过使用函数式语言的经验。我知道我需要重新解决这个问题,但我无法在Haskel中实现它。 – munch