hdoj 2514 Another Eight Puzzle(DFS)
2014-04-25 21:34
399 查看
略恶心,,乱搞居然一遍A掉了。
Total Submission(s): 792 Accepted Submission(s): 494
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
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”.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1: 7 3 1 4 5 8 6 2
Case 2: Not unique
Case 3: No answer
[align=left]Source[/align]
ECJTU 2008 Autumn Contest
想清楚关系,,深搜搞定。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 10;
int flag[20][2] = {1, 2, 1, 3, 1, 4, 2, 3, 2, 5, 2, 6, 3, 4, 3, 5, 3, 6, 3, 7,
4, 6, 4, 7, 5, 6, 5, 8, 6, 7, 6, 8, 7, 8};
int sign
;
int num
, res
;
int vis
;
int cnt;
void init(){
memset(sign, 0, sizeof(sign));
for(int i = 0; i < 17; i++){
int tx = flag[i][0], ty = flag[i][1];
sign[tx][ty] = sign[ty][tx] = 1;
}
}
bool judge(int k, int t){
for(int i = 1; i < k; i++){
if(sign[i][k] && abs(num[i] - t) == 1) return false;
}
return true;
}
void dfs(int cur){
if(cnt > 1) return;
if(cur > 8){
cnt++;
for(int i = 1; i <= 8; i++) res[i] = num[i];
return;
}
if(num[cur]) dfs(cur + 1);
else{
for(int i = 1; i <= 8; i++){
if(!vis[i] && judge(cur, i)){
vis[i] = 1;
num[cur] = i;
dfs(cur + 1);
num[cur] = 0;
vis[i] = 0;
}
}
}
}
int main(){
int T;
init();
scanf("%d", &T);
int cas = 1;
while(T--){
int t;
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
for(int i = 1; i <= 8; i++){
scanf("%d", &num[i]);
vis[num[i]] = 1;
}
cnt = 0;
dfs(1);
printf("Case %d: ", cas++);
if(cnt > 1) puts("Not unique");
else if(cnt == 0) puts("No answer");
else{
for(int i = 1; i < 8; i++) printf("%d ", res[i]);
printf("%d\n", res[8]);
}
}
return 0;
}
Another Eight Puzzle
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 792 Accepted Submission(s): 494
[align=left]Problem Description[/align]
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).
[align=left]Input[/align]
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.
[align=left]Output[/align]
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”.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1: 7 3 1 4 5 8 6 2
Case 2: Not unique
Case 3: No answer
[align=left]Source[/align]
ECJTU 2008 Autumn Contest
想清楚关系,,深搜搞定。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int N = 10;
int flag[20][2] = {1, 2, 1, 3, 1, 4, 2, 3, 2, 5, 2, 6, 3, 4, 3, 5, 3, 6, 3, 7,
4, 6, 4, 7, 5, 6, 5, 8, 6, 7, 6, 8, 7, 8};
int sign
;
int num
, res
;
int vis
;
int cnt;
void init(){
memset(sign, 0, sizeof(sign));
for(int i = 0; i < 17; i++){
int tx = flag[i][0], ty = flag[i][1];
sign[tx][ty] = sign[ty][tx] = 1;
}
}
bool judge(int k, int t){
for(int i = 1; i < k; i++){
if(sign[i][k] && abs(num[i] - t) == 1) return false;
}
return true;
}
void dfs(int cur){
if(cnt > 1) return;
if(cur > 8){
cnt++;
for(int i = 1; i <= 8; i++) res[i] = num[i];
return;
}
if(num[cur]) dfs(cur + 1);
else{
for(int i = 1; i <= 8; i++){
if(!vis[i] && judge(cur, i)){
vis[i] = 1;
num[cur] = i;
dfs(cur + 1);
num[cur] = 0;
vis[i] = 0;
}
}
}
}
int main(){
int T;
init();
scanf("%d", &T);
int cas = 1;
while(T--){
int t;
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
for(int i = 1; i <= 8; i++){
scanf("%d", &num[i]);
vis[num[i]] = 1;
}
cnt = 0;
dfs(1);
printf("Case %d: ", cas++);
if(cnt > 1) puts("Not unique");
else if(cnt == 0) puts("No answer");
else{
for(int i = 1; i < 8; i++) printf("%d ", res[i]);
printf("%d\n", res[8]);
}
}
return 0;
}
相关文章推荐
- 安卓开发者必备的42个链接
- 2014年实习生招聘之腾讯实习生招聘面试(一面)—2014/04/01
- c++ 使用蚁群算法解决TSP问题。
- malloc/free与new/delete的区别
- Nodejs环境搭建
- 逃票的chanming(1)
- HDU 1048 The Hardest Problem Ever
- 今天是讲解struts框架实现的jsp页面的跳转
- poj 2387 Til the Cows Come Home spfa+dis解答
- 给节点设置tag【从零开始cocos3.0final 】
- 【jQuery】页面顶部显示的进度条效果
- 2013 通化邀请赛
- 新一周冲刺计划2
- C/C++每日小练(七)——墓地雕塑
- 线性表(二)
- DNS \ ARP 解析过程
- linux下通过命令行使用Vtune统计处理器微体系结构特征
- 大数据的排序问题
- 消息事件通知拓展点
- eclipse中package部分包变成了文件夹