24h購物| | PChome| 登入
2012-10-07 11:20:00| 人氣435| 回應0 | 上一篇 | 下一篇

[UVA][bfs] 11198 - Dancing Digits

推薦 0 收藏 0 轉貼0 訂閱站台

Problem D

Dancing Digits

Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand in a line to have a wonderful party. Each time, a male digit can ask a female digit to dance with him, or a female digit can ask a male digit to dance with her, as long as their sum is a prime. Before every dance, exactly one digit goes to who he/she wants to dance with - either to its immediate left or immediate right.

For simplicity, we denote a male digit x by itself x, and denote a female digit x by -x. Suppose the digits are in order {1, 2, 4, 5, 6, -7, -3, 8}. If -3 wants to dance with 4, she must go either to 4's left, resulting {1, 2, -3, 4, 5, 6, -7, 8} or his right, resulting {1, 2, 4, -3, 5, 6, -7, 8}. Note that -3 cannot dance with 5, since their sum 3+5=8 is not a prime; 2 cannot dance with 5, since they're both male.

Given the initial ordering of the digits, find the minimal number of dances needed for them to sort in increasing order (ignoring signs of course).

Input

The input consists of at most 20 test cases. Each case contains exactly 8 integers in a single line. The absolute values of these integers form a permutation of {1, 2, 3, 4, 5, 6, 7, 8}. The last case is followed by a single zero, which should not be processed.

Output

For each test case, print the case number and the minimal number of dances needed. If they can never be sorted in increasing order, print -1.

Sample Input

1 2 4 5 6 -7 -3 8
1 2 3 4 5 6 7 8
1 2 3 5 -4 6 7 8
1 2 3 5 4 6 7 8
2 -8 -4 5 6 7 3 -1
0

Output for the Sample Input

Case 1: 1
Case 2: 0
Case 3: 1
Case 4: -1
Case 5: 3

Rujia Liu's Present 1: A Tiny Contest of Brute Force



#include <stdio.h>
#include <map>
#include <queue>
using namespace std;
int isprime(int n) {
    int i;
    for(i = 2; i < n; i++)
        if(n%i == 0)
            return 0;
    return 1;
}
int main() {
    int n[8], sign[9], cases = 0;
    int i, j, k, isp[9][9];
    for(i = 1; i <= 8; i++)
        for(j = 1; j <= 8; j++)
            isp[i][j] = isprime(i+j);
    while(scanf("%d", &n[0]), n[0]) {
        if(n[0] < 0)
            n[0] *= -1, sign[n[0]] = 1;
        else    sign[n[0]] = 0;
        int tmp = n[0];
        for(i = 1; i < 8; i++) {
            scanf("%d", &n[i]);
            if(n[i] < 0)
                n[i] *= -1, sign[n[i]] = 1;
            else    sign[n[i]] = 0;
            tmp = tmp*10 + n[i];
        }
        queue<int> Q;
        map<int, int> G;
        map<int, int>::iterator it;
        Q.push(tmp);
        G[tmp] = 0;
        int a[8], state, step;
        while(!Q.empty()) {
            tmp = Q.front();
            Q.pop();
            if(tmp == 12345678) {
                tmp = -1;
                break;
            }
            step = G[tmp];
            for(i = 7; i >= 0; i--)
                a[i] = tmp%10, tmp /= 10;
            for(i = 0; i < 8; i++) {
                for(j = 0; j < 8; j++) {
                    if(i == j)  continue;
                    if(sign[a[i]] != sign[a[j]] && isp[a[i]][a[j]]) {
                        /*left*/
                        for(k = 0, state = 0; k < j; k++) {
                            if(k == i)  continue;
                            state = state*10 + a[k];
                        }
                        state = state*10 + a[i], state = state*10 + a[j];
                        for(k = j+1; k < 8; k++) {
                            if(k == i)  continue;
                            state = state*10 + a[k];
                        }
                        if(G.find(state) == G.end()) {
                            G[state] = step+1;
                            Q.push(state);
                        }
                        /*right*/
                        for(k = 0, state = 0; k < j; k++) {
                            if(k == i)  continue;
                            state = state*10 + a[k];
                        }
                        state = state*10 + a[j], state = state*10 + a[i];
                        for(k = j+1; k < 8; k++) {
                            if(k == i)  continue;
                            state = state*10 + a[k];
                        }
                        if(G.find(state) == G.end()) {
                            G[state] = step+1;
                            Q.push(state);
                        }
                    }
                }
            }
        }
        printf("Case %d: ", ++cases);
        if(tmp == -1)
            printf("%d\n", G[12345678]);
        else
            puts("-1");
    }
    return 0;
}

台長: Morris
人氣(435) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][dfs] 10624 - Super Number
此分類上一篇:[UVA][brute] 11218 - KTV

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文