LightOJ-1165 Digit Dancing(BFS+康托展开)

本文介绍了一道关于数字排列组合的算法题目DigitDancing。通过BFS搜索算法寻找最少的操作次数来将数字按升序排列。文章详细展示了如何使用康展开压缩状态,并通过剪枝优化搜索过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Digit Dancing
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

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();
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值