hdoj 2514 Another Eight Puzzle(DFS)
来源:互联网 发布:手绘软件有哪些 编辑:程序博客网 时间:2024/05/22 03:05
略恶心,,乱搞居然一遍A掉了。
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
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).
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
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
Source
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[N][N];int num[N], res[N];int vis[N];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;}
0 0
- hdoj 2514 Another Eight Puzzle(DFS)
- HDU 2514--Another Eight Puzzle【DFS】
- hdu 2514 Another Eight Puzzle(DFS+回溯)
- hdu 2514 Another Eight Puzzle(DFS暴搜)
- (全排列)Another Eight Puzzle--HDOJ
- hdu 2541 Another Eight Puzzle(dfs)
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- HDU 2514 Another Eight Puzzle
- hdu(2514) Another Eight Puzzle
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle 枚举
- hdu 2514—— Another Eight Puzzle
- hdu2514 Another Eight Puzzle
- Another Eight Puzzle
- Another Eight Puzzle
- 迷糊的大学
- struts2+hibernate整合出现not found session的异常
- [翻译] Experiences from a Decade of TinyOS Development
- 2014年实习生招聘之腾讯实习生招聘面试(一面)—2014/04/01
- kinect骨骼追踪在舞蹈表演评估中的应用
- hdoj 2514 Another Eight Puzzle(DFS)
- 安卓开发者必备的42个链接
- API实现串口通讯
- JSP Filter执行流程猜想
- Github入门
- 书都买回来了,必须要努力的去学习了!
- OpenCV 读写XML
- 一个简单的求学生成绩的小程序
- windows中用source insight查看linux中的程序