2010-06-04 95 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