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>
#define REP(i,n) for (int i=0; i<n; i++)

using namespace std;

int table[11][11];
bool checked[100005];

int rete(int a, int b, int c, int d){
	int x;
	x = table[0][a];
	x = table[x][b];
	x = table[x][c];
	x = table[x][d];
	return x;
}

bool check(int a, int b, int c, int d, int e){
	// if (checked[a*10000+b*1000+c*100+d*10+e]) return false;
	// checked[a*10000+b*1000+c*100+d*10+e] = true;
	
	int x;
	x = table[0][a];
	x = table[x][b];
	x = table[x][c];
	x = table[x][d];
	x = table[x][e];
	return x==0;
}

int answer(int a, int b, int c, int d, int e){
	REP(i,10){
			if (i!=a && check(i, b, c, d, e)) return 1;
			if (i!=b && check(a, i, c, d, e)) return 1;
			if (i!=c && check(a, b, i, d, e)) return 1;
			if (i!=d && check(a, b, c, i, e)) return 1;	
			if (i!=e && check(a, b, c, d, i)) return 1;	
	}
	if (a!=b && check(b, a, c, d, e)) return 1;
	if (b!=c && check(a, c, b, d, e)) return 1;
	if (c!=d && check(a, b, d, c, e)) return 1;
	if (d!=e && check(a, b, c, e, d)) return 1;
	return 0;
}

int main(){
	int ans=0;
	REP(i,100000){
		checked[i] = false;
	}
	REP(j, 10){
		REP(k, 10){
			table[j][k] = 0;
		}
	}
	
	REP(j,10){
		REP(k,10){
			scanf("%d", &table[j][k]);
		}
	}
	
	REP(a,10){
		REP(b,10){
			REP(c,10){
				REP(d,10){
				
					int e = rete(a, b, c, d);
					check(a, b, c, d, e);
					//if (a==2) cerr << a << b << c << d << e << endl;;
					ans += answer(a, b, c, d, e);
				}
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}