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, s, e) for(int i = (int)(s); i < (int)(e); ++i)
#define rep(i, n) REP(i, 0, n)
#define repr(i, n) for(int i = (n-1); i >= 0; --i)

int x[10][10];

using arr4 = array<int, 4>;
using arr5 = array<int, 5>;

arr4 conv(int n) {
  arr4 ret;
  repr(i, 4) {
    ret[i] = n%10;
    n /= 10;
  }
  return ret;
}

arr5 calc1(const arr4& a) {
  arr5 ret;
  rep(i, 4) ret[i] = a[i];
  int tmp = 0;
  rep(i, 4) {
    tmp = x[tmp][a[i]];
  }
  ret[4] = tmp;
  return ret;
}

using v5 = vector<arr5>;

v5 calc2(arr5& a) {
  v5 ret;
  rep(i, 5) {
    rep(j, 10) {
      if(j != a[i]) {
	swap(j, a[i]);
	ret.push_back(a);
	swap(j, a[i]);
      }
    }
  }
  return ret;
}
v5 calc3(arr5& a) {
  v5 ret;
  rep(i, 4) {
    if(a[i] == a[i+1]) continue;
    swap(a[i], a[i+1]);
    ret.push_back(a);
    swap(a[i], a[i+1]);
  }
  return ret;
}

int calc4(const arr5& a) {
  int ret = 0;
  rep(i, 5) ret = x[ret][a[i]];
  return ret;
}

int main() {
  rep(i, 10) rep(j, 10) cin >> x[i][j];
  int ans = 0;
  rep(i, 1e4) {
    auto a = conv(i);
    auto b = calc1(a);
    auto u = calc2(b);
    auto v = calc3(b);
    bool f = false;
    for(auto&& x : u) f |= calc4(x) == 0;
    for(auto&& x : v) f |= calc4(x) == 0;
    if(f) ++ans;
  }
  cout << ans << endl;
  return 0;
}