检查位置是否小于或大于另一个位置序言 [英] Check if the position if less then or greater then another position prolog

查看:45
本文介绍了检查位置是否小于或大于另一个位置序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经修改了这个问题,以便更具体地说明我在寻找什么.我试图找出(1,2)的位置是否大于(1,1).使用以下代码,我获得了一个特定元素,即 what,并返回位置.例如,如果我们想在这个迷宫中找到 1,那么我们可以这样做:

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.

现在我要做的就是将 (1,1) 与 (1,2) 进行比较,看看它是否大于 (1,1).最终我想要完成的是找到一个位置的左右.所以 (1,1) 的左边是 (1,0) 而 (1,2) 是右边.我如何将这些变成路径,从而获得从 (1,1) 到 (1,3) 的最短路径.

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).

推荐答案

回答您问题的第一部分:

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)

我注意到像这样混合 nth1nth0 并不是非常直观,我想知道您是否混淆了 X 和 Y 坐标,但最终它可能应该没关系.

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.

请注意,我们并不担心是否生成了有效点数;如果您要求不存在的点,mazeCell 只会失败.我们也不必担心以其他方式穿越迷宫.mazeCell/3 足够通用来处理任何事情.现在我们同时拥有有趣的点和有趣的值,并且可以对它们做任何我们想做的事情.

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.

希望这会有所帮助!

这篇关于检查位置是否小于或大于另一个位置序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆