UVa 11198:dancing Digits,rujia Liu's Divine title

Source: Internet
Author: User
Tags abs bool cas hash

Topic Link:

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=2139

Type: Implicit graph search, BFS, hash weight, simulation

Original title:

Digits like to dance. One day, 1, 2, 3, 4, 5, 6, 7 and 8 stand into 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 the WHO he/she wants to dance to the With-either left or immediate R Ight.

For simplicity, we denote a male digit x by itself x, and denote a female digit x by-x. Suppose the digits are into 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 he right, resulting {1, 2, 4,-3, 5, 6,-7, 8}. Note That-3 cannot dance with 5, since their sum 3+5=8 be 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 on increasing order (I Gnoring signs of course).

Input

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

Output

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

Sample Input

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

Output for the Sample Input

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

The main effect of the topic:

Numbers like to dance. One day, 1,2,3,4,5,6,7,8 stood in a row ready to dance. Numbers are also male and female, positive for men and negative for women. Every time a male figure can invite any female figure to dance with him, of course female figures can also be danced with any number of male figures. The premise is that the sum of the numbers of men and women (both absolute values) is a prime number. You can also dance with the numbers next to him (her), whether they are male or female.

Suppose the initial arrangement is {1, 2, 4, 5, 6,-7,-3, 8}, if-3 want to dance with 41, then-3 will have to go to the left and right of 4, if on the left then the arrangement becomes: {1, 2,-3, 4, 5, 6,-7, 8}, if on the right then the arrangement becomes { 1, 2, 4,-3, 5, 6,-7, 8}.

The problem is to give an initial arrangement, find a minimum number of invitations to dance, so that their arrangement becomes ascending (in absolute terms). If not, output-1.

Analysis and Summary:

This problem is a bit disgusting, it should be my solution compared to the setback, direct simulation of the process of State conversion, and then get a new arrangement state. The simulation process needs to be noted that the invitation is on the left or right of your position.

As long as a little more careful basic no problem.

Just because of the wrong one small letter and re a few times, are careless cause of trouble, depressed ...

* * UVa 11198-dancing Digits * Implicit graph Search, BFS * This article url:http://www.bianceng.cn/programming/sjjg/201410/45632.htm * time : 0.740s (UVa) * author:d_double/#include <iostream> #include <cstdio> #include <cstring>  
Include<cmath> using namespace std;  
typedef int STATE[8];  
const int hashsize = 1000003;  
State start;  
State que[50000];  
int head[hashsize], next[hashsize], step[50000], ans;  
     
int prime[] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1};  inline void init_lookup_table () {ans =-1;  
    Step[0] = 0;  
memset (head, 0, sizeof);  
    } inline int hash (state &s) {int v=0;  
    for (int i=0; i<8; ++i) v = v*10 + abs (s[i));  
Return (V & 0x7fffffff)% Hashsize;  
    } inline bool Try_to_insert (int s) {int h = hash (que[s]);  
    int u = head[h];  
        while (U) {if (memcmp (Que[u], que[s], sizeof (que[s)) ==0) return false;  
    U = next[u];  
    } Next[s] = Head[h]; head[h] = s;  
return true;  
    BOOL Is_ok (state &s) {for (int i=0; i<7; ++i) if (ABS (S[I)) > abs (S[I+1)) return false;  
return true; }//Invitation to dance after the permutation transforms void goto_dance (state &s, int u, int v, int dir) {if (dir==1) {//left if (U==v-1) RET  Urn  If it's on his left, don't move if (u==v+1) {//If it's right again, direct swap int tmp = S[u]; S[U]=S[V];  
        S[V] = tmp;  
        int t = S[u];  
            if (u>v) {for (int i=u; i>v;-i) s[i] = s[i-1];  
        S[V] = t;  
            } else{for (int i=u; i<v-1; ++i) s[i] = s[i+1];  
        S[v-1] = t;  } else{//Right if (u==v+1) return;//If it is on his right, do not move if (u==v-1) {int tmp =  S[u]; S[U]=S[V];  
        S[V] = tmp;  
        int t = S[u];  
            if (u>v) {for (int i=u; i>v+1;-i) s[i] = s[i-1];  
        S[v+1] = t; } else{for (inT I=u; i<v;  
            ++i) S[i] = s[i+1];  
        S[V] = t;  
    }} void BFs () {init_lookup_table ();  
    int front = 0, rear = 1;  
    memcpy (Que[0], start, sizeof (start));  
    Try_to_insert (0);  
        while (front < rear) {state &s = Que[front];  
        if (IS_OK (s)) {ans = Step[front]; for (int i=0; i<8; ++i) {for (int j=0; j<8; ++j) if (I!=j && (s[i]>0&&s[j]& lt;0 | |  
                s[i]<0&&s[j]>0)) {int sum = ABS (S[i]) +abs (s[j));  
                if (!prime[sum]) continue;  
                    Move to the left or right of the target: for (int k=1; k<=2; ++k) {state &t = que[rear];  
                    memcpy (t, S, sizeof (s));  
                    Goto_dance (T, I, J, K);  if (Try_to_insert (rear)) {Step[rear] = step[front]+1;  
                    ++rear;   
              }  }} ++front;  
    int main () {int cas = 1;  
        while (scanf ("%d", &start[0]), start[0]) {for (int i=1; i<8; ++i) scanf ("%d", &start[i]);  
        BFS ();  
    printf ("Case%d:%d\n", cas++, ans);  
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.