开发者

Tell prolog to stop and start search with other possibilities

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 开发者_Go百科try that way.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜