码迷,mamicode.com
首页 > 其他好文 > 详细

方格填数

时间:2019-04-01 17:06:02      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:cst   algorithm   span   ext   using   NPU   tar   bsp   star   

在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);
}

 

方格填数

标签:cst   algorithm   span   ext   using   NPU   tar   bsp   star   

原文地址:https://www.cnblogs.com/8023spz/p/10637229.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!