Prolog - Searching within a list of lists - how to do it

Posted on 2004-11-13
Medium Priority
Last Modified: 2008-02-01

I'm working on a program that lacks this one thing to be complete. :(
It's about a simple robot that cleans a room; the room is represented through a 10x10 map divided into squares, each square having it's own unique pair of x,y coordinates. I'm representing the coordinates via a list that contains two elements: [Xcoord,Ycoord], and a path through the room is represented via a sequence of squares in a list: [[0,0],[0,1][1,1]], for example. The robot doesn't move diagonally.
Now I need to find out how to get to a certain square being on another square; for example, I'm on [5,4] and I need to get to [0,0] - I need a predicate that returns me a list with a possible path through these squares, and I just can't do it :((( The closest I got didn't even work correctly. :(((( I know I could just subtract/sum the coordinates of the original position to get the result I want, but that's not what I need :(

Can someone please post some ideas, links and/or code examples on this?

Thanx in advance :)
Question by:bass20

Expert Comment

ID: 12576723
Might be that you find the answer that here:

I've had use of this site before when it came to mathematical problems, maybe it will help you as well?
Good luck,

Author Comment

ID: 12577968
No Prolog there...

Assisted Solution

gerodim earned 315 total points
ID: 12678184
lets say that your command would work like this

the best way to get the correct path would be to move horizontaly at first till you reach the spot and then vertically towards it (or vice versa if you like)...
so you could create a recursive command that gives u all the horizontal coordinates and then the vertical (you may have to rearrange your coordinates list to
more lists). I'll post the code a.s.a.p.

Assisted Solution

funkyfinger earned 60 total points
ID: 12678821
What you want is a recursive algorithm,
This is what it would do:

mark current location as "been here"
if can go north go north if at destination return true
if can go south go south if at destination return true
if can go east go east if at destination return true
if can go west go west if at destination return true
if no move available return false
unmark location

if you want I'll find the algorithm, I have it somewhere

Because you want a list of every posible solution you will need to find the number of possible solutions and call this recursive function that many times.

However you will get list of moves that are unnecessary such as
east, north, east, south
east, east
east, south, east, north

(all bring robot to same location)
Is this what you want?


Accepted Solution

gerodim earned 315 total points
ID: 12680134
take a look at this one: it's a bit different than your type of map&pathfinding but it'll give you some ideas...

you can also see if u can grasp anything out of this sourcecode i found in a book....
:- [pmeters].
graph :-

path([H|T], Y, [H|T]) :-
      H = Y.

path([H|T], Y, Path) :-
      \+ member(Next,[H|T]),
      path([Next,H|T],Y, Path).

adjacent(H,Next) :- d(H,Next);d(Next,H).

If you combine this algorithm with the horizontal/vertical movement you can get it to work pretty easily...

You may also want to check this link:
Or this one:


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This story has been written with permission from the scammed victim, a valued client of mine – identity protected by request.
As computers become more powerful, jobs will be replaced with artificial intelligence, leaving no place in the future for humans in the workforce. In the oncoming technological revolution, the staggering change to human mind labor will prove the ski…
Notifications on Experts Exchange help you keep track of your activity and updates in one place. Watch this video to learn how to use them on the site to quickly access the content that matters to you.
Where to go on the main page to find the job listings. How to apply to a job that you are interested in from the list that is featured on our Careers page.
Suggested Courses
Course of the Month15 days, 12 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question