Deep Search
# Include <iostream> # include <cstdio> # include <cstring> using namespace STD; int num [10], a [10], vis [10], ANS [10]; int K, flag; void DFS (int x) {int I, j; If (x = k) {flag ++; for (I = 0; I <8; I ++) ans [I] = num [I]; return;} If (flag> 1) return; For (j = 1; j <= 8; j ++) {If (vis [J]) continue; if (a [x] = 0) {If (ABS (J-num [1]) = 1 | ABS (J-num [3]) = 1 | ABS (J-num [2]) = 1) continue ;} if (A [x] = 1) {If (ABS (J-num [0]) = 1 | ABS (J-num [2]) = 1 | ABS (J-num [4]) = 1 | ABS (J-num [5]) = 1) continue ;} if (A [x] = 2) {If (ABS (J-num [0]) = 1 | ABS (J-num [1]) = 1 | ABS (J-num [3]) = 1 | ABS (J-num [4]) = 1 | ABS (J-num [5]) = 1 | ABS (J-num [6]) = 1) continue ;} if (A [x] = 3) {If (ABS (J-num [0]) = 1 | ABS (J-num [2]) = 1 | ABS (J-num [5]) = 1 | ABS (J-num [6]) = 1) continue ;} if (A [x] = 4) {If (ABS (J-num [1]) = 1 | ABS (J-num [2]) = 1 | ABS (J-num [5]) = 1 | ABS (J-num [7]) = 1) continue ;} if (A [x] = 5) {If (ABS (J-num [2]) = 1 | ABS (J-num [1]) = 1 | ABS (J-num [4]) = 1 | ABS (J-num [7]) = 1 | ABS (J-num [6]) = 1 | ABS (J-num [3]) = 1) continue ;} if (A [x] = 6) {If (ABS (J-num [3]) = 1 | ABS (J-num [2]) = 1 | ABS (J-num [5]) = 1 | ABS (J-num [7]) = 1) continue ;} if (A [x] = 7) {If (ABS (J-num [4]) = 1 | ABS (J-num [5]) = 1 | ABS (J-num [6]) = 1) continue;} num [A [x] = J; vis [J] = 1; DFS (x + 1); num [A [x] =-1; vis [J] = 0 ;}} int main () {int I; int T; int COUNT = 1; CIN> T; while (t --) {k = 0; memset (VIS, 0, sizeof (VIS); for (I = 0; I <8; I ++) {CIN> num [I]; If (Num [I] = 0) {A [k ++] = I; num [I] =-1; // note here} elsevis [num [I] = 1;} printf ("case % d:", Count ++ ); flag = 0; DFS (0); If (flag = 1) {printf ("% d", ANS [0]); for (I = 1; I <8; I ++) printf ("% d", ANS [I]); cout <Endl;} else {If (flag = 0) cout <"No answer" <Endl; elsecout <"not unique" <Endl ;}} return 0 ;}