方格填数

在2行5列的格子中填入1到10的数字。
要求:
相邻的格子中的数,右边的大于左边的,下边的大于上边的。

如【图1.png】所示的2种,就是合格的填法。

请你计算一共有多少种可能的方案。

请提交该整数,不要填写任何多余的内容(例如:说明性文字)。

答案:

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
long long n;
int s[10],c;
bool vis[10];
int start(int k) {
    int d = 0;
    if(k % 5) d = max(d,s[k - 1] + 1);
    if(k - 5 >= 0) d = max(d,s[k - 5] + 1);
    return d;
}
void dfs(int k) {
    if(k >= 10) c ++;
    for(int i = start(k);i < 10;i ++) {
        if(!vis[i]) {
            vis[i] = true;
            s[k] = i;
            dfs(k + 1);
            vis[i] = false;
        }
    }
}
int main() {
    dfs(0);
    printf("%d",c);
}
原文地址:https://www.cnblogs.com/8023spz/p/10637229.html