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:
//?????????????????????s[0]s[1]?????¨?????????
#include <iostream>
#include <string>
using namespace std;

int x[10][10];

char check_digit(string base_id) {
	int num = 0;
	for (int i = 0; i < 4; i++) {
		num = x[num][base_id[i] - '0'];
	}
	return (char)(num + '0');
}

bool is_correct(string id) {
	int num = 0;
	for (int i = 0; i < 5; i++) num = x[num][id[i] - '0'];
	return (num == 0);
}

bool is_detect_error(string id) {
	string eid;
	
	for (int i = 0; i < 5; i++) {
		eid = id;
		for (int j = 0; j < 10; j++) {
			eid[i] = (j + '0');
			if (eid != id && is_correct(eid)) { return false; }
		}
	}
	
	for (int i = 0; i < 4; i++) {
		eid = id;
		swap(eid[i], eid[i + 1]);
		if (eid != id && is_correct(eid)) { return false; }
	}
	return true;
}

int dfs(string base_id) {
	if (base_id.length() == 4) {
		return !is_detect_error( base_id + check_digit(base_id) );
	}
	
	int ret = 0;
	for (int i = 0; i < 10; i++) {
		ret += dfs(base_id + (char)('0' + i));
	}
	return ret;
}

int main() {
	int i, j;
	for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) cin >> x[i][j];
	cout << dfs("") << endl;
	return 0;
}