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 can[10][10][10][10][10];
	for (int a = 0; a < 10; a++) {
		for (int b = 0; b < 10; b++) {
			for (int c = 0; c < 10; c++) {
				for (int d = 0; d < 10; d++) {
					for (int e = 0; e < 10; e++) {
						can[a][b][c][d][e] = x[x[x[x[x[0][a]][b]][c]][d]][e] == 0;
					}
				}
			}
		}
	}
	int res = 0;
	int t[5];
	for (t[0] = 0; t[0] < 10; t[0]++) {
		for (t[1] = 0; t[1] < 10; t[1]++) {
			for (t[2] = 0; t[2] < 10; t[2]++) {
				for (t[3] = 0; t[3] < 10; t[3]++) {
					bool flag = false;
					int cnt = 0;
					for (t[4] = 0; t[4] < 10; t[4]++) {
						if (can[t[0]][t[1]][t[2]][t[3]][t[4]]) {
							cnt++;
							for (int i = 0; i < 5; i++) {
								for (int j = 0; j < 10; j++) {
									if (t[i] != j) {
										int u[] = { t[0], t[1], t[2], t[3], t[4] };
										u[i] = j;
										if (can[u[0]][u[1]][u[2]][u[3]][u[4]]) {
											flag = true;
										}
									}
								}
							}
							for (int i = 1; i < 5; i++) {
								if (t[i - 1] == t[i]) continue;
								swap(t[i - 1], t[i]);
								if (can[t[0]][t[1]][t[2]][t[3]][t[4]]) {
									flag = true;
								}
								swap(t[i - 1], t[i]);
							}
						}
					}
					if (flag || cnt != 1) {
						res++;
					}
				}
			}
		}
	}
	cout << res << endl;
	return 0;
}