Hdu2514 Another Eight Puzzle: enter a number to search for a question

Source: Internet
Author: User

Another Eight Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 659 Accepted Submission (s): 405

 

Problem Description
Fill 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 ).

 


Input
The 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.


 


Output
For 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
3
7 3 1 4 5 8 0 0
7 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0


Sample Output
Case 1: 7 3 1 4 5 8 6 2
Case 2: Not unique
Case 3: No answer


Source
Ecjtu2008 Autumn Contest
 


Recommend
Lcy
 

Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2514

 

Input 8 numbers indicates that the value at the position of a B c d e f g h in the figure can only be 1-8 numbers, and the adjacent numbers cannot be consecutive numbers, that is, the absolute value cannot be 1. Some numbers are 0. enter the number of unused values from 1 to 8.

How many methods are there? If there is only one output, it has multiple or not output as in the sample.

 

Train of Thought DFS search

[Cpp] # include <stdio. h>
# Include <math. h>
# Include <string. h>
Int a [10], vis [10], ans [10], anscnt;
Int abs (int q)
{
If (q <0) return-q;
Return q;
}
Int OK ()
{
If (abs (a [2]-a [1])! = 1 &&
Abs (a [3]-a [1])! = 1 &&
Abs (a [4]-a [1])! = 1 &&

Abs (a [2]-a [3])! = 1 &&
Abs (a [2]-a [5])! = 1 &&
Abs (a [2]-a [6])! = 1 &&

Abs (a [3]-a [4])! = 1 &&
Abs (a [3]-a [5])! = 1 &&
Abs (a [3]-a [6])! = 1 &&
Abs (a [3]-a [7])! = 1 &&

Abs (a [4]-a [6])! = 1 &&
Abs (a [4]-a [7])! = 1 &&

Abs (a [5]-a [6])! = 1 &&
Abs (a [5]-a [8])! = 1 &&

Abs (a [6]-a [7])! = 1 &&
Abs (a [6]-a [8])! = 1 &&

Abs (a [7]-a [8])! = 1
)
Return 1;
Else return 0;
}
Void DFS (int k)
{
Int I, cnt = 0;
If (k = 9)
// Note that here k = 9 instead of putting it after 8 at the end of the called function. Then the last value assignment will be restored to 0.
{
If (OK ())
{
Anscnt ++;
If (anscnt = 1)
{
For (I = 1; I <= 8; I ++)
Ans [I] = a [I];
}
}
Return;
}
If (anscnt> = 2) return;
If (a [k]! = 0) DFS (k + 1 );
Else
For (I = 1; I <= 8; I ++)
{
If (! Vis [I])
{
A [k] = I;
Vis [I] = 1;
DFS (k + 1 );
A [k] = 0;
Vis [I] = 0;
}
}
}
 
Int main ()
{
Int t, cas = 0;
Scanf ("% d", & t );
While (t --)
{
Anscnt = 0;
Int I;
Memset (vis, 0, sizeof (vis ));
Memset (ans, 0, sizeof (ans ));
Scanf ("% d", & a [1], & a [2], & a [3], & a [4], & a [5], & a [6], & a [7], & a [8]);
For (I = 1; I <= 8; I ++) vis [a [I] = 1;
DFS (1 );
Printf ("Case % d:", ++ cas );
If (anscnt = 1)
{
For (I = 1; I <8; I ++) printf ("% d", ans [I]);
Printf ("% d \ n", ans [I]);
}
Else if (anscnt = 0) printf ("No answer \ n ");
Else printf ("Not unique \ n ");
}
Return 0;
}

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.