题目大意:
Patience游戏的玩法是:使用一个14列棋盘和扑克牌。首先把A放到第一列,第二列留空,然后在后面十二列中随机排列2-K. 你每次可以将空位前数字加一拿到空位来。胜利条件为前13列为按顺序摆放的牌,没有胜利时无法行动则失败。
现在前14-n列都已完成,空位以及剩下的牌在后n列中随机排列。问有多少种排列能胜利。
居然要打表差评!
特殊的暴力技巧:从结果往前搜
暴力渣代码:
#include <cstdio>
#include <algorithm>
using std::swap;
int n, a[14], ans;
void dfs(int x)
{
ans++;
for(int i = 1; i <= n; ++i)
{
if(i != x && a[i-1]+1 == a[i])
{
swap(a[i], a[x]);
dfs(i);
swap(a[i], a[x]);
}
}
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; ++i) a[i] = i;
a[n] = -1;
dfs(n);
printf("%d\n", ans);
return 0;
}
之后打表不解释。
#include <cstdio>
const int f[14] = {0, 1, 2, 5, 14, 47, 189, 891, 4815, 29547, 203173, 1548222, 12966093, 118515434};
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int n; scanf("%d", &n);
printf("%d\n", f[n]);
#ifndef ONLINE_JUDGE
fclose(stdin), fclose(stdout);
#endif
return 0;
}