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:
/*  -*- coding: utf-8 -*-
 *
 * 1368.cc: Quality of Check Digits
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int D = 10;
const int K = 4;

/* typedef */

/* global variables */

int xs[D][D], ps[K + 1];

/* subroutines */

int check(int n, int ps[]) {
  int c = 0;
  for (int i = 0; i < n; i++) c = xs[c][ps[i]];
  return c;
}

bool next(int ps[]) {
  int k = K - 1;
  while (k >= 0) {
    if (++ps[k] < D) break;
    ps[k--] = 0;
  }
  return (k >= 0);
}

void printps(int n, int ps[]) {
  for (int i = 0; i < n; i++) putchar('0' + ps[i]);
  putchar('\n');
}

/* main */

int main() {
  for (int i = 0; i < D; i++)
    for (int j = 0; j < D; j++) scanf("%d", &xs[i][j]);

  int cnt = 0;
  do {
    ps[K] = check(K, ps);
    //printps(K + 1, ps);

    bool ok = true;
    for (int k = 0; ok && k <= K; k++) {
      int pk = ps[k];
      for (int d = 0; ok && d < D; d++)
	if (pk != d) {
	  ps[k] = d;
	  ok = (check(K + 1, ps) != 0);
	  ps[k] = pk;
	}
    }

    for (int k = 0; ok && k < K; k++)
      if (ps[k] != ps[k + 1]) {
	swap(ps[k], ps[k + 1]);
	ok = (check(K + 1, ps) != 0);
	swap(ps[k], ps[k + 1]);
      }

    if (! ok) cnt++;
  } while (next(ps));

  printf("%d\n", cnt);
  return 0;
}