home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / pascal / 7797 < prev    next >
Encoding:
Internet Message Format  |  1993-01-02  |  3.4 KB

  1. Xref: sparky comp.lang.pascal:7797 rec.games.programmer:5238 comp.sys.ibm.pc.misc:16281 comp.sys.ibm.pc.programmer:722
  2. Path: sparky!uunet!munnari.oz.au!ariel.ucs.unimelb.EDU.AU!ucsvc.ucs.unimelb.edu.au!lugb!lux!cscmd
  3. Newsgroups: comp.lang.pascal,rec.games.programmer,comp.sys.ibm.pc.misc,comp.sys.ibm.pc.programmer
  4. Subject: Re: Source of a little puzzle under MS-DOS TurboPascal wanted
  5. Message-ID: <1993Jan2.100625.27480@lugb.latrobe.edu.au>
  6. From: cscmd@lux.latrobe.edu.au (Mitch Davis)
  7. Date: Sat, 2 Jan 1993 10:06:25 GMT
  8. Sender: news@lugb.latrobe.edu.au (USENET News System)
  9. References: <1992Dec30.162530.7112@delos.stgt.sub.org>
  10. Organization: La Trobe University
  11. Lines: 70
  12.  
  13. In article <1992Dec30.162530.7112@delos.stgt.sub.org> stefanb@delos.stgt.sub.org (Stefan Bechtold) writes:
  14. >Hi !
  15. >
  16. >A friend of mine is looking for a puzzle; it's a little bit difficult 
  17. >to describe but I'll try it.
  18. >This puzzle shuffles a display like those old 16 square plastic
  19. >puzzles.
  20. >(Like:        9    14    10    4
  21. >        
  22. >        6    1    8    12
  23. >            
  24. >        2    15        5
  25. >        
  26. >        11    7    3    13
  27. >
  28. >and you have to put those squares in the right order)
  29. >
  30. >The program should also be able to solve the problem itself.
  31.  
  32. First, I should mention that picking Turbo Pascal as a language for
  33. solving this problem is fairly poor.  To do this in a language like LISP
  34. or Scheme is about 100 lines (or at least, that is how big my CS
  35. assignment this year to do it was), while I can't see how you'd do it
  36. in TP would take less than say 600 lines.
  37.  
  38. Here is one way of tackling the problem:
  39.  
  40. First, make some way of representing the state of the puzzle.  In Scheme
  41. terms, something like ((9 14 10 4)(6 1 8 12)(2 15 0 5)(11 7 3 13)), or a
  42. 4x4 array in TP.
  43.  
  44. Second, build a function which, given a transformation like "u","d","l"
  45. or "r", it takes a puzzle state and returns the state of the puzzle
  46. AFTER that transformation.
  47.  
  48. Next, you start with a few lists.  One list contains all the states
  49. visited so far, so it starts off with only one item, the initial state.
  50. Then to solve it, you use recursion to search the tree of possible
  51. transformations until you either find some combination of
  52. transformations which results in the final state, or you are left with
  53. no states after any transform which have not occured before.  In effect,
  54. you'll be applying "u,d,l,r,uu,ud,ul,ur,du,dd,dl,dr,lu,ld,ll,lr,ru,rd,rl,
  55. rr,uuu,uud, etc" to the initial state, and seeing if you get the final
  56. state.  The order of the letters you try gets read off when your
  57. recursion unwinds, and this gives the moves you need.
  58.  
  59. Note that this is a VERY computationally intensive algorithm if the
  60. solution is non-trivial (it is of the order O(4^n)), and only uses a
  61. simple heuristic - that is, it doesn't search parts of the search tree
  62. that have already been searched.  What it WON'T do is infalibly see
  63. that a simple move would be shorter than a longer set.
  64.  
  65. Hmm.  Other thoughts:
  66.  
  67. Take each state in the list in turn:
  68.   Apply in turn, u,d,l and r.
  69.   Put the resultant states in another list.
  70.   Are any of the resultant states the final state?  If so, unravel the
  71.   recursion reading off the moves.
  72.   If not, add the resultant states to the list of ones to search next
  73.   time, unless they already occur, ie, no duplicates.
  74.  
  75. Having said all that, I think it IS possible to do it in TP (although
  76. not a wise thing to do).  I must admit I am tempted, and if I get around
  77. to doing it, my preferred method to use would be to hijack the
  78. TSortedCollections from Turbo Vision.
  79.  
  80. Hope this helps.
  81.  
  82. Mitch.
  83.