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:
///
// File:  1368.cpp
// Author: ymiyamoto
//
// Created on Thu Nov 30 21:58:14 2017
//

#include <array>
#include <cstdint>
#include <iostream>

using namespace std;

uint32_t calc_e(array<uint32_t, 5> code, array<array<uint32_t, 10>, 10> matrix)
{
  uint32_t num = 0;
  for (uint32_t i = 0; i < code.size() - 1; i++) {
    num = matrix[num][code[i]];
  }
  return num;
}

bool check(array<uint32_t, 5> code, array<array<uint32_t, 10>, 10> matrix)
{
  uint32_t num = 0;
  for (uint32_t i = 0; i < code.size(); i++) {
    num = matrix[num][code[i]];
  }

  return num == 0;
}

int32_t main()
{
  array<array<uint32_t, 10>, 10> matrix;
  for (uint32_t i = 0; i < 10; i++) {
    for (uint32_t j = 0; j < 10; j++) {
      cin >> matrix[i][j];
    }
  }

  uint32_t count = 0;
  for (uint32_t i = 0; i < 10000; i++) {
    bool invalid = false;
    array<uint32_t, 5> code;
    code[0] = i / 1000;
    code[1] = (i - code[0] * 1000) / 100;
    code[2] = (i - code[0] * 1000 - code[1] * 100) / 10;
    code[3] = (i - code[0] * 1000 - code[1] * 100) % 10;
    code[4] = calc_e(code, matrix);

    for (uint32_t j = 0; j < 5; j++) {
      for (uint32_t k = 0; k < 10; k++) {
        array<uint32_t, 5> err(code);
        err[j] = k;
        if (err[j] != code[j]) {
          if (check(err, matrix)) {
            invalid = true;
          }
        }
      }
    }

    for (uint32_t j = 0; j < 5 - 1; j++) {
      array<uint32_t, 5> err(code);
      swap(err[j], err[j + 1]);
      if (err[j] != code[j] || err[j + 1] != code[j + 1]) {
        if (check(err, matrix)) {
          invalid = true;
        }
      }
    }

    if (invalid) {
      count++;
    }
  }

  cout << count << endl;

  return 0;
}