Note that there are some explanatory texts on larger screens.

plurals
  1. POFind all paths between point on board
    primarykey
    data
    text
    <p>In this topic: <a href="https://stackoverflow.com/questions/17170517/how-find-path-at-the-m-x-n-table/17222618#17222618">How find path at the m x n table</a> I found out how generate paths between two points, and this is code:</p> <pre><code>go m n (i,j) = [ (i+1,j) | i&lt;m ] ++ [ (i-1,j) | i&gt;1 ] ++ [ (i,j+1) | j&lt;n ] ++ [ (i,j-1) | j&gt;1 ] -- isEndOfPath p q = (p == q) genPath p q acc m n input buf = g p q acc buf where g p q acc buf | p==q = [(acc)] -- return acc, buf g p q acc buf = [s | r &lt;- go m n q, notElem r buf, notElem r acc, notElem r input, s &lt;- genPath p r (r:acc) m n input (r:buf)] ++ [s | r &lt;- go m n q, notElem r acc, r==p, s &lt;- genPath p r (r:acc) m n input (r:buf)] </code></pre> <p>For example, we can search path from (2,2) to (1,1) on board 2x2. Thus, we can call it as </p> <pre><code>genPath (2,2) (1,1) [(1,1)] 2 2 [(3,3),(1,1)] [(1,1)] </code></pre> <p>and we have result</p> <pre><code>[[(2,2),(2,1),(1,1)],[(2,2),(2,1),(1,1)],[(2,2),(1,2),(1,1)],[(2,2),(1,2),(1,1)]] </code></pre> <p>So we have correct paths.</p> <p>And now i will find all paths between pairs of points. In Prolog it was very easy and I haven't any problem with this. So, mayby I show you my algorithm and code:</p> <p>First predicat - when we find all paths, we return it:</p> <pre><code>genAllPaths([],A,A,_,_,_,_). </code></pre> <p>Else, we must generate paths recursively. So, firs we find path between first pairs, and then we can search other paths:</p> <pre><code>genAllPaths([(I1,J1),(I2,J2)|T],Acc,S,M,N,Input,Bufor) :- genPath((I1,J1),(I2,J2),[(I2,J2)],X,M,N,Input,[(I2,J2)|Bufor],NewBufor), genAllPaths(T,[X|Acc],S,M,N,Input,NewBufor). </code></pre> <p>If you don't understand something, please ask me. </p> <p>Hence, now I'll do it in haskell. I tried, but again, I have a lot of problems with it. If you know how do it and would like help me - I will very grateful.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload