Wacky_rubia

лесенка

May 7th, 2015
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.IO
  2. import Data.List as List
  3. import qualified Data.Map as Map
  4. main = do
  5.     x <- readFile "test.txt"
  6.     let words_ = words x
  7.     x <- readFile "letters.txt"
  8.     let letters = words x
  9.     let xs = bfs "ffff" "ffbb" ["ffff"] ["ffff"] [] letters words_
  10.     print xs
  11.     where
  12.         bfs start dest acc queue father letters words_
  13.             | (elem dest acc) = father--get_path father start [dest]
  14.             | otherwise = do
  15.                 let new_words = [x | x <- get_neigbours (head queue) words_ letters, notElem x queue, x /= (head queue), notElem x acc]
  16.                 let new_queue = (tail queue) ++ new_words
  17.                 let new_father = father ++ [(w, (head queue)) | w <- new_words, length queue > 0]
  18.                 let new_acc = [head queue] ++ acc
  19.                 bfs start dest new_acc new_queue new_father letters words_
  20.  
  21. get_path father start acc
  22.     | (elem start acc) = acc
  23.     | otherwise = do
  24.         if (elemIndex (head acc, _) father) != Nothing
  25.             then let ind = elemIndex (head acc, _) father
  26.             else throw SomeException
  27.         let e = father!!ind
  28.         let new_acc = (snd e) ++ acc
  29.         get_path father start new_acc
  30.  
  31. check_for_neigbour word ans words_
  32.     | elem word ans = False
  33.     | elem word words_ = True
  34.     | otherwise = False
  35.  
  36. change_letter word letters = do
  37.     [ch ind word letters i | i <- [0..3], ind <- [1..length word]]
  38.         where
  39.             ch ind word letters ind_let =  do
  40.                 let new_ind = ind - 1
  41.                 (take new_ind word) ++ (letters!!ind_let) ++ (drop ind word)
  42.  
  43.  
  44. get_neigbours word words_ letters = filter (\x -> elem x words_) (change_letter word letters)
Advertisement
Add Comment
Please, Sign In to add comment