Problem: Example

Input

0 3 1 7 5 9 8 6 4 2
7 0 9 2 1 5 4 8 6 3
4 2 0 6 8 7 1 3 5 9
1 7 5 0 9 8 3 4 2 6
6 1 2 3 0 4 5 9 7 8
3 6 7 4 2 0 9 5 8 1
5 8 6 9 7 2 0 1 3 4
8 9 4 5 3 6 2 0 1 7
9 4 3 8 6 1 7 2 0 5
2 5 8 1 4 3 6 7 9 0


Output

0

Solution (in CPP):

Solution:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

using lint = long long;
using ldouble = long double;

vector<int> tov(int N) {
    vector<int> ret;
    for (int i = 0; i < 4; ++i) {
        ret.push_back(N % 10);
        N /= 10;
    }
    reverse(ret.begin(), ret.end());
    return ret;
}

int main() {
    vector<vector<int>> table(10, vector<int>(10));
    for (auto &v : table) {
        for (auto &e : v) {
            cin >> e;
        }
    }

    auto calc_check = [&](vector<int> v) {
        int pre = 0;
        for (int i = 0; i < 4; ++i) {
            pre = table[pre][v[i]];
        }
        return pre;
    };

    auto check = [&](vector<int> v) {
        int pre = 0;
        for (int i = 0; i < 5; ++i) {
            pre = table[pre][v[i]];
        }
        return pre == 0;
    };

    int ans = 0;
    for (int id = 0; id < 10000; ++id) {
        vector<int> v = tov(id);
        v.push_back(calc_check(v));

        bool bad = false;
        for (int i = 0; i < 5; ++i) {
            int pre = v[i];
            for (int d = 0; d < 10; ++d) {
                v[i] = d;
                if (d != pre && check(v)) bad = true;
            }
            v[i] = pre;
        }

        for (int i = 0; i < 4; ++i) {
            if (v[i] == v[i + 1]) continue;
            swap(v[i], v[i + 1]);
            if (check(v)) bad = true;
            swap(v[i], v[i + 1]);
        }

        if (bad) ++ans;
    }

    cout << ans << endl;
    return 0;
}