hdu 2514 Another Eight Puzzle 图填数字 next_permutation+暴力
2013-07-25 17:39
495 查看
题目已经给出八个点以及各点的连接情况,要求两个相连的点的值不能连续,输入部分点的值,让你填其他点的值,检查能否填出已经是否有唯一解。
只有八个点,把要填的数值排序填入点中,然后检查一下就可以了。
代码:
只有八个点,把要填的数值排序填入点中,然后检查一下就可以了。
代码:
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 9; int que[maxn], pre[maxn], pos[maxn], rec[maxn]; bool app[maxn]; int cnt = 0; int a[17] = {1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7}; int b[17] = {2, 3, 4, 3, 5, 6, 4, 5, 6, 7, 6, 7, 6, 8, 7, 8, 8}; void check() { for (int i = 0; i < 17; i++) if (pos[a[i]] - pos[b[i]] == 1 || pos[b[i]] - pos[a[i]] == 1) return; if (cnt++ > 1) return; for (int i = 1; i <= 9; i++) rec[i] = pos[i]; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { cnt = 0; memset(app, 0, sizeof(app)); for (int j = 1; j <= 8; j++) { scanf("%d", &pre[j]); pos[j] = pre[j]; app[pre[j]] = true; } int t = 0; for (int j = 1; j <= 8; j++) if (!app[j]) que[t++] = j; do { int p = 0; for (int j = 1; j <= 8; j++) { if (pre[j] == 0) pos[j] = que[p++]; // printf("%d ", pos[j]); } // printf("\n"); check(); } while (next_permutation(que, que + t)); printf("Case %d:", i); if (cnt == 1) for (int j = 1; j <= 8; j++) printf(" %d", rec[j]); else if (cnt > 1) printf(" Not unique"); else printf(" No answer"); printf("\n"); } return 0; }
相关文章推荐
- 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【DFS】
- HDU 2514 Another Eight Puzzle(DFS)
- HDU 2514 Another Eight Puzzle
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle(DFS暴搜)
- Problem V:Another Eight Puzzle(HDU 2514)
- hdu 2514 Another Eight Puzzle(DFS+回溯)
- HDU 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle
- hdu 2514 Another Eight Puzzle 枚举
- HDU [ Another Eight Puzzle ]——dfs全排列变式
- hdoj 2514 Another Eight Puzzle(DFS)
- hdu2514 Another Eight Puzzle 填数字 搜索水题
- hdu 2518(dfs)Another Eight Puzzle
- hdu2514 Another Eight Puzzle (全排列、暴力)