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;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repp(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perr(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)

typedef long long LL;
typedef long double LD;

vector<vector<int>> v;

int sol(vector<int> &s){
  int x = 0;
  rep(i,4){
    x=v[x][s[i]];
  }
  return x;
}

bool isValid(vector<int> &s){
  int x = 0;
  rep(i,5){
    x = v[x][s[i]];
  }
  if(x) return false;
  else return true;
}

int main(){
  int n = 10;
  v=vector<vector<int>>(n,vector<int>(n));
  rep(i,n) rep(j,n) cin >> v[i][j];
  int ans=0;
  rep(i,10000){
    int x = i;
    vector<int> s(5);
    per(j,4){
      s[j]=x%10;
      x/=10;
    }
    s[4]=sol(s);
    bool ok = true;
    vector<int> tmp;
    rep(j,5){
      rep(k,10){
        if(k==s[j]) continue;
        tmp=s;
        tmp[j]=k;
        if(isValid(tmp)) ok=false;
      }
    }
    rep(j,4){
      if(s[j]==s[j+1]) continue;
      tmp=s;
      swap(tmp[j],tmp[j+1]);
      if(isValid(tmp)) ok=false;
    }
    if(!ok) ans++;
  }
  cout << ans << endl;
  return 0;
}