训练2 23题
来源:互联网 发布:playclub人物数据 编辑:程序博客网 时间:2024/05/29 02:31
题目: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.<br>There are 17 pairs of connected cicles:<br>A-B , A-C, A-D<br>B-C, B-E, B-F<br>C-D, C-E, C-F, C-G<br>D-F, D-G<br>E-F, E-H<br>F-G, F-H<br>G-H<br><center><img src=../../../data/images/C150-1010-1.jpg></center><br>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 .<br><br>In this problems,some circles are already filled,your tast is to fill the remaining circles to obtain a solution (if possivle).<br>
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.<br><br>
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”.<br>
Sample Input
3<br>7 3 1 4 5 8 0 0<br>7 0 0 0 0 0 0 0<br>1 0 0 0 0 0 0 0<br>
Sample Output
Case 1: 7 3 1 4 5 8 6 2<br>Case 2: Not unique<br>Case 3: No answer<br>
Source
ECJTU 2008 Autumn Contest
思路:类似数独问题,将8个数字填入图中,连线两端的数字不能连续,因为线有17条,所以check函数写得非常长,其他的跟一般的DFS题目没有大区别,就是其他的题目只需恢复标记数组,而这个题目还需要恢复填写数字的数组。
#include <cstdio>
#include <iostream>#include <string>
#include <sstream>
#include <cstring>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <map>
#define PI acos(-1.0)
#define ms(a) memset(a,0,sizeof(a))
#define msp memset(mp,0,sizeof(mp))
#define msv memset(vis,0,sizeof(vis))
using namespace std;
//#define LOCAL
int sign[10];
int mp[10];
int ans,res[10];
bool check(int n)
{
switch(n)
{
case 1:
{
if(abs(mp[0]-mp[1])==1)return 0;
return 1;
}
case 2:
{
if(abs(mp[0]-mp[2])==1)return 0;
if(abs(mp[1]-mp[2])==1)return 0;
return 1;
}
case 3:
{
if(abs(mp[0]-mp[3])==1)return 0;
if(abs(mp[2]-mp[3])==1)return 0;
return 1;
}
case 4:
{
if(abs(mp[2]-mp[4])==1)return 0;
if(abs(mp[1]-mp[4])==1)return 0;
return 1;
}
case 5:
{
if(abs(mp[1]-mp[5])==1)return 0;
if(abs(mp[2]-mp[5])==1)return 0;
if(abs(mp[3]-mp[5])==1)return 0;
if(abs(mp[4]-mp[5])==1)return 0;
return 1;
}
case 6:
{
if(abs(mp[2]-mp[6])==1)return 0;
if(abs(mp[3]-mp[6])==1)return 0;
if(abs(mp[5]-mp[6])==1)return 0;
return 1;
}
case 7:
{
if(abs(mp[4]-mp[7])==1)return 0;
if(abs(mp[5]-mp[7])==1)return 0;
if(abs(mp[6]-mp[7])==1)return 0;
return 1;
}
}
return 1;
}
void dfs(int n)
{
if(n==7)
{
if(mp[n]==0)
{
for(int i=1; i<=8; i++)
{
if(sign[i]==0)
{
mp[n]=i;
sign[i]=1;
if(check(n))
{
ans++;
for(int j=0; j<8; j++)
res[j]=mp[j];
}
sign[i]=0;
mp[n]=0;
}
}
}
else
{
if(check(n))
{
ans++;
for(int j=0; j<8; j++)
res[j]=mp[j];
}
}
}
else
{
if(mp[n]==0)
{
for(int i=1; i<=8; i++)
{
if(sign[i]==0)
{
mp[n]=i;
sign[i]=1;
if(check(n))dfs(n+1);
sign[i]=0;
mp[n]=0;
}
}
}
else
{
if(check(n))dfs(n+1);
}
}
return;
}
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif // LOCAL
ios::sync_with_stdio(false);
int n,cas=0;
cin>>n;
while(n--)
{
cas++;
ans=0,ms(res),ms(mp),ms(sign);
for(int i=0; i<8; i++)
{
cin>>mp[i];
if(mp[i]!=0)sign[mp[i]]=1;
}
for(int i=0; i<8; i++)
{
if(mp[i]==0)
{
dfs(i);
break;
}
}
if(ans==1)
{
printf("Case %d:",cas);
for(int i=0; i<8; i++)
printf(" %d",res[i]);
printf("\n");
}
else if(ans==0)printf("Case %d: No answer\n",cas);
else printf("Case %d: Not unique\n",cas);
}
return 0;
}
0 0
- 训练2 23题
- 训练2 11题
- 训练2 15题
- 训练2 8题
- 训练2 19题
- 训练2 4题
- 训练题
- 2017-10-23校内训练题题解
- 第五周训练赛2--B题
- 暑假训练第二期---思维题2
- JS编程训练 | 题2:数组求和
- 寒假训练---训练赛2--Fighting
- 寒假训练--训练赛2--Good Luck!
- 寒假训练--训练赛2--加密术
- 训练4 习题23
- 训练日记-23
- 11/23训练日记
- 单片机训练题
- 重拾梦想 C++
- 从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS
- [DynamicProgramming]动态规划题目泛做
- UITableView 的项目运用
- 1008 猜数字
- 训练2 23题
- 敏感词过滤
- JBoss4.0.0部署
- 从上往下打印二叉树
- PHP简介
- Android-Toast的属性设置
- BaseAdapter和Viewholder的抽取(模版设计模式)
- csdn markdown格式的一些记录
- Native与H5交互的那些事