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>
#define rep(i,n) for(int i = 0; i < n; i++)
#define MAX_N 40
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int ta[10][10];

int table(vector<int> v){
    int ret = ta[0][v[0]];
    ret = ta[ret][v[1]];
    ret = ta[ret][v[2]];
    ret = ta[ret][v[3]];
    if(v.size() > 4) ret = ta[ret][v[4]];
    return ret;
}

int check(string s){
    vector<int> num;
    rep(i,s.size()) num.push_back(s[i]-'0');
    num.push_back(table(num));
    rep(i,5){
        rep(j,10){
            if(j == num[i]) continue;
            vector<int> tmp = num;
            tmp[i] = j;
            if(table(tmp) == 0) return 1;
        }
    }
    rep(i,4){
        if(num[i] == num[i+1]) continue;
        vector<int> tmp = num;
        swap(tmp[i],tmp[i+1]);
        if(table(tmp) == 0) return 1;
    }
    return 0;
}

int main(){
    int ans = 0;
    rep(i,10) rep(j,10) cin >> ta[i][j];
    for(int i = 0; i <= 9999; i++){
        string s = to_string(i);
        while(s.size() < 4) s.insert(0,"0");
        ans += check(s);
    }
    cout << ans << endl;
}