Check if the position if less then or greater then another position prolog

别等时光非礼了梦想. 提交于 2020-01-15 03:35:13

问题


I have revised this question to be more specific on what I'm looking for. I am trying to find if the position of (1,2) is greater than (1,1). Using the following code I get a specific element, being what, and return the position. For example if we want to find 1 in this maze, than we do the following:

findMaze([Row|_],What,(0,C)):-
   findRow(Row,What,C).
findMaze([_|Rows],What,R):-
   findMaze(Rows,What,TempR),
   R is TempR + 1.

findRow([What|_],What,0).
findRow([_|Tail],What,Where):-
   findRow(Tail,What,TWhere),
   Where is TWhere + 1.

Now all I want to do is take (1,1) and compare it with (1,2) to see if that is greater than (1,1). Ultimately what I want to accomplish is find the left and right of a position. So the left of (1,1) would be (1,0) and (1,2) would be the right. How do I make these into paths, thus getting the shortest path from (1,1) to (1,3).


回答1:


To answer the first part of your question:

mazeCell(Maze, What, (X,Y)) :-
    nth1(X, Maze, Row),
    nth0(Y, Row, What).

This captures the behavior you want:

?- mazeCell([[x,x,x,x,x,x,x],[x,-,-,-,-,-,1,x]], 1, Point).
Point =  (2, 6)

I note that mixing nth1 and nth0 like this is not supremely intuitive, and I do wonder if you are confusing your X and Y coordinates, but ultimately it probably shouldn't matter.

The nice thing about this predicate is that it is very versatile; you can use it to look things up by index, search for things by value, or iterate the entire maze. So for instance, you can examine the neighboring cells like this:

pointNeighbor((X,Y), (X1,Y)) :- succ(X, X1).
pointNeighbor((X,Y), (X1,Y)) :- succ(X1, X).
pointNeighbor((X,Y), (X,Y1)) :- succ(Y, Y1).
pointNeighbor((X,Y), (X,Y1)) :- succ(Y1, Y).

neighbors(Maze, Point, NeighborPoint, NeighborValue) :-
    pointNeighbor(Point, NeighborPoint),
    mazeCell(Maze, NeighborValue, NeighborPoint).

Now we can find the cells next to the player quite easily:

?- Maze = [[x,x,x,x,x,x,x],[x,-,-,-,-,-,1,x]], 
   mazeCell(Maze, 1, Point), 
   neighbors(Maze, Point, NeighborPoint, NeighborValue).
Maze = [[x, x, x, x, x, x, x], [x, -, -, -, -, -, 1|...]],
Point =  (2, 6),
NeighborPoint =  (1, 6),
NeighborValue = x ;

Maze = [[x, x, x, x, x, x, x], [x, -, -, -, -, -, 1|...]],
Point =  (2, 6),
NeighborPoint =  (2, 7),
NeighborValue = x ;

Maze = [[x, x, x, x, x, x, x], [x, -, -, -, -, -, 1|...]],
Point =  (2, 6),
NeighborPoint =  (2, 5),
NeighborValue =  (-) ;

false.

Notice that we didn't worry about whether we were generating valid points; mazeCell will simply fail if you ask for points that don't exist. We also didn't have to worry about traversing the maze other ways. mazeCell/3 is general enough to handle whatever. And now we have the interesting points and the interesting values at once and can do whatever we wish with them.

Hope this helps!



来源:https://stackoverflow.com/questions/34033215/check-if-the-position-if-less-then-or-greater-then-another-position-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!