uva 11198 - Dancing Digits
来源:互联网 发布:洛奇英雄传自我优化 编辑:程序博客网 时间:2024/06/10 14:10
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 digitx 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;}
- uva 11198 - Dancing Digits
- UVA 11198 Dancing Digits
- UVA 11198 - Dancing Digits
- uva 11198 Dancing Digits
- uva 11198 - Dancing Digits 隐式图 bfs
- UVA 11198 Dancing Digits (BFS + 判重)
- uva 11198 Dancing Digits(隐式图搜索+哈细判重)
- uva 11198 - Dancing Digits 跳舞的数字 (BFS+HASH)
- UVA - 11198 Dancing Digits(bfs+hash+线性表)
- 11198 - Dancing Digits
- UVa 11198 - Dancing Digits,Rujia Liu的神题(二)
- UVa 11198 - Dancing Digits,Rujia Liu的神题(二)
- 11198 - Dancing Digits(BFS + hash判重)
- UVa 11332 - Summing Digits
- UVa 12527 - Different Digits
- UVa 11687 - Digits
- UVa 993 - Product of digits
- UVa 993 - Product of digits
- Java System.arraycopy()
- AsyncTask 介绍以及应用
- PKCS#11 用户级加密应用程序示例
- iOS三种Json方法解析国家气象局API
- 类型转化安全性问题
- uva 11198 - Dancing Digits
- 关于C语言的一个问题
- HTTP协议详解
- Effective C++之模板和泛型编程
- android开发之流量监控
- Android ApiDemo学习(二)notification——1 IncomingMessage
- linux 删除已输入的命令
- group_concat函数详解
- java请求http连接(servlet)类