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 a[10][10], b[6];
int ans;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	for(int i=0; i<10; ++i)
		for(int j=0; j<10; ++j)
			cin >> a[i][j];
	++b[1];
	while(1){
//		cout << b[0] << " " << b[1] << " " << b[2] << " " << b[3] << "\n";
		int c = 0;
		for(int i=4; i>=1; --i)
			c = a[c][b[i]];
		b[0] = c;
//		cout << b[4] << " " << b[3] << " " << b[2] << " " << b[1] << " " << b[0] << "\n";
		bool f = false;
		for(int i=0; i<5; ++i)
			for(int j=1; j<10; ++j){
				b[i] += j;
				b[i] %= 10;
				int t = 0;
				for(int k=4; k>=0; --k)
					t = a[t][b[k]];
				if(t == 0)
					f = true;
				b[i] -= j - 10;
				b[i] %= 10;
			}
		for(int i=0; i<4; ++i){
			if(b[i] == b[i+1]) continue;
			swap(b[i], b[i+1]);
			int t = 0;
			for(int k=4; k>=0; --k)
				t = a[t][b[k]];
			if(t == 0)
				f = true;
			swap(b[i], b[i+1]);
		}
		if(f) ++ans;
		++b[1];
		for(int i=1; i<=4; ++i){
			if(b[i] == 10){
				b[i] = 0;
				++b[i+1];
			}
		}
		if(b[4]+b[3]+b[2] == 0 && b[1] == 1) break;
	}
	cout << ans << "\n";
}