Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Commit 2c68424

Browse files
author
cd155
committed
add more version to 8.2, find one path
1 parent 7eef824 commit 2c68424

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

‎src/Recursion.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ permutateSeq existSeq (x:xs) = map (++ [x]) existSeq ++
104104
105105
Theoretically, it has 2^(c+r) different paths
106106
107-
test case: allPaths (0,0) (2,2) [(1,1), (1,2)]
107+
1. find all valid paths
108+
test case: allPaths (0,0) (2,2) [(1,1), (1,2)]
109+
110+
2. find only one valid path
111+
test case: findOnePath (0,0) (3,3) [(1,1), (2,1), (3,1),(1,2),(1,3)]
108112
-}
109113

110114
{-
@@ -167,3 +171,20 @@ removeNothing :: [Maybe(Nat, Nat)] -> [Maybe(Nat, Nat)]
167171
removeNothing xs
168172
| xs!!(length xs -1) == Nothing = removeNothing $ init xs
169173
| otherwise = xs
174+
175+
-- 8.2, find only one valid path
176+
isValidPath :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)] -> Bool
177+
isValidPath (x, y) (c, r) constrs
178+
| x == c && y == r = True
179+
| x > c || y > r = False
180+
| (x, y) `elem` constrs = False
181+
| otherwise = isValidPath (x+1, y) (c, r) constrs ||
182+
isValidPath (x, y+1) (c, r) constrs
183+
184+
findOnePath :: (Nat, Nat) -> (Nat, Nat) -> [(Nat, Nat)] -> [(Nat, Nat)]
185+
findOnePath (x, y) (c, r) constrs
186+
| isValidPath (x+1, y) (c, r) constrs =
187+
(x+1, y): findOnePath(x+1, y) (c,r) constrs
188+
| isValidPath (x, y+1) (c, r) constrs =
189+
(x, y+1): findOnePath(x, y+1) (c,r) constrs
190+
| otherwise = []

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /