LightOJ-1165 Digit Dancing(BFS+康托展开)
来源:互联网 发布:8080端口怎么关闭 编辑:程序博客网 时间:2024/06/14 20:38
Description
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
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains exactly eight integers in a single line. The absolute values of these integers form a permutation of {1, 2, 3, 4, 5, 6, 7, 8}.
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
5
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
Sample Output
Case 1: 1
Case 2: 0
Case 3: 1
Case 4: -1
Case 5: 3
题意:如果一个正数和一个负数的绝对值之和是质数,那么可以把其中一个数移到另一个数的前面或者后面,问要最少移动多少次才能把这些数的绝对值按升序排序
题解:BFS,因为只有8个数,可以先用康拓展开压缩每个状态,搜索时注意剪枝即可
#include<cstdio>#include<algorithm>#include<string.h>#include<queue>using namespace std;const int INF = 0x6f6f6f6f;int fac[]={1,1,2,6,24,120,720,5040,40320};int prime[]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1},a[10],ans;int vis[5040*100];struct node{ int x[10],step;}now,nxt;bool check(node now){ for(int i=0;i<7;i++) if(abs(now.x[i])>abs(now.x[i+1])) return false; return true;}int Cantor(node& now){ int cnt,ret=0; for(int i=0;i<8;i++){ cnt=0; for(int j=i+1;j<8;j++){ if(now.x[j]<now.x[i]) cnt++; } ret+=cnt*fac[7-i]; } return ret;}void solve(int flag,int i,int j){ int index=0; if(flag){ if(i==0) nxt.x[index++]=now.x[j]; for(int k=0;k<8;k++){ if(k==j) continue; else if(k==i-1){ nxt.x[index++]=now.x[k]; nxt.x[index++]=now.x[j]; } else nxt.x[index++]=now.x[k]; } } else{ if(i==7) nxt.x[7]=now.x[j]; for(int k=0;k<8;k++){ if(k==j) continue; else if(k==i+1){ nxt.x[index++]=now.x[j]; nxt.x[index++]=now.x[k]; } else nxt.x[index++]=now.x[k]; } } // printf("%d\n",index);}void bfs(){ ans=INF; queue<node>q; now.step=0; for(int i=0;i<8;i++) now.x[i]=a[i]; q.push(now); vis[Cantor(now)]=1; while(!q.empty()){ now=q.front();q.pop(); if(check(now)) {ans=now.step;break;} for(int i=0;i<8;i++){ for(int j=0;j<8;j++){ if(now.x[i]*now.x[j]<0&&prime[abs(now.x[i])+abs(now.x[j])]){ nxt.step=now.step+1; if(j!=i+1) { solve(0,i,j); int tmp=Cantor(nxt); if(!vis[tmp]){ vis[tmp]=1; q.push(nxt); } } if(j!=i-1) { solve(1,i,j); int tmp=Cantor(nxt); if(!vis[tmp]){ vis[tmp]=1; q.push(nxt); } } } } } } if(ans==INF) printf("-1\n"); else printf("%d\n",ans);}int main(){ int T; // freopen("in.txt","r",stdin); scanf("%d",&T); for(int cas=1;cas<=T;cas++){ printf("Case %d: ",cas); memset(vis,0,sizeof(vis)); for(int i=0;i<8;i++) scanf("%d",&a[i]); bfs(); }}
- LightOJ-1165 Digit Dancing(BFS+康托展开)
- [搜索] LightOj 1165 - Digit Dancing
- loj 1165(bfs+康托展开)
- hdu1430(康托展开+BFS)
- 康托展开+bfs-2
- USACO msquare (BFS+康托展开)
- hdu1430魔板(BFS+康托展开)
- 【USACO3.2.5】魔板 康托展开/BFS
- [BFS+康托展开]Hdu 1043 Eight
- hdu 3567 康托展开 +BFS
- [U]3.2.5 Magic Squares 康托展开+BFS
- [POJ]1077 Eight 八数码:康托展开+BFS
- hdu1043Eight (经典的八数码)(康托展开+BFS)
- hdu1043Eight (经典的八数码)(康托展开+BFS)
- poj 1077 bfs+康托展开(8数码问题)
- 【康托展开+状压BFS】poj1077 Eight(八数码问题)
- HDU 1043 Eight(bfs+康托展开)
- hdu 1430 魔板(bfs+预处理+康托展开)
- 匿名类
- js设计模式-桥接模式-单例包装器
- Arch软件安装
- 旋转的星空背景
- pycharm 无法输入中文,ubuntu 14.04中文
- LightOJ-1165 Digit Dancing(BFS+康托展开)
- 适合新手使用的网页设计软件有哪些
- 关于sizeof的一些计算
- 如何用Spring将Service注入到ServletContextListener中
- Codeforces Round #376 (Div. 2) F 区间求和+二分
- C++“准”标准库Boost学习指南(7):Boost.Variant
- 选择符总结
- mysql(一):mysql数据库以及创建表
- js 将json字符串转换为json对象的方法解析