当前位置: 代码迷 >> 综合 >> card sorting
  详细解决方案

card sorting

热度:10   发布时间:2024-01-11 16:13:01.0


对纸牌正反排序


暴力  , 状压

#include <bits/stdc++.h>
using namespace std;int nu[200] , co[200] , best[200];
int term[4] ;
bool judge(int i , int j , int zhuangya){if(co[i] == co[j]){if((zhuangya>>co[i]) & 1)return nu[i] < nu[j] ;elsereturn nu[i] > nu[j] ;}else{return term[ co[i] ] < term[ co[j] ] ;}
}int main(){int n ;map<char , int> num ;num['T'] = 10 ;num['J'] = 11 ;num['Q'] = 12 ;num['K'] = 13 ;num['A'] = 14 ;map<char , int> color ;color['s'] = 0 ;color['h'] = 1 ;color['d'] = 2 ;color['c'] = 3 ;char read[5] ;while( ~ scanf("%d" , &n)){for(int i = 0 ; i < n ; i ++ ){scanf("%s" , read) ;if(num.count( read[0] )) nu[i] = num[ read[0] ] ;else nu[i] = read[0] - '0' ;co[i] = color[ read[1] ] ;}for(int i = 0 ; i < 4 ; i ++ ) term[i] = i ;int ans = 200 ;do{for(int zhuangya = 0 ; zhuangya < 16 ; zhuangya ++ ){for(int i = n - 1 ; i >= 0 ; i -- ){best[i] = 1 ;for(int j = i + 1 ; j < n ; j ++ ){if(judge(i , j , zhuangya))best[i] = max(best[i] , best[j] + 1 );}ans = min(ans , n - best[i]) ;}}}while(next_permutation(term , term + 4)) ;cout << ans << endl ;}return 0 ;
}