Another Eight Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 152 Accepted Submission(s): 103
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 A~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
ECJTU 2008 Autumn Contest
Recommend
lcy
思路:
利用next_permutation()函数将未利用的数字进行全排列,然后填充到初始数组当中,每进行一次排列,就检查一次是否符合条件。如果符合条件,ans就加一,超过2的时候,直接break就行
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<math.h>
#define PI acos(-1.0)
#define eps 0.00000001
using namespace std;
int mp[10][10];
int vis[10],num[10],res[10];
int Check(int tmp[])
{
for(int i=1; i<=8; ++i)
for(int j=1; j<=8; ++j)
if(mp[i][j] && abs(tmp[i] - tmp[j]) ==1)
{
// cout << tmp[i] << ' '<<tmp[j]<<"---";
return 0;
}
return 1;
}
main(void)
{
// freopen("in.txt","r",stdin);
int ncase,cnt=1;
cin>>ncase;
while(ncase--)
{
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
mp[1][2] = mp[2][1] = mp[1][3] = mp[3][1] = mp[1][4] = mp[4][1] = 1;
mp[2][3] = mp[3][2] = mp[2][5] = mp[5][2] = mp[2][6] = mp[6][2] = 1;
mp[3][4] = mp[4][3] = mp[3][5] = mp[5][3] = mp[3][6] = mp[6][3] = mp[3][7] = mp[7][3] = 1;
mp[4][6] = mp[6][4] = mp[4][7] = mp[7][4] = 1;
mp[5][6] = mp[6][5] = mp[5][8] = mp[8][5] = 1;
mp[7][6] = mp[6][7] = mp[6][8] = mp[8][6] = 1;
mp[7][8] = mp[8][7] = 1;
int tmp[10],lst[10],lstNum=0;
for(int i=1; i<=8; ++i)
{
cin >> num[i];
tmp[i] = num[i];
vis[num[i]] = 1;
}
//没有用过的数
for(int i=1; i<=8; ++i)
if(!vis[i]) lst[lstNum++] = i;
int ans = 0;
do
{
int nxt = 0;
//填补数字
for(int i=1; i<=8; ++i)
if(!num[i]) tmp[i] = lst[nxt++];
//送去检测
if(Check(tmp))
{
ans++;
for(int i=1; i<=8; ++i)
res[i] = tmp[i];
}
// cout << "--"<<ans<<endl;
if(ans > 1) break;
}while(next_permutation(lst,lst+lstNum));
if(ans == 0)
printf("Case %d: No answer\n",cnt++);
else if(ans > 1)
printf("Case %d: Not unique\n",cnt++);
else if(ans == 1)
{
printf("Case %d:",cnt++);
for(int i=1; i<=8; ++i)
cout << ' '<<res[i];
cout << endl;
}
}
return 0;
}