告诉 prolog 停止并开始搜索其他可能性
我正在尝试用序言来解决魔方,我已经尝试过:
cubo_resuelto(F, F, F, F, F, F, F, F, F,
A, A, A, A, A, A, A, A, A,
I, I, I, I, I, I, I, I, I,
D, D, D, D, D, D, D, D, D,
B, B, B, B, B, B, B, B, B,
T, T, T, T, T, T, T, T, T).
mov(f, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F7, F4, F1, F8, F5, F2, F9, F6, F3,
A1, A2, A3, A4, A5, A6, I9, I6, I3,
I1, I2, B1, I4, I5, B2, I7, I8, B3,
A7, D2, D3, A8, D5, D6, A9, D8, D9,
D7, D4, D1, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9)).
mov(a, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,%
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(D1, D2, D3, F4, F5, F6, F7, F8, F9,
A7, A4, A1, A8, A5, A2, A9, A6, A3,
F1, F2, F3, I4, I5, I6, I7, I8, I9,
T9, T8, T7, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, I3, I2, I1)).
mov(d, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, B3, F4, F5, B6, F7, F8, B9,
A1, A2, F3, A4, A5, F6, A7, A8, F9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D7, D4, D1, D8, D5, D2, D9, D6, D3,
B1, B2, T3, B4, B5, T6, B7, B8, T9,
T1, T2, A3, T4, T5, A6, T7, T8, A9)).
mov(i, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(A1, F2, F3, A4, F5, F6, A7, F8, F9,
T1, A2, A3, T4, A5, A6, T7, A8, A9,
I7, I4, I1, I8, I5, I2, I9, I6, I3,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
F1, B2, B3, F4, B5, B6, F7, B8, B9,
B1, T2, T3, B4, T5, T6, B7, T8, T9)).
mov(b, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, I7, I8, I9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, T3, T2, T1,
D1, D2, D3, D4, D5, D6, F7, F8, F9,
B7, B4, B1, B8, B5, B2, B9, B6, B3,
D9, D8, D7, T4, T5, T6, T7, T8, T9)).
mov(t, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
D3, D6, D9, A4, A5, A6, A7, A8, A9,
A3, I2, I3, A2, I5, I6, A1, I8, I9,
D1, D2, B9, D4, D5, B8, D7, D8, B7,
B1, B2, B3, B4, B5, B6, I1, I4, I7,
T7, T4, T1, T8, T5, T2, T9, T6, T3)).
move(+M, OldState, NewState):-
mov(M, OldState, NewState).
move(-M, OldState, NewState):-
mov(M, NewState, OldState).
move_list([], X, X).
move_list([Move|T], X, Z):-
move(Move, X, Y),
move_list(T, Y, Z).
所以我可以做这样的事情:
move_list(X, cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'G',
'G', 'G', 'W', 'G', 'G', 'W', 'G', 'G', 'W',
'B', 'P', 'P', 'B', 'P', 'P', 'B', 'P', 'P',
'P', 'P', 'P', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
它会回答
X = [+f, +f, +f]
但是当我尝试这个时:
move_list(X, cubo('R', 'R', 'P', 'R', 'R', 'W', 'P', 'P', 'P',
'B', 'B', 'R', 'B', 'B', 'R', 'G', 'G', 'R',
'G', 'G', 'W', 'G', 'G', 'W', 'R', 'R', 'W',
'B', 'B', 'B', 'P', 'P', 'P', 'B', 'Y', 'Y',
'Y', 'Y', 'Y', 'P', 'W', 'W', 'P', 'W', 'W',
'W', 'G', 'G', 'Y', 'Y', 'B', 'Y', 'Y', 'G'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
序言尝试仅用+f运动来解决。
我想告诉序言,在 4 +f 之后开始查看其他动作。
我还想告诉它,尝试最多 N 个动作。
我怎样才能做到这一点?
谢谢。
PS:我知道这不是解决魔方的最佳方法,但我想尝试这种方法。
I'm trying to solve the Rubik's cube with prolog, I've tried this:
cubo_resuelto(F, F, F, F, F, F, F, F, F,
A, A, A, A, A, A, A, A, A,
I, I, I, I, I, I, I, I, I,
D, D, D, D, D, D, D, D, D,
B, B, B, B, B, B, B, B, B,
T, T, T, T, T, T, T, T, T).
mov(f, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F7, F4, F1, F8, F5, F2, F9, F6, F3,
A1, A2, A3, A4, A5, A6, I9, I6, I3,
I1, I2, B1, I4, I5, B2, I7, I8, B3,
A7, D2, D3, A8, D5, D6, A9, D8, D9,
D7, D4, D1, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9)).
mov(a, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,%
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(D1, D2, D3, F4, F5, F6, F7, F8, F9,
A7, A4, A1, A8, A5, A2, A9, A6, A3,
F1, F2, F3, I4, I5, I6, I7, I8, I9,
T9, T8, T7, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, I3, I2, I1)).
mov(d, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, B3, F4, F5, B6, F7, F8, B9,
A1, A2, F3, A4, A5, F6, A7, A8, F9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D7, D4, D1, D8, D5, D2, D9, D6, D3,
B1, B2, T3, B4, B5, T6, B7, B8, T9,
T1, T2, A3, T4, T5, A6, T7, T8, A9)).
mov(i, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(A1, F2, F3, A4, F5, F6, A7, F8, F9,
T1, A2, A3, T4, A5, A6, T7, A8, A9,
I7, I4, I1, I8, I5, I2, I9, I6, I3,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
F1, B2, B3, F4, B5, B6, F7, B8, B9,
B1, T2, T3, B4, T5, T6, B7, T8, T9)).
mov(b, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, I7, I8, I9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, T3, T2, T1,
D1, D2, D3, D4, D5, D6, F7, F8, F9,
B7, B4, B1, B8, B5, B2, B9, B6, B3,
D9, D8, D7, T4, T5, T6, T7, T8, T9)).
mov(t, cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
A1, A2, A3, A4, A5, A6, A7, A8, A9,
I1, I2, I3, I4, I5, I6, I7, I8, I9,
D1, D2, D3, D4, D5, D6, D7, D8, D9,
B1, B2, B3, B4, B5, B6, B7, B8, B9,
T1, T2, T3, T4, T5, T6, T7, T8, T9),
cubo(F1, F2, F3, F4, F5, F6, F7, F8, F9,
D3, D6, D9, A4, A5, A6, A7, A8, A9,
A3, I2, I3, A2, I5, I6, A1, I8, I9,
D1, D2, B9, D4, D5, B8, D7, D8, B7,
B1, B2, B3, B4, B5, B6, I1, I4, I7,
T7, T4, T1, T8, T5, T2, T9, T6, T3)).
move(+M, OldState, NewState):-
mov(M, OldState, NewState).
move(-M, OldState, NewState):-
mov(M, NewState, OldState).
move_list([], X, X).
move_list([Move|T], X, Z):-
move(Move, X, Y),
move_list(T, Y, Z).
So I can do something like this:
move_list(X, cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'G', 'G', 'G',
'G', 'G', 'W', 'G', 'G', 'W', 'G', 'G', 'W',
'B', 'P', 'P', 'B', 'P', 'P', 'B', 'P', 'P',
'P', 'P', 'P', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
And it will answer with
X = [+f, +f, +f]
But when I try this:
move_list(X, cubo('R', 'R', 'P', 'R', 'R', 'W', 'P', 'P', 'P',
'B', 'B', 'R', 'B', 'B', 'R', 'G', 'G', 'R',
'G', 'G', 'W', 'G', 'G', 'W', 'R', 'R', 'W',
'B', 'B', 'B', 'P', 'P', 'P', 'B', 'Y', 'Y',
'Y', 'Y', 'Y', 'P', 'W', 'W', 'P', 'W', 'W',
'W', 'G', 'G', 'Y', 'Y', 'B', 'Y', 'Y', 'G'),
cubo('R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R',
'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G', 'G',
'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P',
'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W',
'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y', 'Y')).
Prolog try to solve just with the +f movement.
I want to tell prolog that after 4 +f start look with other moves.
I also want to tell it that just try at max N movements.
How can I do that?
Thanks.
PS: I know that is not the best way to solve the Rubik's cube, but I want to try that way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要限制移动次数,请在谓词中添加 counter。仅当计数器在限制范围内时,移动才有效。每一步都会减少计数器。柜台应该是您所在州的一部分。
你是对的,这似乎是解决魔方的一种非常糟糕的方法,特别是如果你允许两次达到相同的状态 - 为了防止这种情况,请考虑在序言中设计回溯算法。
To limit the number of moves, add counter to the predicate. Move should be valid only if the counter is within the limit. Every move should decrease the counter. Counter(s) should be part of your state.
And you are right, that seems like a very bad way to solve Rubik's cube, especially if you allow reaching the same state twice - to prevent this, look at designing backtracking algorithms in prolog.