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

int X(int a,int b){return tb[a][b];}

int check(vector<int>a){
  int res=0;
  for(int i=0;i<a.size();i++)res=X(res,a[i]);
  return res;
}

int main(){
  for(int i=0;i<10;i++)
    for(int j=0;j<10;j++)cin>>tb[i][j];

  int ans=0;
  for(int i=0;i<10000;i++){
    int n=i;
    vector<int> a;
    while(n)a.push_back(n%10),n/=10;
    while(a.size()<4)a.push_back(0); 
    reverse(a.begin(),a.end());
    int res=check(a);
    a.push_back(res);
    set <vector<int> > used;
    
    int flg=0;
    for(int i=0;i<5;i++)
      for(int j=0;j<10;j++){
	int tmp=a[i];
	if(a[i]==j)continue;
	a[i]=j;
	flg|=!check(a);
	a[i]=tmp;
      }

    for(int j=0;j<a.size()-1;j++){
      swap(a[j],a[j+1]);      
      if(a[j]==a[j+1])continue;
      if(used.count(a))continue;
      used.insert(a);
      if(!check(a))flg=1;
      swap(a[j],a[j+1]);
    }
    ans+=flg;
  }
  cout <<ans<<endl;
  return 0;
}