Note that there are some explanatory texts on larger screens.

plurals
  1. POHow find path at the m x n table
    primarykey
    data
    text
    <p>i have table m x n and pairs of points. For example, we can have table 3 x 3 and pairs of points:</p> <p>A = (1,3),(3,2) B = (1,2),(3,1)</p> <p>I must find all paths which will be connect points in pairs.This paths can't intersect each other. We can go in left, right, down and up. In preceding example, we have followig paths:</p> <p>A = (1,3) -> (2,3) -> (3,3) -> (3,2) B = (1,2) -> (2,2) -> (2,1) -> (3,1)</p> <p>(if is more solve, i would like have all)</p> <p>Have anyone any concept how can i do it in haskell?</p> <hr> <blockquote> <p>Perhaps you could explain in words your Prolog algorithm</p> </blockquote> <p>Ok, furthermore i have my code, so: </p> <p>I have four predicate to go on left, right, up and down. </p> <pre><code>go((I1,J1),(I2,J2),_,_) :- J1 is J2, I2&gt;=2, I1 is I2 - 1. go((I1,J1),(I2,J2),_,N) :- I1 is I2, J2=&lt;N-1, J1 is J2 + 1. go((I1,J1),(I2,J2),M,_) :- J1 is J2, I2=&lt;M-1, I1 is I2 + 1. go((I1,J1),(I2,J2),_,_) :- I1 is I2, J2&gt;=2, J1 is J2 - 1. </code></pre> <p>For example <code>go((I,J),(3,3),5,5)</code> returns </p> <pre><code>(I,J) = (2,3) (I,J) = (4,3) (I,J) = (3,2) (I,J) = (3,4) </code></pre> <p>Of course, arguments 5 is size of table - here we have table 5x5.</p> <p>I must knew, when is end of path, so i wrote:</p> <pre><code>endOfPath((I1,J1),(I2,J2)) :- I1 == I2, J1 == J2. </code></pre> <p>Then I could make predicate which will generate paths from point (I1,J1) to (I2,J2). First we must check if it is end of path:</p> <pre><code>generatePath((I1,J1),(I2,J2),T,T,_,_,_,B,B) :- endOfPath((I1,J1),(I2,J2)),!. </code></pre> <p>If it isn't end of path we must generate paths recursively. </p> <pre><code>generatePath((I1,J1),(I2,J2), Acc,T,M,N,Input,Bufor,NewBufor) :- go((I3,J3),(I2,J2),M,N), \+ member((I3,J3),Bufor), \+ member((I3,J3),Acc), \+ member((I3,J3),Input), generatePath((I1,J1),(I3,J3),[(I3,J3)|Acc],T,M,N,Input,[(I3,J3)|Bufor],NewBufor). </code></pre> <p>Thus, first we find point which is directly next to (I2,J2), then we check several conditions (for example, if (I3,J3) belong to any other path - it's wrong point). And then we generate path from (I1,J1) to (I3,J3) recursively. We have problem, when (I3,J3) is end of path, because (I3,J3) belong to Input and condition + member((I3,J3),Input) is not fulfilled.</p> <p>Hence, I wrote the last predicate:</p> <pre><code>generatePath((I1,J1),(I2,J2), Acc,T,M,N,Input,Bufor,NewBufor) :- go((I3,J3),(I2,J2),M,N), \+ member((I3,J3),Acc), I3 == I1, J3 == J1, generatePath((I1,J1),(I3,J3),[(I3,J3)|Acc],T,M,N,Input,[(I3,J3)|Bufor],NewBufor). </code></pre> <p>It was quite easy and gives good results but I don't know how can I make it in Haskell. Really, I have very big problem and please help me.</p>
    singulars
    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