现在的位置: 首页 > 综合 > 正文

黑书上的一个小题

2018年04月05日 ⁄ 综合 ⁄ 共 2069字 ⁄ 字号 评论关闭
#include"iostream"

const int Max= 10;
const int Choice = 5;
char ans[Max];
int total[Choice] = {0};

void tra(){
	for(int i = 0 ; i < Max ; i ++){
		if(ans[i] == 'a')total[0]++;
		if(ans[i] == 'b')total[1]++;
		if(ans[i] == 'c')total[2]++;
		if(ans[i] == 'd')total[3]++;
		if(ans[i] == 'e')total[4]++;
	}
}

bool rule_a(){
	int i = 0;
	for( ; i < Max && ans[i] != 'b' ; i++);
	for(int j = 0 ; j < 5 ; j ++){
		if(i < Max && ans[0] == 'a' + j && i == j + 2 - 1){
			return true;
		}
	}
	return false;
}

bool rule_b(){
	for(int i = 1 ; i < 6 ; i++){
		if(ans[1] == ('a' - 1 + i) && ans[i] == ans[i + 1])
			return true;
	}
	return false;
}

bool rule_c(){
	if(ans[2] == 'a' && ans[0] == 'a')
		return true;
	if(ans[2] == 'b' && ans[1] == 'b')
		return true;
	if(ans[2] == 'c' && ans[3] == 'c')
		return true;
	if(ans[2] == 'd' && ans[6] == 'd')
		return true;
	if(ans[2] == 'e' && ans[5] == 'e')
		return true;
	return false;
}

bool rule_d(){
	if(ans[3] == 'a' && total[0] == 0)return true;
	if(ans[3] == 'b' && total[0] == 1)return true;
	if(ans[3] == 'c' && total[0] == 2)return true;
	if(ans[3] == 'd' && total[0] == 3)return true;
	if(ans[3] == 'e' && total[0] == 4)return true;
	return false;
}

bool rule_e(){
	for(int i = 0 ; i < 5 ; i ++){
		if(ans[4] == 'a' + i && ans[4] == ans[9 - i])
			return true;
	}
	return false;
}

bool rule_f(){
	bool flag = true;
	for(int i = 0 ; i < 4 ; i ++){
		if(total[0] == total[i + 1]){
			flag = false;
			if(ans[5] == 'a' + i)
				return true;
		}
	}
	if(ans[5] == 'e' && flag)return true;
	return false;
}

bool rule_g(){
	int e = abs((int)(ans[6] - ans[7]));
	for(int i = 0 ; i < 5 ; i++){
		if(ans[6] == 'a' + i && 4 - i == e)
			return true;
	}
	return false;
}

bool rule_h(){
	int s = total[0] + total[4];
	for(int i = 0 ; i < 5 ; i ++){
		if(ans[7] == 'a' + i && i + 2 == s)
			return true;
	}
	return false;
}

bool rule_i(){
	int s = total[1] + total[2] + total[3];
	if(ans[8] == 'a' && (s == 2 || s == 3 || s == 5 || s == 7))
		return true;
	if(ans[8] == 'b' && (s == 1 || s == 2 || s == 6))
		return true;
	if(ans[8] == 'c' && (s == 1 || s == 4 || s == 9))
		return true;
	if(ans[8] == 'd' && (s == 1 || s == 8))
		return true;
	if(ans[8] == 'e' && (s == 5 || s == 10))
		return true;
	return false;
}

bool check(){
	memset(total,0,sizeof(total));
	tra();
	if(rule_a() && rule_b() && rule_c() && rule_d() && rule_e() && rule_f() && rule_g() && rule_h() && rule_i()){
		return true;
	}
	return false;
}

bool dfs(int depth){
	if(depth == Max && check()){
		for(int i = 0; i < Max; i++)
			printf("%c ",ans[i]);
		putchar('\n');
		return true;
	}
	if(depth < Max){
		for(int i = 0 ; i < 5 ; i++){
			ans[depth] = 'a' + i;
			dfs(depth + 1);
			ans[depth] -= i;
		}
	}
	return false;
}

int main(void){
	dfs(0);
	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.