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>
using namespace std;
int M[10][10];

int count(int &a, int &b, int &c, int &d, int &e){
  int cnt = 0;
  int A[5] = {a,b,c,d,e};
  for(int i = 0; i < 5; ++i){
    for(int j = 0; j < 10; ++j){
      int B[5];
      if(A[i] == j) continue;
      for(int k = 0; k < 5; ++k){
	if(k == i) B[k] = j;
	else B[k] = A[k];
      }
      if(!M[M[M[M[M[0][B[0]]][B[1]]][B[2]]][B[3]]][B[4]])
	++cnt;
    }
  }
  for(int i = 0; i < 4; ++i){
    if(A[i] == A[i+1]) continue;
    swap(A[i],A[i+1]);
    if(!M[M[M[M[M[0][A[0]]][A[1]]][A[2]]][A[3]]][A[4]])
      ++cnt;
    swap(A[i],A[i+1]);
  }
  if(cnt) return 1;
  else return 0;
}

int main(){
  for(int i = 0; i < 10; ++i)
    for(int j = 0; j < 10; ++j)
      cin >> M[i][j];
  int ans = 0;
  for(int i = 0; i < 10; ++i){
    for(int j = 0; j < 10; ++j){
      for(int k = 0; k < 10; ++k){
	for(int l = 0; l < 10; ++l){
	  int e = M[M[M[M[0][i]][j]][k]][l];
	  ans += count(i,j,k,l,e);
	}
      }
    }
  }
  cout << ans << endl;
  return 0;
}