-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f5b98dc
commit 58717b2
Showing
3 changed files
with
82 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,50 @@ | ||
/* | ||
written by M.Hiroi | ||
thanks | ||
?- depth_search([[left, left, left, left]]). | ||
*/ | ||
|
||
test :- | ||
depth_search([[left, left, left, left]]). | ||
|
||
/* 農夫だけ */ | ||
move([F, G, W, C], [NF, G, W, C]) :- (F == left -> NF = right ; NF = left). | ||
|
||
/* 農夫と山羊 */ | ||
move([F, F, W, C], [NF, NF, W, C]) :- (F == left -> NF = right ; NF = left). | ||
|
||
/* 農夫と狼 */ | ||
move([F, G, F, C], [NF, G, NF, C]) :- (F == left -> NF = right ; NF = left). | ||
|
||
/* 農夫とキャベツ */ | ||
move([F, G, W, F], [NF, G, W, NF]) :- (F == left -> NF = right ; NF = left). | ||
|
||
safe([F, G, W, C]) :- | ||
safe_cabbage(F, G, C), safe_goat(F, G, W). | ||
|
||
safe_cabbage(F, G, C) :- F == C. | ||
safe_cabbage(F, G, C) :- G \== C. | ||
|
||
safe_goat(F, G, W) :- F == G. | ||
safe_goat(F, G, W) :- G \== W. | ||
|
||
|
||
depth_search([State | History]) :- | ||
State == [right, right, right, right], !, print_answer([State | History]). | ||
|
||
depth_search([State | History]) :- | ||
move(State, NewState), | ||
safe(NewState), | ||
not(member(NewState, History)), | ||
depth_search([NewState, State | History]). | ||
|
||
print_answer([]) :- !. | ||
print_answer([State | Rest]) :- | ||
print_answer(Rest), write(State), nl. | ||
% | ||
% komachi.swi : パズル「小町算」の解法 | ||
% | ||
% Copyright (C) 2005 Makoto Hiroi | ||
% | ||
|
||
% | ||
% 式の計算 (+, -) だけ | ||
% | ||
calc_one_exp(A, +, B, N):- N is A + B. | ||
calc_one_exp(A, -, B, N):- N is A - B. | ||
|
||
calc_exp([Num], Num). | ||
calc_exp([A, Op, B | Rest], Num) :- | ||
calc_one_exp(A, Op, B, Num1), calc_exp([Num1 | Rest], Num). | ||
|
||
% | ||
% 式の生成 | ||
% | ||
set_op(N, Exp, [N, + | Exp]). | ||
set_op(N, Exp, [N, - | Exp]). | ||
set_op(N, [N1 | Rest], [N2 | Rest]) :- N2 is N1 * 10 + N. | ||
|
||
make_exp(10, Exp) :- | ||
reverse(Exp, Exp1), calc_exp(Exp1, N), N =:= 100, write(Exp1), nl, fail. | ||
|
||
make_exp(N, Exp):- | ||
N < 10, set_op(N, Exp, Exp1), N1 is N + 1, make_exp(N1, Exp1). | ||
|
||
solve :- make_exp(2, [1]). | ||
|
||
% リスト:覆面算 | ||
|
||
% 部分集合の判定 | ||
selects([], Ys). | ||
selects([X | Xs], Ys) :- select(X, Ys, Ys1), selects(Xs, Ys1). | ||
|
||
% Send + More = Money のチェック | ||
check(S, E, N, D, O, R, Y, Send, More, Money) :- | ||
Send is S * 1000 + E * 100 + N * 10 + D, | ||
More is 1000 + O * 100 + R * 10 + E, | ||
Money is 10000 + O * 1000 + N * 100 + E * 10 + Y, | ||
Money =:= Send + More. | ||
|
||
% パズルの解法 | ||
solve_puz(Send, More, Money) :- | ||
selects([S, E, N, D, O, R, Y], [0, 2, 3, 4, 5, 6, 7, 8, 9]), | ||
check(S, E, N, D, O, R, Y, Send, More, Money). | ||
|
||
% ?- solve_puz(Send, More, Money). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
% Thanks to M.Hiroi | ||
% | ||
% kaeru.swi : 蛙跳びゲーム | ||
% | ||
% Copyright (C) 2005 Makoto Hiroi | ||
% | ||
|
||
next(a, f). next(a, h). next(b, g). next(b, i). | ||
next(c, d). next(c, h). next(d, i). next(d, k). | ||
next(e, j). next(e, l). next(f, g). next(f, k). | ||
next(g, l). next(i, j). | ||
% 新しい局面の生成 | ||
new_state([b, s | Rest], [s, b | Rest]). | ||
new_state([s, w | Rest], [w, s | Rest]). | ||
new_state([b, w, s | Rest], [s, w, b | Rest]). | ||
new_state([s, b, w | Rest], [w, b, s | Rest]). | ||
new_state([X | R1], [X | R2]) :- new_state(R1, R2). | ||
|
||
jump(X, Y) :- next(X, Y). | ||
jump(X, Y) :- next(Y, X). | ||
% 手順の表示 | ||
print_answer([]) :- !. | ||
print_answer([State | Rest]) :- | ||
print_answer(Rest), write(State), nl. | ||
|
||
search(12, Path) :- | ||
reverse(Answer, Path), write(Answer), nl, !, fail. | ||
% 反復深化による解法 | ||
depth_search(Limit, Limit, [State | History]) :- | ||
State == [w,w,w,s,b,b,b], | ||
write(Limit),nl, | ||
print_answer([State | History]), | ||
fail. | ||
|
||
search(N, [Postion | Rest]) :- | ||
jump(Postion, NextPostion), | ||
not(member(NextPostion, Rest)), | ||
N1 is N + 1, | ||
search(N1, [NextPostion, Postion | Rest]). | ||
depth_search(N, Limit, [State | History]) :- | ||
N < Limit, | ||
new_state(State, NewState), | ||
not(member(NewState, History)), | ||
N1 is N + 1, | ||
depth_search(N1, Limit, [NewState, State | History]). | ||
|
||
solve :- | ||
between(1, 20, N), | ||
depth_search(0, N, [[b,b,b,s,w,w,w]]). | ||
|
||
test :- search(1, [a]). |