[UVA][向BFS] 704 - Colour Hash@Morris' Blog|PChome Online 人新台
2013-06-28 09:39:59| 人1,624| 回0 | 上一篇 | 下一篇

[UVA][向BFS] 704 - Colour Hash

0 收藏 0 0 站台


  Colour Hash 

This puzzle consists of two wheels. Both wheels can rotate both clock and counter-clockwise. They contain 21 coloured pieces, 10 of which are rounded triangles and 11 of which are separators. Figure 1 shows the final position of each one of the pieces. Note that to perform a one step rotation you must turn the wheel until you have advanced a triangle and a separator.


Figure 1. Final puzzle configuration

Your job is to write a program that reads the puzzle configuration and prints the minimum sequence of movements required to reach the final position. We will use the following integer values to encode each type of piece:


0 grey separator
1 yellow triangle
2 yellow separator
3 cyan triangle
4 cyan separator
5 violet triangle
6 violet separator
7 green triangle
8 green separator
9 red triangle
10 red separator


A puzzle configuration will be described using 24 integers, the first 12 to describe the left wheel configuration and the last 12 for the right wheel. The first integer represents the bottom right separator of the left wheel and the next eleven integers describe the left wheel clockwise. The thirteenth integer represents the bottom left separator of right wheel and the next eleven integers describe the right wheel counter-clockwise.

The final position is therefore encoded like:

0 3 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1

If for instance we rotate the left wheel clockwise one position from the final configuration (as shown in Figure 2) the puzzle configuration would be encoded like:

2 1 0 3 4 3 0 5 6 5 0 1 0 7 8 7 0 9 10 9 0 5 0 1


Figure 2. The puzzle after rotating the left wheel on step clockwise from the final configuration.

Input 

Input for your program consists of several puzzles. The first line of the input will contain an integer n specifying the number of puzzles. There will then be n lines each containing 24 integers separated with one white space, describing the initial puzzle configuration as explained above.

Output 

For each configuration your program should output one line with just one number representing the solution. Each movement is encoded using one digit from 1 to 4 in the following way:


1 Left Wheel Clockwise rotation
2 Right Wheel Clockwise rotation
3 Left Wheel Counter-Clockwise rotation
4 Right Wheel Counter-Clockwise rotation


No space should be printed between each digit. Since multiple solutions could be found, you should print the solution that is encoded as the smallest number. The solution will never require more than 16 movements.

If no solution is found you should print ``NO SOLUTION WAS FOUND IN 16 STEPS". If you are given the final position you should print ``PUZZLE ALREADY SOLVED".

Sample Input  /h2>

3 0 3 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1 0 3 4 5 0 3 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1 0 9 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 3 0 1 2 1 

Sample Output 

PUZZLE ALREADY SOLVED 1434332334332323 NO SOLUTION WAS FOUND IN 16 STEPS 



Miguel A. Revilla
2000-02-09

目描述:

那有的, 要求起始到目, 操作有四左, 右, 左逆, 右逆, 但只求 16 步以(含)的解。出的候字典序越小越好, 操作次越少越好。

目解法:

操作 4 , 最多 16 步, 因此最到 4^16 = 2^32 基本上不在限通,
因此考剖半, 先目使用 bfs 找到 8 步的最佳解, 最多 2^16 = 65536 ,
然後每起始也使用 bfs 找到 8 步的解, 看能不能撞到理的解。

因此每操作只需要 O(65536), 降了非常多。
同也是向 bfs 的 O(sqrt(n)) 的思路。



#include <stdio.h>
#include <iostream>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
map<string, string> R;
string left1(string v) {
    static char t1, t2;
    string tn;
    tn = v;
    t1 = tn[11], t2 = tn[10];
    tn[11] = tn[9], tn[10] = tn[8], tn[9] = tn[7];
    tn[8] = tn[6], tn[7] = tn[5], tn[6] = tn[4];
    tn[5] = tn[3], tn[4] = tn[2], tn[3] = tn[1];
    tn[2] = tn[0], tn[1] = t1, tn[0] = t2;
    return tn;
}
string right2(string v) {
    static char t1, t2;
    string tn;
    tn = v;
    t1 = tn[9], t2 = tn[10];
    tn[9] = tn[11], tn[10] = tn[12], tn[11] = tn[13];
    tn[12] = tn[14], tn[13] = tn[15], tn[14] = tn[16];
    tn[15] = tn[17], tn[16] = tn[18], tn[17] = tn[19];
    tn[18] = tn[20], tn[19] = t1, tn[20] = t2;
    return tn;
}
string left3(string v) {
    static char t1, t2;
    string tn;
    tn = v;
    t1 = tn[9], t2 = tn[10];
    tn[9] = tn[11], tn[10] = tn[0], tn[11] = tn[1];
    tn[0] = tn[2], tn[1] = tn[3], tn[2] = tn[4];
    tn[3] = tn[5], tn[4] = tn[6], tn[5] = tn[7];
    tn[6] = tn[8], tn[7] = t1, tn[8] = t2;
    return tn;
}
string right4(string v) {
    static char t1, t2;
    string tn;
    tn = v;
    t1 = tn[11], t2 = tn[10];
    tn[11] = tn[9], tn[10] = tn[20], tn[9] = tn[19];
    tn[20] = tn[18], tn[19] = tn[17], tn[18] = tn[16];
    tn[17] = tn[15], tn[16] = tn[14], tn[15] = tn[13];
    tn[14] = tn[12], tn[13] = t1, tn[12] = t2;
    return tn;
}
void build() {//bfs
    R["034305650121078709T90"] = "";
    queue<string> Q;
    string tn, next;
    Q.push("034305650121078709T90");
    while(!Q.empty()) {
        tn = Q.front(), Q.pop();
        string &step = R[tn];
        if(step.length() >= 8)//stop extend
            continue;
        // 1 left wheel clockwise, build by inverse
        next = left3(tn);
        string &o1 = R[next];
        if(o1 == "")    o1 = "1" + step, Q.push(next);
        // 2 right wheel clockwise
        next = right4(tn);
        string &o2 = R[next];
        if(o2 == "")    o2 = "2" + step, Q.push(next);
        // 3 left wheel counter-clockwise
        next = left1(tn);
        string &o3 = R[next];
        if(o3 == "")    o3 = "3" + step, Q.push(next);
        // 4 right wheel counter-clockwise
        next = right2(tn);
        string &o4 = R[next];
        if(o4 == "")    o4 = "4" + step, Q.push(next);
    }
}
void sol(string tn) {
    map<string, string> rR;
    map<string, string>::iterator it;
    string next;
    queue<string> Q;
    rR[tn] = "";
    Q.push(tn);
    while(!Q.empty()) {
        tn = Q.front(), Q.pop();
        string &step = rR[tn];
        it = R.find(tn);
        if(it != R.end()) {
            cout << step << it->second << endl;
            return;
        }
        if(step.length() >= 8)
            continue;
        next = left1(tn);
        string &o1 = rR[next];
        if(o1 == "")    o1 = step+"1", Q.push(next);
        // 2 right wheel clockwise
        next = right2(tn);
        string &o2 = rR[next];
        if(o2 == "")    o2 = step+"2", Q.push(next);
        // 3 left wheel counter-clockwise
        next = left3(tn);
        string &o3 = rR[next];
        if(o3 == "")    o3 = step+"3", Q.push(next);
        // 4 right wheel counter-clockwise
        next = right4(tn);
        string &o4 = rR[next];
        if(o4 == "")    o4 = step+"4", Q.push(next);
    }
    puts("NO SOLUTION WAS FOUND IN 16 STEPS");
}
int main() {
    build();
    int testcase;
    scanf("%d", &testcase);
    while(testcase--) {
        int i, x;
        string s = "";
        for(i = 0; i < 24; i++) {
            scanf("%d", &x);
            if(i < 21) {
                if(x == 10)
                    s += 'T';
                else
                    s += (char)(x + '0');
            }
        }
        if(s == "034305650121078709T90")
            puts("PUZZLE ALREADY SOLVED");
        else
            sol(s);
    }
    return 0;
}
/*
10
0 3 4 3 0 5 6 5 0 1 2 1 0 7 8 7 0 9 10 9 0 1 2 1
*/

台: Morris
人(1,624) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA] 815 - Flooded!
此分上一篇:[UVA][dp] 607 - Scheduling Lectures

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86