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>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<cmath>
#include<ctime>
#include<locale>
using namespace std;
long long  t[10][10];
long long  f(long long  a[4])
{
    long long  x=0;
    for(long long  i=0;i<4;i++)x=t[x][a[i]];
    return x;
}
int main()
{

{
    for(long long  i=0;i<10;i++)
        for(long long  j=0;j<10;j++)
            scanf("%ld",&t[i][j]);
    long long s=0;
    for(long long  i=0;i<10000;i++)
    {
        long long  a[4],b[4],k=0;
        a[3]=i%10;
        a[2]=i/10%10;
        a[1]=i/100%10;
        a[0]=i/1000%10;
        long long  x0=f(a);
        for(long long  j=0;j<4&&k==0;j++)
        {
            for(long long  i=0;i<4;i++)b[i]=a[i];
            for(long long  x=0;x<10&&k==0;x++)
            {
                if(a[j]==x)continue;
                b[j]=x;
                if(t[f(b)][x0]==0){s++;k++;}
            }
        }
        for(long j=0;j<10&&k==0;j++)
        {
            if(j==x0)continue;
            if(t[x0][j]==0){s++;k++;}
        }
        for(long long  j=1;j<4&&k==0;j++)
        {
            for(long long  x=0;x<4;x++)b[x]=a[x];
            if(a[j]==a[j-1])continue;
            {long long  x=b[j];b[j]=b[j-1];b[j-1]=x;}
            if(t[f(b)][x0]==0){s++;k++;}
        }
        if(x0!=a[3]&&k==0)
        {
            for(long long  x=0;x<4;x++)b[x]=a[x];
            b[3]=x0;
            if(t[f(b)][a[3]]==0){s++;k++;}
        }
        //cout<<i<<' '<<s<<endl;
    }
    cout<<s<<endl;
}
    return 0;
}