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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int board[10][10];

int f(string &s) {
	ll res = 0;
	for (int i = 0; i < s.size(); i++) {
		res = board[res][s[i] - '0'];
	}
	return res;
}
bool check(string &s) {
	ll res = 0;
	return f(s) == 0;
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) scanf("%d", &board[i][j]);

	ll res = 0;
	for (int i = 0; i <= 9999; i++) {
		string s = to_string(i);
		ll tmp = f(s);
		s += (char)('0' + tmp);
		bool flag = false;
		while (s.size() < 5) s = '0' + s;
		for (int i = 0; i + 1 < s.size(); i++) {
			if (s[i] != s[i + 1]) {
				swap(s[i], s[i + 1]);
				if (f(s) == 0) flag = true;
				swap(s[i], s[i + 1]);
			}
		}
		for (int i = 0; i < s.size(); i++) {
			for (char j = '0'; j <= '9'; j++) {
				if (s[i] == j) continue;
				char c = s[i];
				s[i] = j;
				if (f(s) == 0) flag = true;
				s[i] = c;
			}
		}
		if (flag) res++;
	}
	cout << res << endl;
	return 0;
}