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 <algorithm>
#include <iostream>
#include <vector>

std::vector<int> to_vec(int n) {
  std::vector<int> v;
  for (int i = 0; i < 5; ++i) {
    v.push_back(n % 10);
    n /= 10;
  }
  std::reverse(v.begin(), v.end());
  return v;
}

int checkdigit(int table[10][10], const std::vector<int> &v) {
  int a = 0;
  for (int i = 0; i < 4; ++i) {
    a = table[a][v[i]];
  }
  return a;
}

bool is_valid(int table[10][10], const std::vector<int> &v) {
  int a = 0;
  for (int i = 0; i < 5; ++i) {
    a = table[a][v[i]];
  }
  return a == 0;
}

int main() {
  int table[10][10];
  int num = 0;
  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      std::cin >> table[i][j];
    }
  }
  for (int n = 0; n < 10000; ++n) {
    std::vector<int> v = to_vec(10 * n);
    v.back() = checkdigit(table, v);
    for (int i = 0; i < 5; ++i) {
      int x = v[i];
      for (int d = 0; d < 10; ++d) {
        if (d == x) continue;
        v[i] = d;
        if (is_valid(table, v)) goto fail;
      }
      v[i] = x;
    }
    for (int i = 0; i < 4; ++i) {
      if (v[i] == v[i + 1]) continue;
      std::swap(v[i], v[i + 1]);
      if (is_valid(table, v)) goto  fail;
      std::swap(v[i], v[i + 1]);
    }
    continue;
fail:
    ++num;
  }
  std::cout << num << std::endl;
  return 0;
}