solution.cpp 1.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
//搭积木 768
#include <bits/stdc++.h>
using namespace std;
int row = 4, col;
int seq[5][8]; //下标从1开始
int tmp[11];
int cnt = 0;
bool used[10];

void change()
{ //将一维数组转换成二维数组
    int cur = 1;
    for (int i = 1; i <= row; ++i)
    {
        for (int j = 1; j <= i; ++j)
        {
            seq[i][j] = tmp[cur++];
        }
    }
}

bool judge()
{ //判断二维数组是否符合条件
    for (int i = 1; i <= row - 1; ++i)
    {
        for (int j = 1; j <= i; ++j)
        {
            if (seq[i][j] > seq[i + 1][j] || seq[i][j] > seq[i + 1][j + 1])
                return false;
        }
    }
    return true;
}
int ans = 0;

void dfs(int idx)
{
    //idx:当前要操作的下标(从1开始)
    if (idx == 11)
    {
        change();
        if (judge())
        {
            cnt++;
            return;
        }
        return;
    }
    for (int i = 0; i <= 9; ++i)
    {
        if (!used[i])
        {
            tmp[idx] = i;
            used[i] = true;
            dfs(idx + 1);
            used[i] = false;
        }
    }
}

int main()
{
    memset(used, false, sizeof(used));
    dfs(1);
    cout << cnt << endl;
    return 0;
}