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

int calc(vector<int> S){
  int r=0;
  int c;
  for(int i=0;i<4;i++){
    c=S[i];
    r=m[r][c];
  }
  return r; 
}

int main(){
  for (int i=0;i<100;i++){
    cin >> m[i/10][i%10];
  }
 int sample=0;
 int cnt = 0;
  for (int i=0;i<10000;i++){
    sample=i;
    vector<int> S(5);
    S[0]=sample/1000;
    S[1]=(sample-1000*S[0])/100;
    S[2]=(sample-1000*S[0]-100*S[1])/10;
    S[3]=(sample-1000*S[0]-100*S[1]-10*S[2]);
    S[4]=calc(S);
    bool isOk = true;
    for(int j=0;j<5;j++){
      for(int k=0;k<10;k++){
	if(S[j]==k){
	  continue;
	}
	vector<int> tmp=S;
	tmp[j]=k;
	int tmp1=calc(tmp);
	if(m[tmp1][tmp[4]]==0){
	  cnt++;
	  isOk = false;
	  break;
	}
      }

      if(!isOk){
	break;
      }
    }

    if(!isOk){
      continue;
    }
    for(int j=0;j<4;j++){
      if(S[j] == S[j + 1]){
	continue;
      }
      
      vector<int> tmp=S;
      swap(tmp[j],tmp[j+1]);
      int tmp1=calc(tmp);
      if(m[tmp1][tmp[4]]==0){
	cnt++;
	isOk = false;
	break;
      }
    }
  }
  cout<<cnt<<endl;
  
  return 0;
}