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 <vector>
#include <algorithm>
using namespace std;

int G[11][11];
int main() {
  for (int i = 0; i < 10; ++i) for (int j = 0; j < 10; ++j) cin >> G[i][j];
  int res = 0;
  for (int num = 0; num < 10000; ++num) {
	vector<int> vec;
	int tnum = num;
	while (tnum > 0) vec.push_back(tnum%10), tnum /= 10;
	while (vec.size() < 4) vec.push_back(0);
	reverse(vec.begin(), vec.end());
	int e = G[G[G[G[0][vec[0]]][vec[1]]][vec[2]]][vec[3]];
	vec.push_back(e);

	//cout << num << ": ";

	bool ok = true;
	for (int it = 0; it < 5; ++it) {
	  for (int a = 0; a < 10; ++a) {
		if (vec[it] == a) continue;
		vector<int> nvec = vec;
		nvec[it] = a;
		int check = G[G[G[G[G[0][nvec[0]]][nvec[1]]][nvec[2]]][nvec[3]]][nvec[4]];
		if (check == 0) ok = false;
	  }
	}
	for (int it = 0; it < 4; ++it) {
	  vector<int> nvec = vec;
	  swap(nvec[it], nvec[it+1]);
	  if (nvec == vec) continue;
	  int check = G[G[G[G[G[0][nvec[0]]][nvec[1]]][nvec[2]]][nvec[3]]][nvec[4]];
	  if (check == 0) {
		ok = false;
	  }
	}
	
	if (!ok) ++res;
  }
  cout << res << endl;
}