Hdu2514 another eight puzzle

Source: Internet
Author: User
Another eight puzzle

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 716 accepted submission (s): 442

Problem descriptionfill the following 8 circles with digits 1 ~ 8, with each number exactly once. conntcted circles cannot be filled with two consecutive numbers.
There are 17 pairs of connected cicles:
A-B, A-C, A-D
B-C, B-E, B-F
C-D, C-E, C-F, C-G
D-F, D-G
E-F, E-H
F-G, F-H
G-H


Filling g with 1 and D with 2 (or G with 2 and D with 1) is illegal since g and D are connected and 1 and 2 are consecutive. however, filling a with 8 and B with 1 is legal since 8 and 1 are not consecutive.

In this problems, some circles are already filled, your tast is to fill the remaining circles to obtain a solution (if possivle ).

Inputthe first line contains a single integer T (1 ≤ T ≤ 10), the number of test cases. Each test case is a single line containing 8 integers 0 ~ 8, the numbers in Circle ~ H.0 indicates an empty circle.

 

Outputfor each test case, print the case number and the solution in the same format as the input. if there is no solution, print "no answer ". if there more than one solution, print "not unique ".

Sample Input

37 3 1 4 5 8 0 07 0 0 0 0 0 0 01 0 0 0 0 0 0 0
 

Sample output

Case 1: 7 3 1 4 5 8 6 2Case 2: Not uniqueCase 3: No answer
 

Sourceecjtu2008 autumn contest

Recommendlcy

The first self-developed DFS question must be remembered. After a full afternoon, I came out with a sense of accomplishment. First, paste an AC code. Some comments in the middle are useful for your own understanding. You can imitate them. The first write is poor. Don't be surprised!

Problem-solving ideas: This questions with DFS answers, is a classic DFS questions, its prototype visible http://acm.hdu.edu.cn/showproblem.php? PID = 1016, which is slightly adapted from the original question.
Problem-solving data structure:

Haha! For others, see comments and wish you a pleasant question!

First paste a concise code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int map [9] [9]; int X [9], Y [9]; int sum; int temp [9]; void DFS (INT t) {int I, j; If (t = 9) // if the final result meets the requirements, record result {sum ++; If (sum = 1) for (I = 0; I <= 8; I ++) temp [I] = x [I];} else if (X [T]) // This position has been assigned, skipping DFS (t + 1 ); else // assign I to X [T]-'a + T-1 '{for (I = 1; I <= 8; I ++) // search from start to end each time to check whether the {If (! Y [I]) // I is not used {for (j = 1; j <= 8; j ++) // If (Map [T] [J] | map [J] [T]) & X [J] & ABS (I-X [J]) = 1) {break;} If (j = 9) // no conflict {Y [I] = 1; // used, Mark X [T] = I; DFS (t + 1); y [I] = 0; // cancel the recursive sign x [T] = 0 ;}}}} int main () {int case1 = 1; memset (MAP, 0, sizeof (MAP); map [1] [2] = map [1] [3] = map [1] [4] = 1; map [2] [3] = map [2] [5] = map [2] [6] = 1; map [3] [4] = map [3] [5] = map [3] [6] = map [3] [7] = 1; map [4] [6] = map [4] [7] = 1; Map [5] [6] = map [5] [8] = 1; map [6] [7] = map [6] [8] = 1; Map [7] [8] = 1; int N, I; scanf ("% d ", & N); While (n --) {memset (Y, 0, sizeof (y); for (I = 1; I <= 8; I ++) {scanf ("% d", & X [I]), Y [x [I] = 1;} printf ("case % d:", case1 ++ ); sum = 0; DFS (1); If (sum = 1) {printf ("% d", temp [1]); for (I = 2; I <= 8; I ++) printf ("% d", temp [I]);} else if (sum = 0) printf ("No answer "); else printf ("not unique"); printf ("\ n") ;}return 0 ;}

This is the original code, original flavor, and a celebration of youth

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int map [9] [9]; int X [9], Y [9]; int sum; int temp [9]; void DFS (INT t) {// printf ("\ n \ t \ TT = % d -------", t ); int I, j; If (t = 9) // if the final result meets the requirements, record the result {// printf ("if the final result meets the requirements, record Result \ n "); sum ++; If (sum = 1) for (I = 0; I <= 8; I ++) temp [I] = x [I]; // printf ("% d", X [1]); // output test // for (I = 2; I <= 8; I ++) // printf ("% d", X [I]); // printf ("\ n ");} else if (X [T]) // This Position assigned, skip DFS (t + 1); // printf ("// This position has been assigned, skip \ n "), else // assign I to X [T]-'a + T-1 '{// printf ("// assign I to X [T]-'a + T-1' t = % d \ n ", i, T); for (I = 1; I <= 8; I ++) // searches from start to end each time, determine whether the required {// printf ("--------- I = % d \ n", I); If (! Y [I]) // I not used {// printf ("// I not used \ n"); For (j = 1; j <= 8; j ++) // conflict judgment if (Map [T] [J] | map [J] [T]) & X [J] & ABS (I-X [J]) = 1) {// printf ("conflict with X [J] = % d \ n ", X [J]); break;} If (j = 9) // no conflict {// printf ("// no conflict \ n "); Y [I] = 1; // used, Mark X [T] = I; DFS (t + 1); y [I] = 0; // when return, remove the recursive sign x [T] = 0 ;}}}} int main () {int case1 = 1; memset (MAP, 0, sizeof (MAP )); map [1] [2] = map [1] [3] = map [1] [4] = 1; map [2] [3] = map [2] [5] = map [2] [6] = 1; map [3] [4] = map [3] [5] = map [3] [6] = map [3] [7] = 1; map [4] [6] = map [4] [7] = 1; Map [5] [6] = map [5] [8] = 1; map [6] [7] = map [6] [8] = 1; Map [7] [8] = 1; int N, I; scanf ("% d ", & N); While (n --) {memset (Y, 0, sizeof (y); for (I = 1; I <= 8; I ++) {scanf ("% d", & X [I]), Y [x [I] = 1;} printf ("case % d:", case1 ++ ); sum = 0; DFS (1); // printf ("\ n ------- \ n"); If (sum = 1) {printf ("% d ", temp [1]); for (I = 2; I <= 8; I ++) printf ("% d", temp [I]);} else if (sum = 0) printf ("No answer"); else printf ("not unique"); printf ("\ n");} return 0 ;}

If you do not post an AC post, the test case is wrong, but I think my method is worth sharing, so I will post it, so you don't need to study it! I have already found the problem, that is, I used X [I] in the middle to judge, but when the recursion is back, the value is not restored, so it is enough to erase multiple traces, for more information, see the AC code!

Test code: Use two cases: # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; int map [9] [9]; int X [9], Y [9]; int sum; int temp [9]; void DFS (int t) {printf ("\ n \ t \ TT = % d -------", T); int I, j; If (t = 9) // If to the end, yes. record the result {printf ("if it is the final result, record the result \ n"); sum ++; If (sum = 1) for (I = 0; I <= 8; I ++) temp [I] = x [I]; printf ("% d", X [1]); // output test for (I = 2; I <= 8; I ++) printf ("% d", X [I]); printf ("\ n");} else if (X [T]) // The location has been assigned Value, skip printf ("// This position has been assigned, skip \ n"), DFS (t + 1 ); else // assign I to X [T]-'a + T-1 '{printf ("// assign I to X [T]-'a + T-1' t = % d \ n ", i, T); for (I = 1; I <= 8; I ++) // searches from start to end each time, determine if {printf ("--------- I = % d \ n", I); If (! Y [I]) // I not used {printf ("// I not used \ n"); For (j = 1; j <= 8; j ++) // If (Map [T] [J] | map [J] [T]) & X [J] & ABS (I-X [J]) = 1) {printf ("conflict with X [J] = % d \ n ", X [J]); break;} If (j = 9) // no conflict {printf ("// no conflict \ n"); y [I] = 1; // used, Mark X [T] = I; DFS (t + 1); // y [I] = 0; // when return, undo recursive flag }}} int main () {int case1 = 1; memset (MAP, 0, sizeof (MAP )); map [1] [2] = map [1] [3] = map [1] [4] = 1; map [2] [3] = map [2] [5] = map [2] [6] = 1; map [3] [4] = map [3] [5] = map [3] [6] = map [3] [7] = 1; map [4] [6] = map [4] [7] = 1; Map [5] [6] = map [5] [8] = 1; map [6] [7] = map [6] [8] = 1; Map [7] [8] = 1; int N, I; scanf ("% d ", & N); While (n --) {memset (Y, 0, sizeof (y); for (I = 1; I <= 8; I ++) {scanf ("% d", & X [I]), Y [x [I] = 1;} printf ("case % d: \ n ", case1 ++); sum = 0; DFS (1); printf ("\ n ------- \ n");/* If (sum = 1) {printf ("% d", temp [1]); for (I = 2; I <= 8; I ++) printf ("% d ", temp [I]);} else if (sum = 0) printf ("No answer"); else printf ("not unique"); printf ("\ n "); */printf ("case ended, sum = % d \ n", sum);} return 0;} Test Result: 37 3 1 4 5 8 0 0 case 1: T = 1 ------- // This location has been assigned a value. Skip t = 2 ------- // This location has been assigned a value, skip T = 3 ------- // This position has been assigned, skip T = 4 ------- // This position has been assigned, skip T = 5 ------- // This position has been assigned, skip T = 6 ------- // This position has been assigned, skip T = 7 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used with X [j] = 1 conflict --------- I = 3 --------- I = 4 --------- I = 5 --------- I = 6 // I is not used // no conflict T = 8 ------- // assign I X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used // no conflict T = 9 ------- if to the end, meet the requirements, record Results 7 3 1 4 5 6 2 --------- I = 3 --------- I = 4 --------- I = 5 --------- I = 6 --------- I = 7 --------- I = 8 --------- I = 7 ----------- I = 8 ------- case ended, sum = 17 0 0 0 0 0 0 0 case 2: T = 1 ------- // This position has been assigned, skip t = 2 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 // I not used // no conflict T = 3 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used with X [J] = 1 conflict --------- I = 3 // I not used // no conflict T = 4 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 3 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 // I not used // no conflict T = 5 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 1 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 --------- I = 6 // I not used // no conflict T = 6 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 1 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 --------- I = 6 --------- I = 7 --------- I = 8 // I not used // no conflict T = 7 ------- // assign I to X [T]-' A + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used with X [J] = 3 conflict --------- I = 3 --------- I = 4 // I not conflict with X [J] = 3 --------- I = 5 --------- I = 6 --------- I = 7 --------- I = 8 --------- I = 7 --------- I = 8 --------- I = 6 --------- I = 7 --------- I = 8 --------- I = 4 // I is not used and X [J] = 5 conflict --------- I = 5 --------- I = 6 --------- I = 7 --------- I = 8 ----------- I = 2 // I is not used and X [J] = 3 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 --------- I = 6 --------- I = 7 --------- I = 8 ------- the case is over, sum = 01 0 0 0 0 0 0 0 case 3: T = 1 ------- // This position has been assigned, skip t = 2 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used with X [j] = 1 conflict --------- I = 3 // I not used // no conflict T = 3 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 1 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 // I not used // no conflict T = 4 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 1 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 5 conflict --------- I = 5 --------- I = 6 // I is not used and X [J] = 5 conflict --------- I = 7 // I is not used // no conflict T = 5 -------/ /assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I not used conflict with X [J] = 3 --------- I = 3 --------- I = 4 // I not used and X [J] = 3 conflict --------- I = 5 --------- I = 6 // I not used and X [J] = 5 conflict --------- I = 7 --------- I = 8 // I not used // no conflict T = 6 ------- // assign I to X [T]-'a + T-1't = 2009095316 --------- I = 1 --------- I = 2 // I is not used and X [J] = 3 conflict --------- I = 3 --------- I = 4 // I is not used and X [J] = 3 conflict --------- I = 5 --------- I = 6 // I not used conflict with X [J] = 5 --------- I = 7 --------- I = 8 --------- I = 8 ----------- I = 6 // I is not used and X [J] = 7 Conflict --------- I = 7 --------- I = 8 --------- I = 4 // I is not used and X [J] = 5 conflict --------- I = 5 --------- I = 6 // I is not used and X [J] = 5 conflict --------- I = 7 --------- I = 8 ------- the case is over, sum = 0 Process returned 0 (0x0) execution time: 135.859 spress any key to continue. problem: Recursive Backtracking failed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.