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 table[10][10];

int check(vector<int> x){
  int tmp = 0;
  for(int i=0;i<5;i++){
    tmp = table[tmp][x[i]];
  }
  return tmp;
}

int calc(vector<int> x){
  int tmp = 0;
  for(int i=0;i<4;i++){
    tmp = table[tmp][x[i]];
  }
  return tmp;
}

int main(){
  for(int i=0;i<10;i++)for(int j=0;j<10;j++){
    scanf("%d", &table[i][j]);
  }
  int ans = 0;
  vector<int> x(5, 0);
  for(int i1=0;i1<10;i1++){
    for(int i2=0;i2<10;i2++){
      for(int i3=0;i3<10;i3++){
        for(int i4=0;i4<10;i4++){
          x[0]=i1;
          x[1]=i2;
          x[2]=i3;
          x[3]=i4;
          x[4]=calc(x);

          bool flag = true;
          for(int j=0;j<5;j++){
            vector<int> one = x;
            for(int k=0;k<10;k++){
              if(x[j] != k){
                one[j] = k;
                if(check(one) == 0)flag = false;
              }
            }
          }

          for(int j = 0; j < 4; j++){
            vector<int> two = x;
            if(x[j] != x[j+1]){
              two[j] = x[j+1];
              two[j+1] = x[j];
              if(check(two) == 0)flag = false;
            }
          }

          if(!flag)ans++;

        }
      }
    }
  }

  printf("%d\n", ans);

  return 0;
}