int audienceGuess[101][5];//观众猜测 bool guessTrue[11];//猜测成立 int n, m; int ansSchemes;//结果数量 10! = 3628800
struct { int rank; int failNum; int guess[101]; }athletes[11]; //rank[]为每个方案运动员的名次,sum保存每个方案运动员名次的十进制整数 structGuess { int rank[11]; } ans[368800];
boolcheckRank(int index, int rank){ int i; for (i = 0; i < athletes[index].failNum; i++) { if (athletes[index].guess[i] == rank) { returnfalse; } } returntrue; }
voidfix(int index){//回溯法把剩余没有名次的运动员根据约束条件补齐 if (index > n) { //保存其中一种结果 for (int i = 1; i <= n; i++) { ans[ansSchemes].rank[i] = athletes[i].rank; } ansSchemes++; } else {
//当前运动员没有名次 if (athletes[index].rank == 0) { for (int i = 1; i <= n; i++) { if (!guessTrue[i] && checkRank(index, i)) {//名次i未被标记且第index个运动员的名次i不在假名次中 athletes[index].rank = i; guessTrue[i] = true; fix(index + 1); athletes[index].rank = 0; guessTrue[i] = false; } } } else { fix(index + 1); } } }
//嗨嗨嗨,回溯 voidassump(int index){ if (index < m) { //index号预测情况模拟 //真 假 i = 0 //假 真 i = 1 for (int i = 0; i < 2; i++) { //两位观众预测相同 int gsI1 = 1 + 2 * i, gsI2 = 2 + 2 * i; int gsI3 = 3 - 2 * i, gsI4 = 4 - 2 * i; if (athletes[audienceGuess[index][gsI1]].rank == audienceGuess[index][gsI2]) { if (checkRank(audienceGuess[index][gsI3], audienceGuess[index][gsI4])) { athletes[audienceGuess[index][gsI3]]. guess[athletes[audienceGuess[index][gsI3]].failNum] = audienceGuess[index][gsI4]; athletes[audienceGuess[index][gsI3]].failNum++;