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;
int check(vector<int> &d2, vector<vector<int>> &x){
  int a = d2[0];
  int b = d2[1];
  int c = d2[2];
  int d = d2[3];
  int e = d2[4];
  return x[x[x[x[x[0][a]][b]][c]][d]][e];
}
int main(){
  vector<vector<int>> x(10, vector<int>(10));
  for (int i = 0; i < 10; i++){
    for (int j = 0; j < 10; j++){
      cin >> x[i][j];
    }
  }
  int ans = 0;
  for (int i = 0; i < 10000; i++){
    string S = to_string(i);
    while (S.size() < 4){
      S = '0' + S;
    }
    vector<int> d(5);
    for (int j = 0; j < 4; j++){
      d[j] = S[j] - '0';
    }
    d[4] = x[x[x[x[0][d[0]]][d[1]]][d[2]]][d[3]];
    bool err = false;
    for (int i = 0; i < 5; i++){
      for (int j = 0; j < 10; j++){
        if (d[i] != j){
          vector<int> d2 = d;
          d2[i] = j;
          if (check(d2, x) == 0){
            err = true;
          }
        }
      }
    }
    for (int i = 0; i < 4; i++){
      if (d[i] != d[i + 1]){
        vector<int> d2 = d;
        swap(d2[i], d2[i + 1]);
        if (check(d2, x) == 0){
          err = true;
        }
      }
    }
    if (err){
      ans++;
    }
  }
  cout << ans << endl;
}