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

Hello,

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?

LVL 1
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Might be that you find the answer that here:
http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/phi3DGeom.html#fivereg

I've had use of this site before when it came to mathematical problems, maybe it will help you as well?
Good luck,
/B
0
Author Commented:
No Prolog there...
0
Commented:
lets say that your command would work like this
?-pathfind([0,1],[4,3])

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.
0
Commented:
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
repeat

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
and
east, east
and
east, south, east, north

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

0
Commented:
take a look at this one: it's a bit different than your type of map&pathfinding but it'll give you some ideas...
http://ai-depot.com/Tutorial/PathFinding.html

you can also see if u can grasp anything out of this sourcecode i found in a book....
:- [pmeters].
graph :-
start(X),
finish(Y),
path([X],Y,Path).

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

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

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:
http://www.cs.mu.oz.au/303/project/
Or this one:
http://www.tjhsst.edu/~rlatimer/prolog/programs.html

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Miscellaneous

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.