Sha 11198-dancing digits

Source: Internet
Author: User

Problem ddancing 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 DigitXBy itselfX, And denote a female Digit
XBy-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 shoshould 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 81 2 3 4 5 6 7 81 2 3 5 -4 6 7 81 2 3 5 4 6 7 82 -8 -4 5 6 7 3 -10
Output for the sample input
Case 1: 1Case 2: 0Case 3: 1Case 4: -1Case 5: 3
Returns two numbers with different numbers and prime numbers. One can be moved to the left or right of the other, and the minimum number of exchanges is obtained to sort the sequence in ascending order;
Obviously BFs. If the ignore symbol is used, all the conditions are 8! = 40320,
Full sorting can be perfectly hashed
For example, the hash value of 1 3 7 2 4 6 9 5 8 is 0*0! + 0*1! + 0*2! + 2*3! + 1*4! + 1*5! + 0*6! + 3*7! + 1*8! = 55596 <9! For specific reasons, you can check out some mathematical books, where the hash value of 1 2 3 4 5 6 7 8 9 is 0, the hash value of 8 7 6 5 4 3 2 1 0 is (9! -1) the maximum value, while the other values are from 0 to (9! -1), and all are unique.

For example, the arrangement of three elements

Sort hash in reverse order

123 000 0132 001 2213 010 1231 002 4312 011 3321 012 5

The hash value is not in ascending order of the original full sorting, but it can ensure no collision.

Open 8! The size array determines the weight. Each time you can use dance, there are four different types of swapping;

At the beginning, when there were a lot of steps, we automatically jumped out of BFS. Instead of writing BFS into a function and calling it recursively, this caused stack overflow. After checking for a long time, we finally found that 0.524 S AC

# Include <stdio. h> # include <string. h> # include <math. h> struct node {int A [9], time;} Q [40321]; int prime [16] =, 40321}, visit [120,720,504], exp [11] = {, 0}, top, tail, F; int Hash (INT team []) {int I, J, K, S = 0; for (I = 8; I> = 2; I --) {k = 0; For (j = 1; j <I; j ++) if (ABS (TEAM [J])> ABS (TEAM [I]) + + K; S = S + K * exp [I];} return s ;}; int main () {int n = 0, I, J, K, team [9], T, temp; while (scanf ("% d", & Q [1]. A [1]), Q [1]. A [1]) {for (I = 2; I <= 8; I ++) scanf ("% d", & Q [1]. A [I]); memset (visit, 0, sizeof (visit); visit [Hash (Q [1]. a)] = 1; Top = 1; tail = 1; F = 0; Q [1]. time = 0; while (F = 0) & (top <= tail) // in the future, BFs will still be written as non-Recursive security. Stack Overflow is really a headache, after one morning, I thought of {If (Hash (Q [Top]. a) = 0) {f = 1; break;} for (I = 1; I <= 7; I ++) for (j = I + 1; j <= 8; j ++) {If (Q [Top]. A [I] * Q [Top]. A [J] <0) & (prime [ABS (Q [Top]. A [I]) + ABS (Q [Top]. A [J])] = 1) {for (k = 1; k <= 8; k ++) team [k] = Q [Top]. A [k]; t = team [J]; for (k = J; k> I; k --) team [k] = team [k-1]; temp = team [I + 1]; team [I + 1] = T; If (visit [Hash (Team)] = 0) {visit [Hash (Team)] = 1; ++ tail; For (k = 1; k <= 8; k ++) Q [tail]. A [k] = team [k]; Q [tail]. time = Q [Top]. time + 1 ;}team [I + 1] = temp; team [I] = T; If (visit [Hash (Team)] = 0) {visit [Hash (Team)] = 1; ++ tail; For (k = 1; k <= 8; k ++) Q [tail]. A [k] = team [k]; Q [tail]. time = Q [Top]. time + 1 ;}for (k = 1; k <= 8; k ++) team [k] = Q [Top]. A [k]; t = team [I]; for (k = I; k <j; k ++) team [k] = team [k + 1]; temp = team [J-1]; team [J-1] = T; If (visit [Hash (Team)] = 0) {visit [Hash (Team)] = 1; ++ tail; For (k = 1; k <= 8; k ++) Q [tail]. A [k] = team [k]; Q [tail]. time = Q [Top]. time + 1 ;}team [J-1] = temp; team [J] = T; If (visit [Hash (Team)] = 0) {visit [Hash (Team)] = 1; ++ tail; For (k = 1; k <= 8; k ++) Q [tail]. A [k] = team [k]; Q [tail]. time = Q [Top]. time + 1 ;}}++ top ;}printf ("case % d:", ++ N); If (f) printf ("% d \ n ", Q [Top]. time); else printf ("-1 \ n");} 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.