uva 11198 - Dancing Digits

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
异号且和为素数的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    0 132    001    2 213    010    1 231    002    4 312    011    3 321    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;
}


 

你可能感兴趣的:(input,each,Go,output,permutation)