2010-06-04 145 views
-1

问题是;我们有一个函数取3个参数, 就好; func([[0,0,0,1,0],[0,1,1,1,0],[0,0,1,0,0], [0,0,1,0,0 ],[0,0,0,1,0]],(1,1),X)第一个是嵌套列表,即 显示5x5矩阵,1s表示满,0表示空,并且 第二个参数(1,1)我们的起点第一行第一列, 第三个参数X是;变量,我们将与 统一点可以从起点访问是(1,1) 所以如果问;定义5x5矩阵

?- func ([ [0,0,0,1] [0,0,1,0] [0,0,1,1] [0,0,1,0] ], (1,1), X). 
X = (1, 1); 

X = (1, 2); 

X = (1, 3); 

X = (2, 2); 

X = (3, 2); 

X = (4, 1); 

X = (4, 2); 

false. 

当我们从(1,1)开始时,我们可以向上,向下,向左和向右移动; 因为如果空(1,1)上没有向左和向上运动,空白时请向右看,写下来,向下看空写下来,再次转到(1,2),向右或向左或向上或向下移动,依此类推。 (2,4)(4,4) 如果例如点(2,3)已满并且(2,4)为空 我们看起来如果我们没有写输出,(2,4)(4,4) 我们可以一个接一个点(2,4),我的意思是, 如果离开,他们的上下是满的,我们不能用点来点(2,4),因为他们已满。

+0

你能更恰当地命名您的问题 – Shravan 2010-06-04 09:55:43

+0

OK,我们已经嵌套列表[0,0] [0,1]和起点POIN(A,B)和X 我们将展示我们的epmty点。 所以x将是(1,1)(1,2)(2,1),因为嵌套列表是2x2矩阵并且 1意味着点(1,2)满并且其他点是0意味着空的并且它们是需要的。 这里是开放的,我认为 – 2010-06-04 10:29:10

回答

10

我的解决方案:拿到教科书,坐在电脑前,为自己找出答案!简单地将某些东西标为家庭作业并不是你自己做的原因。

+3

+1(因为我不能给你更多的xD) – fortran 2010-06-04 10:26:35

+0

谢谢!我回来了这个忙。 – 2010-06-04 10:34:08

+0

@fortran,我也冒昧地看看你对其他问题的一些答案..很少回应! (以防万一你想知道为什么你的声望今天突然升高) – 2010-06-04 10:43:16

0

最后我做了它,这里是代码;

%returns the nth element from the list 
nth([F|_],1,F). 
nth([_|R],N,M) :- N > 1, N1 is N-1, nth(R,N1,M). 

%returns true if cell is empty: gets the cell value at (StartRow,StartColumn) and returns whether the value is 0 
isempty(Maze,StartRow,StartColumn) :- nth(Maze,StartRow,Line),nth(Line,StartColumn,Y), Y == 0. 

%returns the head of the list 
head([Elem|_],Elem). 

%find accessible returns empty list if not in maze size (1 to N for row and column) 
findaccessible(Maze, (StartRow,StartColumn), [], _) :- head(Maze,L),length(L,N), (StartColumn > N ; StartRow > N ; StartColumn < 1 ; StartRow < 1). 

%find all empty cells and retain them in X. L retains the current found cells in order to avoid returning to visited positions. 
findaccessible(Maze, (StartRow,StartColumn), X, L) :- 
    %if cell is empty, retain position and add it to the list 
    isempty(Maze,StartRow,StartColumn) -> (union(L,[(StartRow,StartColumn)],L1),X1 = [(StartRow,StartColumn)], 

    %check right column and if element not visited, find all accessible cells from that point and unify the lists 
    SR is StartRow, SC is StartColumn+1,(member((SR,SC),L) -> union(X1,[],X2) ; (findaccessible(Maze, (SR,SC), Tmp1, L1), union(X1,Tmp1,X2))), 
    %check down row and if element not visited, find all accessible cells from that point and unify the lists 
    SR2 is StartRow+1,SC2 is StartColumn, (member((SR2,SC2),L) -> union(X2,[],X3) ; (findaccessible(Maze, (SR2,SC2), Tmp2, L1), union(X2,Tmp2,X3))), 
    %check left column and if element not visited, find all accessible cells from that point and unify the lists 

    SR3 is StartRow, SC3 is StartColumn-1, (member((SR3,SC3),L) -> union(X3,[],X4) ; (findaccessible(Maze, (SR3,SC3), Tmp3, L1), union(X3,Tmp3,X4))), 
    %check up row and if element not visited, find all accessible cells from that point and unify the lists 
    SR4 is StartRow-1, SC4 is StartColumn, (member((SR4,SC4),L) -> union(X4,[],X) ; (findaccessible(Maze, (SR4,SC4), Tmp4, L1), union(X4,Tmp4,X)))) ; X = []. 

%lists each result 
%if no more results return false 
results(_,[]) :- fail. 
%return the result or return the rest of the results 
results(X,[Head|Rest]) :- X = Head ; results(X,Rest). 

%accessible predicate that finds all empty accessible cells and then list each of them 
accessible(Maze, (StartRow,StartColumn), X) :- findaccessible(Maze, (StartRow,StartColumn), Lst, []), !, results(X,Lst). 

%sample test run 
%accessible([[0, 0, 0, 1, 0], [0, 1, 1, 1, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]], (1, 1), X). 
+0

我改进了格式。可以自己缩进每行四个空格,或者使用带1和0的小按钮来指定代码。 – 2010-06-24 19:13:25