Prolog predicate to traverse a grid -
consider following predicate: path(start, end, size, path)
start cell in grid identified list [row, column]. end cell in grid identified list [row, column]. size list giving maximum row , column values. path consists of list of adjacent cells, adjacent left/right , up/down. path has no loops; i.e. cell can occur once in list.
with following query: path([1,1], [4,4], [4,4], path]. valid path be: [[1,1], [1,2], [1,3], [1,4], [2,4], [2,3], [3,3], [3,4], [4,4)].
how solve this?
here's start. first define path.
path(x,y,[x,y]):- edge(x,y). path(x,y,[x|xs]):- edge(x,w), path(w,y,xs).
then define edge:
edge([x,y], [x1,y]) :- x1 x + 1. edge([x,y], [x,y1]) :- y1 y + 1.
now predicate:
grid_path(start, end, limit, solution) :- within_limit(end, limit), path(start,end, solution).
i don't want spoil fun, i'll leave you. solution generates possible candidates , it's highly inefficient. can play around , optimise search.
Comments
Post a Comment