我有一个任务,我必须使用 Prolog 解决战舰难题。我目前的解决方案遍及整个领域,并将一条船放在它当前所在的坐标上。如果船的任何部分都不允许在那里(我只看给出的船件的总和。)它会放一些水。
example(X) :-
battleship(
[
[n,_,'~',_,_,_,_,n],
[_,_,n,'~',_,o,_,_],
['~','~',_,'~',_,_,_,s],
[_,'~',x,'~',_,_,'~','~'],
[_,_,_,'~',_,_,n,_],
[_,_,_,'~',_,_,_,_],
[_,_,_,x,_,'~',_,_],
[_,_,_,_,_,_,_,_]
],
[5,0,5,1,2,1,2,4],[2,4,2,3,2,1,4,2],[4,3,2,1],X).
上面的行就是你所说的。第一个参数是字段,其中 a~
代表水,_
代表未知,其他一切都是船的一部分。第二个参数是每列中船的总和,第三个参数是相同的,但对于行。第四个参数是船的大小,最后一个参数是我应该把结果放在哪里。
battleship(Grid, Columns, Rows, Ships, Result) :-
print(Result),
fillField(Grid, Columns, Rows, Ships, Result).
fillField(Grid, Columns, Rows, Ships, Result) :-
fillRows(Grid, Columns, Rows, Ships, Result, 0).
fillRows(Grid, Columns, Rows, Ships, Result, RowIndex) :-
length(Grid, Length),
RowIndex >= Length,
print(Grid), print('\n'),
print('Done\n').
fillRows(Grid, Columns, Rows, Ships, Result, RowIndex) :-
length(Grid, Length),
RowIndex < Length,
fillRow(Grid, Columns, Rows, Ships, Result, RowIndex, 0),
NextRow is RowIndex + 1,
fillRows(Grid, Columns, Rows, Ships, Result, NextRow).
fillRow(Grid, Columns, Rows, Ships, Result, RowIndex, ElemIndex) :-
print(Grid),print('\n'),
length(Grid, Length),
ElemIndex >= Length.
fillRow(Grid, Columns, Rows, Ships, Result, RowIndex, ElemIndex) :-
length(Grid, Length),
ElemIndex < Length,
isLegalShip(Grid, RowIndex, ElemIndex),
nth0(RowIndex, Rows, R1),
R2 is R1 - 1,
R2 >= 0,
replace(Rows, RowIndex, R2, NewRows),
nth0(ElemIndex, Columns, C1),
C2 is C1 - 1,
C2 >= 0,
replace(Columns, ColumnIndex, C2, NewColumns),
NextElem is ElemIndex + 1,
fillRow(Grid, NewColumns, NewRows, Ships, Result, RowIndex, NextElem).
fillRow(Grid, Columns, Rows, Ships, Result, RowIndex, ElemIndex) :-
length(Grid, Length),
ElemIndex < Length,
isSea(Grid, RowIndex, ElemIndex),
NextElem is ElemIndex + 1,
fillRow(Grid, Columns, Rows, Ships, Result, RowIndex, NextElem).
isLegalShip(Grid, RowIndex, ElemIndex) :-
nth0(RowIndex, Grid, Row),
nth0(ElemIndex, Row, Element),
isShip(Element).
replace([_|Tail], 0, Value, [Value|Tail]).
replace([Head|Tail], Index, Value, [Head|R]):-
Index > 0,
NextIndex is Index - 1,
replace(Tail, NextIndex, Value, R).
isSea(Grid, RowIndex, ElemIndex) :-
nth0(RowIndex, Grid, Row),
nth0(ElemIndex, Row, '~').
isShip(x).
isShip(o).
isShip(e).
isShip(s).
isShip(w).
isShip(n).
像这样解决问题将从右上角开始,然后向左和向下填充字段。但是,当由于卡住而需要回溯时,它会失败。使用此示例x
将放置在(0,3)
错误的位置。当我们到达这一排的尽头时,我们找到了另一条船,并发现出了点问题。她我们回到我们放置的地方x
。它现在应该放在~
那里,但它告诉我:
ERROR: >/2: Arguments are not sufficiently instantiated
Exception: (13) fillRow([[n, ~, ~, _G907, _G910, _G913, _G916|...], [_G925, _G928, n, ~, _G937, o|...], [~, ~, _G958, ~, _G964|...], [_G979, ~, x, ~|...], [_G1006, _G1009, _G1012|...], [_G1033, _G1036|...], [_G1060|...], [...|...]], [4, 0, 5, 1, 2, 1, 2, 4], [1, 4, 2, 3, 2, 1, 4, 2], [4, 3, 2, 1], _G828, 0, 3) ? creep
Exception: (10) fillRow([[n, _G901, ~, _G907, _G910, _G913, _G916|...], [_G925, _G928, n, ~, _G937, o|...], [~, ~, _G958, ~, _G964|...], [_G979, ~, x, ~|...], [_G1006, _G1009, _G1012|...], [_G1033, _G1036|...], [_G1060|...], [...|...]], [5, 0, 5, 1, 2, 1, 2, 4], [2, 4, 2, 3, 2, 1, 4, 2], [4, 3, 2, 1], _G828, 0, 0) ? creep
我只有一些 Prolog 的基本知识,无法弄清楚哪里出了问题。
任何帮助将不胜感激,我希望这个问题有点清楚。