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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

using i64 = int64_t;

int main() {
    int table[10][10];
    for (int j = 0; j < 10; ++j) {
        for (int k = 0; k < 10; ++k) {
            cin >> table[j][k];
        }
    }

    int ans = 0;
    for (int n = 0; n < 10000; ++n) {
        int d[5];
        for (int j = 0; j < 4; ++j) {
            d[j] = (n / (int)pow(10, 3-j)) % 10;
        }

        { 
            int chk = 0;
            for (int j = 0; j < 4; ++j) {
                chk = table[chk][d[j]];
            }
            d[4] = chk;
        }

        int inc = 0;

        // altering one digit
        for (int j = 0; j < 5; ++j) {
            int ds[5];
            for (int k = 0; k < 5; ++k) {
                ds[k] = d[k];
            }

            for (int k = 0; k < 10; ++k) {
                ds[j] = k;

                bool ignore = true;
                for (int l = 0; l < 5; ++l) {
                    if (d[l] != ds[l]) ignore = false;
                }
                if (ignore) continue;

                int chk = 0;
                for (int l = 0; l < 5; ++l) {
                    chk = table[chk][ds[l]];
                }
                if (chk == 0) {
                    inc = 1;
                }
            }
        }

        // transposing two adjacent digits
        for (int j = 0; j < 4; ++j) {
            int ds[5];
            for (int k = 0; k < 5; ++k) {
                ds[k] = d[k];
            }
            swap(ds[j], ds[j+1]);

            bool ignore = true;
            for (int l = 0; l < 5; ++l) {
                if (d[l] != ds[l]) ignore = false;
            }
            if (ignore) continue;

            int chk = 0;
            for (int l = 0; l < 5; ++l) {
                chk = table[chk][ds[l]];
            }
            if (chk == 0) {
                inc = 1;
            }
        }

        ans += inc;
    }
    cout << ans << endl;

    return 0;
}