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 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 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
異號且和為素數的2個數字,一個可以移動到另一個的左邊或右邊,求最小交換次數使序列升序;
很明顯的bfs,忽略符號則所有情況為8!=40320,
全排列是可以完美雜湊的
例如,1 3 7 2 4 6 9 5 8 的雜湊值等於: 0*0! + 0*1! + 0*2! + 2*3! + 1*4! + 1*5! + 0*6! + 3*7! + 1*8! = 55596 <9! 具體的原因可以去查查一些數學書,其中1 2 3 4 5 6 7 8 9 的雜湊值是0 最小,8 7 6 5 4 3 2 1 0 的雜湊值是(9!-1)最大,而其他值都在0 到(9!-1)中,且均唯一。 例如三個元素的排列
排列 逆序 Hash
123 000 0132 001 2213 010 1231 002 4312 011 3321 012 5
雜湊值不是按原來的全排列的升序,但是可以保證無碰撞。
開一個8!大小數組判重;每次可以dance有四種不同交換的種類;
開始莫名奇妙遇到步數多的就自動跳出bfs,不因該把bfs寫成函數再遞迴調用,導致棧溢出,查了好久,終於發現,0.524s Ac
#include<stdio.h>#include<string.h>#include<math.h>struct node{int a[9],time;}q[40321];int prime[16]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0},visit[40321], Exp[11]={0,1,1,2,6,24,120,720,5040},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)) //以後bfs還是寫成非遞迴的安全,棧溢出真的很蛋疼,找了一上午才想到 { 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;}