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 "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

vector<vector<int>> table(10, vector<int>(10));

int check(string s)
{
    int res = 0;
    res = table[res][s[0] - '0']; // a
    res = table[res][s[1] - '0']; // b
    res = table[res][s[2] - '0']; // c
    res = table[res][s[3] - '0']; // d
    res = table[res][s[4] - '0']; // e
    return res;
}

int calc(int x)
{
    int res = 0, tmp = x;
    res = table[res][tmp / 1000];
    tmp %= 1000;
    res = table[res][tmp / 100];
    tmp %= 100;
    res = table[res][tmp / 10];
    tmp %= 10;
    res = table[res][tmp];
    return res;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            cin >> table[i][j];
        }
    }
    int res = 0;
    for (int i = 0; i <= 9999; i++)
    {
        int x = i * 10 + calc(i);
        string s = to_string(x);
        while (s.length() < 5)
        {
            s = '0' + s;
        }
        bool f = false;
        for (int j = 0; j < 4; j++)
        {
            string t = s;
            swap(t[j], t[j + 1]);
            if (s == t)
                continue;
            if (check(t) == 0)
                f = true;
        }
        for (int j = 0; j < 5; j++)
        {
            for (int k = 0; k <= 9; k++)
            {
                string t = s;
                t[j] = '0' + k;
                if (s == t)
                    continue;
                if (check(t) == 0)
                    f = true;
            }
        }
        if (f)
            res++;
    }
    cout << res << endl;
}