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:
//Copyright(c)2017 Mstdream
#include<bits/stdc++.h>
using namespace std;

inline void splay(int &v){
	v=0;char c=0;int p=1;
	while(c<'0' || c>'9'){if(c=='-')p=-1;c=getchar();}
	while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
	v*=p;
}
int a[15][15],p[15];
int main(){
	for(int i=0;i<=9;i++){
		for(int j=0;j<=9;j++){
			splay(a[i][j]);
		}
	}
	int ans=0;
	for(int k=0;k<=9999;k++){
		int x=k;
		for(int i=4;i>=1;i--)p[i]=x%10,x/=10;
		x=0;
		for(int i=1;i<=4;i++)x=a[x][p[i]];
		p[5]=x;x=0;
		for(int i=1;i<=5;i++)x=a[x][p[i]];
		if(x==0){
			bool f=1;
			for(int i=1;i<=5;i++){
				for(int j=0;j<=9;j++){
					if(j==p[i])continue;
					swap(p[i],j);
					int o=0;
					for(int ff=1;ff<=5;ff++)o=a[o][p[ff]];
					if(o==0)f=0;
					swap(p[i],j);
				}
			}
			for(int i=1;i<=4;i++){
				if(p[i]==p[i+1])continue;
				swap(p[i],p[i+1]);
				int o=0;
				for(int ff=1;ff<=5;ff++)o=a[o][p[ff]];
				if(o==0)f=0;
				swap(p[i],p[i+1]);
			}
			if(!f){
				ans++;
				//cerr<<k<<endl;
				//return 0;
			}
		}
	}
	cout<<ans<<endl;
}