solution.cpp 1.1 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
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

int a[10];
int ans;

bool check()
{
    if (abs(a[0] - a[1]) == 1 || abs(a[0] - a[3]) == 1 || abs(a[0] - a[4]) == 1 || abs(a[0] - a[5]) == 1)
        return false;
    if (abs(a[1] - a[2]) == 1 || abs(a[1] - a[4]) == 1 || abs(a[1] - a[5]) == 1 || abs(a[1] - a[6]) == 1)
        return false;
    if (abs(a[2] - a[5]) == 1 || abs(a[2] - a[6]) == 1)
        return false;
    if (abs(a[3] - a[4]) == 1 || abs(a[3] - a[7]) == 1 || abs(a[3] - a[8]) == 1)
        return false;
    if (abs(a[4] - a[5]) == 1 || abs(a[4] - a[7]) == 1 || abs(a[4] - a[8]) == 1 || abs(a[4] - a[9]) == 1)
        return false;
    if (abs(a[5] - a[6]) == 1 || abs(a[5] - a[8]) == 1 || abs(a[5] - a[9]) == 1)
        return false;
    if (abs(a[6] - a[9]) == 1)
        return false;
    if (abs(a[7] - a[8]) == 1 || abs(a[8] - a[9]) == 1)
        return false;
    return true;
}
int main()
{
    for (int i = 0; i <= 9; i++)
        a[i] = i;

    do
    {
        if (check())
            ans++;
    } while (next_permutation(a, a + 10));
    cout << ans;
    return 0;
}