题解 P1837 【单人纸牌】
写在前面
- 感谢巨佬 yu__xuan 的帮助!
- 原本题解区的大佬们大都写的九层循环,其实此题如果写成状压,可以将这九层循环写成一层,非但简洁、代码可读性强,常数也比直接九维 dp 小。
算法思路
由于每一行都只有四张牌,考虑写成五进制状压 dp。
设当前状态为
因此,若设第
其中
边界条件:
倒序枚举所有状态,每当找到一个当前答案不为
由于一直在拿牌,表示状态的变量会逐渐减小,倒序枚举状态时可行的。
Tips
-
读入的时候用类似于快速读入的方式过滤一下不合法字符可以极大地简化读入部分的代码。
-
扑克牌的点数不等同于真实的扑克牌的点数,因此统计的时候不需要再对点数进行处理,直接将
char转成int存下来即可。
Code
#include<bits/stdc++.h>
#define LF double
const int pow5[] = {1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125};
using namespace std;
LF f[1953125];
int a[10][5];
char Getch() {char ch = getchar(); while((!isalpha(ch)) && (!isdigit(ch))) ch = getchar(); return ch;}
int main() {
for(register int i = 0; i < 9; ++i) {
for(register int j = 1; j <= 4; ++j) {
a[i][j] = Getch(); Getch();
}
}
f[1953124] = 1.0;
for(register int t = pow5[9] - 1; t >= 0; --t) {
if(f[t] == 0) continue;
LF choise = 0;
for(register int p1 = 0; p1 < 9; ++p1) {
for(register int p2 = p1 + 1; p2 < 9; ++p2) {
if((a[p1][t / pow5[p1] % 5] == a[p2][t / pow5[p2] % 5]) && ((t / pow5[p1] % 5) > 0) && ((t / pow5[p2] % 5) > 0)) choise++;
}
}
LF P = f[t] * 1.0 / choise;
for(register int p1 = 0; p1 < 9; ++p1) {
for(register int p2 = p1 + 1; p2 < 9; ++p2) {
if((a[p1][t / pow5[p1] % 5] == a[p2][t / pow5[p2] % 5]) && ((t / pow5[p1] % 5) > 0) && ((t / pow5[p2] % 5) > 0)) {
f[t - pow5[p1] - pow5[p2]] += P;
}
}
}
}
printf("%lf", f[0]);
return 0;
}