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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <set>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define PRIME1 99999883
#define PRIME2 99999893
#define EPS 0.00000001
using namespace std;

int main(){

	int table[10][10],num[5],work[5],tmp,check,left,count = 0,tmp_count;

	for(int i = 0; i <= 9; i++){
		for(int k = 0; k <= 9; k++)scanf("%d",&table[i][k]);
	}

	for(int a = 0; a <= 9; a++){
		for(int b = 0; b <= 9; b++){
			for(int c = 0; c <= 9; c++){
				for(int d = 0; d <= 9; d++){
					num[0] = a,num[1] = b,num[2] = c,num[3] = d;

					left = 0;
					for(int i = 0; i <= 3; i++){
						tmp = table[left][num[i]];
						left = tmp;
					}
					check = left;

					num[4] = check;

					tmp_count = 0;

					for(int not_a = 0; not_a <= 9; not_a++){
						if(a == not_a)continue;

						work[0] = not_a;
						work[1] = num[1];
						work[2] = num[2];
						work[3] = num[3];
						work[4] = num[4];

						left = 0;
						for(int i = 0; i <= 4; i++){
							tmp = table[left][work[i]];
							left = tmp;
						}
						if(left == 0){

							tmp_count++;
							break;
						}
					}

					if(tmp_count == 0){

						for(int not_b = 0; not_b <= 9; not_b++){
							if(b == not_b)continue;

							work[0] = num[0];
							work[1] = not_b;
							work[2] = num[2];
							work[3] = num[3];
							work[4] = num[4];

							left = 0;
							for(int i = 0; i <= 4; i++){
								tmp = table[left][work[i]];
								left = tmp;
							}
							if(left == 0){
								tmp_count++;
								break;
							}
						}
					}


					if(tmp_count == 0){

						for(int not_c = 0; not_c <= 9; not_c++){
							if(c == not_c)continue;

							work[0] = num[0];
							work[1] = num[1];
							work[2] = not_c;
							work[3] = num[3];
							work[4] = num[4];


							left = 0;
							for(int i = 0; i <= 4; i++){
								tmp = table[left][work[i]];
								left = tmp;
							}
							if(left == 0){

								tmp_count++;
								break;
							}
						}
					}

					if(tmp_count == 0){

						for(int not_d = 0; not_d <= 9; not_d++){
							if(d == not_d)continue;

							work[0] = num[0];
							work[1] = num[1];
							work[2] = num[2];
							work[3] = not_d;
							work[4] = num[4];

							left = 0;
							for(int i = 0; i <= 4; i++){
								tmp = table[left][work[i]];
								left = tmp;
							}
							if(left == 0){
								tmp_count++;
								break;
							}
						}
					}

					if(tmp_count == 0){

						for(int not_e = 0; not_e <= 9; not_e++){
							if(not_e == num[4])continue;

							work[0] = num[0];
							work[1] = num[1];
							work[2] = num[2];
							work[3] = num[3];
							work[4] = not_e;

							left = 0;
							for(int i = 0; i <= 4; i++){
								tmp = table[left][work[i]];
								left = tmp;
							}
							if(left == 0){
								tmp_count++;
								break;
							}
						}
					}

					if(tmp_count == 0){
						for(int i = 0; i <= 3; i++){
							if(num[i] == num[i+1])continue;

							for(int k = 0; k <= 4; k++)work[k] = num[k];
							swap(work[i],work[i+1]);

							left = 0;
							for(int k = 0; k <= 4; k++){
								tmp = table[left][work[k]];
								left = tmp;
							}
							if(left == 0){
								tmp_count++;
								break;
							}
						}
					}

					if(tmp_count > 0)count++;
				}
			}
		}
	}


	printf("%d\n",count);

    return 0;
}