根据题目描述,可以知道 1 和 8 只能放在 C 和 F 两个位置,固定好 1 和 8 后,2 和 7 也就固定了,剩下的就是枚举了;
View Code
# include <cstdio> const int tab[][8] = { {7, 3, 1, 4, 5, 8, 6, 2}, {7, 4, 1, 3, 6, 8, 5, 2}, {2, 5, 8, 6, 3, 1, 4, 7}, {2, 6, 8, 5, 4, 1, 3, 7}}; int a[9]; void solve(void) { int ans, cnt, i, j; for (i = 0; i < 8; ++i) scanf("%d", &a[i]); ans = -1, cnt = 0; for (j = 0; j < 4; ++j) { for (i = 0; i < 8; ++i) if (a[i] && a[i]!=tab[j][i]) break; if(i == 8) {++cnt;ans = j;} } if (cnt == 0) puts(" No answer"); else if (cnt > 1) puts(" Not unique"); else { for (int i = 0; i < 8; ++i) printf(" %d", tab[ans][i]); putchar('\n'); } } int main() { int T; scanf("%d", &T); for (int i = 1; i <= T; ++i) { printf("Case %d:", i); solve(); } return 0; }
/**/