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>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
#define EPS 1e-15
using namespace std;
ll tab[11][11];
ll ans = 0;

string tos(ll num){
  string ret = "";
  for(int i = 0;i < 4; i++){
    ret += (char)(num % 10 + '0');
    num /= 10;
  }
  reverse(ALL(ret));
  return ret;
}

ll toint(string s){
  ll kake = 1;
  ll ret = 0;
  for(int i = s.size() - 1;i >= 0; i--){
    ret += kake * (s[i] - '0');
  }
  return ret;
}

ll checkdigit(string s){
  ll now = 0;
  for(int i = 0;i < 5; i++){
    now = tab[now][s[i] - '0'];
  }
  return now == 0;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  for(int i = 0;i < 10; i++){
    for(int j = 0;j < 10; j++){
      cin >> tab[i][j];
    }
  }

  for(int i = 0;i < 10000; i++){
    string s = tos(i);
    bool fl = true;
    ll cd = 0;
    for(int j = 0;j < 4; j++){
      cd = tab[cd][s[j] - '0'];
    }
    s += (char)(cd + '0');
    for(int j = 0;j < 5; j++){
      string cs = s;
      for(int k = 0;k < 10; k++){
	if(s[j] == (k + '0'))continue;
	cs[j] = (char)(k + '0');
	if(checkdigit(cs)){
	  fl = false;
	}
      }
    }
    for(int j = 0;j < 4; j++){
      string cs = s;
      if(cs[j] == cs[j+1])continue;
      char tmp = cs[j];
      cs[j] = cs[j+1];
      cs[j+1] = tmp;
      if(checkdigit(cs)){
	fl = false;
      }
    }
    if(fl == false){
      ans++;
    }
  }
  cout << ans << endl;
  return 0;
}