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 <bits/stdc++.h>

using namespace std;

int main()
{
  int x[10][10];
  for(int i = 0; i < 10; i++) {
    for(int j = 0; j < 10; j++) cin >> x[i][j];
  }
  int ans = 0;
  for(int i = 0; i < 10000; i++) {
    char id[6];
    sprintf(id, "%04d0", i);
    int res = 0;
    for(int j = 0; j < 4; j++) res = x[res][id[j]-'0'];
    id[4] = res + '0';
    bool flag = false;
    for(int j = 0; j < 5; j++) {
      char c = id[j];
      for(int k = 0; k < 10; k++) {
	if(c-'0' == k) continue;
	id[j] = k+'0';
	int tmp = 0;
	for(int l = 0; l < 5; l++) tmp = x[tmp][id[l]-'0'];
	if(tmp == 0) flag = true;
      }
      id[j] = c;
    }
    char s[6]; strcpy(s, id);
    for(int j = 0; j < 4; j++) {
      swap(id[j], id[j+1]);
      if(strcmp(s, id) == 0) {
	swap(id[j], id[j+1]);
	continue;
      }
      int tmp = 0;
      for(int k = 0; k < 5; k++) tmp = x[tmp][id[k]-'0'];
      if(tmp == 0) flag = true;
      swap(id[j], id[j+1]);
    }
    if(flag) ans++;
  }
  cout << ans << endl;
  return 0;
}