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>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <bitset>
#include <string>
#include <time.h>
using namespace std;
long double esp=1e-11;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define cle(a) while(!a.empty())a.pop()
#define mem(p,c) memset(p,c,sizeof(p))
#define mp(A, B) make_pair(A, B)
#define pb push_back
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
typedef long long int LL;
const long double PI = acos((long double)-1);
const LL INF=0x3f3f3f3fll;
const int MOD =1000000007ll;
const int maxn=500;
int f[10][10];
int d[5],g[5];
int cal()
{
    int t=0;
    for(int x=0;x<5;x++)
        t=f[t][g[x]];
    return t;
}
int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("in1.txt", "w", stdout);
    //::iterator iter;                  %I64d
    //for(int x=1;x<=n;x++)
    //for(int y=1;y<=n;y++)
    //scanf("%d",&a);
    //printf("%d\n",ans);
    for(int x=0;x<10;x++)
        for(int y=0;y<10;y++)
            scanf("%d",&f[x][y]);
    int ans=0;
    for(int x=0;x<10000;x++)
    {
        int t=x;
        for(int z=0;z<4;z++)
        {
            d[3-z]=t%10;
            t/=10;
        }
        t=0;
        for(int y=0;y<4;y++)
            t=f[t][d[y]];
        d[4]=t;
        int flag=0;
        for(int y=0;y<=3;y++)
        if(d[y]!=d[y+1])
        {
            for(int z=0;z<5;z++)g[z]=d[z];
            swap(g[y],g[y+1]);
            if(cal()==0)flag=1;
        }
        for(int y=0;y<=4;y++)
        for(int k=0;k<10;k++)
        if(k!=d[y])
        {
            for(int z=0;z<5;z++)g[z]=d[z];
            g[y]=k;
            if(cal()==0)flag=1;
        }
        ans+=flag;
    }
    printf("%d\n",ans);
    return 0;
}