【二进制枚举+LCS】Card Hand Sorting

本文探讨了一种在卡牌游戏中按花色和等级排序卡牌的算法,利用二进制枚举和最长公共子序列(LCS)技术,解决最小移动次数问题。通过状态压缩和全排列,算法有效地找到最优解。

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

【二进制枚举+LCS】Card Hand Sorting

题目描述

When dealt cards in the card game Plump it is a good idea to start by sorting the cards in hand by suit and rank. The different suits should be grouped and the ranks should be sorted within each suit. But the order of the suits does not matter and within each suit, the cards may be sorted in either ascending or descending order on rank. It is allowed for some suits to be sorted in ascending order and others in descending order.
Sorting is done by moving one card at a time from its current position to a new position in the hand, at the start, end, or in between two adjacent cards. What is the smallest number of moves required to sort a given hand of cards?

输入

The first line of input contains an integer n (1 ≤ n ≤ 52), the number of cards in the hand. The second line contains n pairwise distinct space-separated cards, each represented by two characters. The first character of a card represents the rank and is either a digit from 2 to 9 or
one of the letters T , J , Q , K , and A representing Ten, Jack, Queen, King and Ace, respectively, given here in increasing order. The second character of a card is from the set { s , h , d , c } representing the suits spades ♠, hearts ♥, diamonds ♦, and clubs ♣.

输出

Output the minimum number of card moves required to sort the hand as described above.

样例输入

7
9d As 2s Qd 2c Jd 8h

样例输出

2

看一眼,52,嗯 状态压缩 暴力 二进制枚举?然后,怎么换啊,不会。好难。。。
比赛结束,LCS,嗯,会了
LCS差点不会写...尴尬
通过移位符判断位置。

#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<char,int>mp;
inline void init()
{
    for(int i=2;i<=9;++i){
        mp[i+'0']=i;
    }
    mp['T']=10;
    mp['J']=11;
    mp['Q']=12;
    mp['K']=13;
    mp['A']=14;
    mp['s']=0;
    mp['h']=1;
    mp['d']=2;
    mp['c']=3;
}
struct node
{
    int id;
    int val;
    int block;
    int k;
}s[55];
int dp[55],n;
inline bool cmp(node x,node y)
{
    if(x.k==y.k){
        return x.val<y.val;
    }
    return x.k<y.k;
}
inline int lcs(){
   memset(dp,0,sizeof(dp));
   int ans=1;
   dp[0]=1;
   for(int i=1;i<n;++i){
        dp[i]=1;
    for(int j=0;j<i;++j){
        if(s[i].id>s[j].id){
            dp[i]=max(dp[j]+1,dp[i]);
        }
    }
    ans=max(dp[i],ans);
   }
   return ans;
}
int main()
{
    init();
    scanf("%d",&n);
    char str[3];
    for(int i=0;i<n;++i){
        scanf("%s",str);
        s[i].val=mp[str[0]];
        s[i].block=mp[str[1]];
        s[i].id=i;
    }
    int arr[4]={0,1,2,3},ans=1e9;
    do{
        for(int i=0;i<16;++i){
            for(int j=0;j<n;++j){
                s[j].k=arr[s[j].block];
                s[j].val=abs(s[j].val)*(((i>>s[j].k)&1)!=1?-1:1);
            }
            sort(s,s+n,cmp);
            ans=min(ans,n-lcs());
        }
    }while(next_permutation(arr,arr+4));//全排列
    printf("%d\n",ans);
    return 0;
}

转载于:https://www.cnblogs.com/smallocean/p/9749053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值