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 <vector>
using namespace std;

int t[10][10];
int power(int a, int n) { int ret = 1; for (int i = 0; i < n; i++) ret *= a; return ret; }

int getD(int id, int digit) {
	return (id / power(10, digit)) % 10;
}

int checkDigit(int id, int digitNum) {
	int row = 0;
	for (int i = digitNum - 1; i >= 0; i--) {
		int col = getD(id, i);
		row = t[row][col];
	}
	return row;
}

vector<int> getErrorId(int id) {
	int i, j;
	vector<int> ret;
	
	//alter
	for (i = 0; i < 5; i++) {
		for (j = 0; j < 10; j++) {
			int plus = (j - getD(id, i)) * power(10, i);
			if (plus == 0) continue;
			
			id += plus;
			ret.push_back(id);
			id -= plus;
		}
	}
	
	//swap
	for (i = 0; i < 4; i++) {
		int x = getD(id, i);
		int y = getD(id, i + 1);
		if (x == y) continue;
		
		int tmp = id;
		
		id -= x * power(10, i);
		id -= y * power(10, i + 1);
		id += x * power(10, i + 1);
		id += y * power(10, i);
		ret.push_back(id);
		
		id = tmp;
	}
	return ret;
}

int main() {
	int i, j;
	
	for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) cin >> t[i][j];
	
	int ans = 0;
	for (i = 0; i < 10000; i++) {
		int e = checkDigit(i, 4);
		int id = i * 10 + e;
		vector<int> errorId = getErrorId(id);
		for (j = 0; j < errorId.size(); j++) {
			if (checkDigit(errorId[j], 5) == 0) {
				ans++;
				break;
			}
		}
	}
	cout << ans << endl;
	return 0;
}