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 <iostream>
#include <cstring>
#include <set>
using namespace std;

int op[10][10];
int cache[100000];

int calc(int i) {
  int res = op[0][(i / 10000) % 10];
  res = op[res][(i / 1000) % 10];
  res = op[res][(i / 100) % 10];
  res = op[res][(i / 10) % 10];
  res = op[res][i % 10];
  return res;
}

int construct(int a, int b, int c, int d, int e) {
  return a * 10000 + b * 1000 + c * 100 + d * 10 + e;
}

bool check(int x) {
  int a = (x / 10000) % 10;
  int b = (x / 1000) % 10;
  int c = (x / 100) % 10;
  int d = (x / 10) % 10;
  int e = x % 10;

  // check swaps
  if (construct(b, a, c, d, e) != x &&
      calc(construct(b, a, c, d, e)) == 0) return false;
  if (construct(a, c, b, d, e) != x &&
      calc(construct(a, c, b, d, e)) == 0) return false;
  if (construct(a, b, d, c, e) != x &&
      calc(construct(a, b, d, c, e)) == 0) return false;
  if (construct(a, b, c, e, d) != x &&
      calc(construct(a, b, c, e, d)) == 0) return false;

  for (int i = 0; i < 10; ++i) {
    if (construct(i, b, c, d, e) != x &&
        calc(construct(i, b, c, d, e)) == 0) return false;
    if (construct(a, i, c, d, e) != x &&
        calc(construct(a, i, c, d, e)) == 0) return false;
    if (construct(a, b, i, d, e) != x &&
        calc(construct(a, b, i, d, e)) == 0) return false;
    if (construct(a, b, c, i, e) != x &&
        calc(construct(a, b, c, i, e)) == 0) return false;
    if (construct(a, b, c, d, i) != x &&
        calc(construct(a, b, c, d, i)) == 0) return false;
  }

  return true;
}

int main() {
  memset(op, 0, sizeof(op));

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

  for (int i = 0; i < 100000; ++i) {
    cache[i] = calc(i);
  }

  std::set<int> no;
  int ans = 0;
  for (int i = 0; i < 100000; ++i) {
    if (cache[i] == 0 && !check(i)) {
      no.insert(i/10);
    }
  }
  cout << no.size() << endl;
}