UVa 211 - The Domino Effect [DFS剪枝]
来源:互联网 发布:华云数据面试 编辑:程序博客网 时间:2024/06/10 03:57
这道题只看书上的中文解释可能有些费力,理解题后还是比较简单的,对每个数字看能否与右边或下边的数字配对组成一张牌,注意加各种剪枝就行。
#include <bits/stdc++.h>using namespace std;int kase, solution;int tab[10][10], res[10][10], vis[30];int dict[7][7];const int dir[2][2] = { { 0, 1 }, { 1, 0 } };void init(){++kase;solution = 0;memset(tab, 0, sizeof(tab));memset(res, 0, sizeof(res));memset(vis, 0, sizeof(vis));int cnt = 0;for (int i = 0; i <= 6; ++i) for (int j = i; j <= 6; ++j) dict[i][j] = ++cnt;}bool in(){for (int i = 1; i <= 7; ++i) for (int j = 1; j <= 8; ++j) if(!(cin >> tab[i][j])) return false; if(kase>=2) printf("\n\n\n");printf("Layout #%d:\n\n", kase);for (int i = 1; i <= 7; ++i){for (int j = 1; j <= 8; ++j)printf("%4d", tab[i][j]);printf("\n");}printf("\n");return true;}void out(){ if(!solution) printf("Maps resulting from layout #%d are:\n", kase);++solution;printf("\n");for (int i = 1; i <= 7; ++i){for (int j = 1; j <= 8; ++j)printf("%4d", res[i][j]);printf("\n");}}inline int calc(int x, int y){if (x > y) swap(x, y);return dict[x][y];}void DFS(int d, int r0, int c0){if (d == 28) {out(); return;}if (c0==9) c0 = 1, ++r0;if (res[r0][c0]){ DFS(d, r0, c0 + 1); return;}for (int i = 0; i < 2; ++i){int r = r0 + dir[i][0], c = c0 + dir[i][1];if(r > 7 || c > 8 || res[r][c]) continue;int val = calc(tab[r0][c0], tab[r][c]);if (!vis[val]){vis[val] = 1; res[r][c] = res[r0][c0] = val;DFS(d + 1, r0, c0 + 1);vis[val] = 0; res[r][c] = res[r0][c0] = 0;}}}int main(){ ios::sync_with_stdio(false);while (true){init();if(!in()) return 0;DFS(0, 1, 1);printf("\nThere are %d solution(s) for layout #%d.\n", solution, kase);}return 0;}
0 0
- UVa 211 - The Domino Effect [DFS剪枝]
- UVA 211 - The Domino Effect(dfs+剪枝)
- uva 211 - The Domino Effect(DFS)
- UVa 211 - The Domino Effect (DFS)
- uva-211-The Domino Effect
- UVA 211 The Domino Effect
- UVA 211 The Domino Effect
- uva 211 The Domino Effect
- The Domino Effect (UVA
- The Domino Effect UVA
- The Domino Effect UVA
- UVA 211(p215)----The Domino Effect
- UVA 211 The Domino Effect 【搜索】
- UVa211 - The Domino Effect(DFS)
- UVa 211 - The Domino Effect(枚举+回溯)
- Uva211 The Domino Effect 【dfs回溯】【习题7-3】
- 211 - The Domino Effect(暴力)
- UVa211 - The Domino Effect
- lua学习笔记---值,类型
- C++设计模式
- 数组-12. 简易连连看(20)
- 二叉树路径和
- Unity3D架构系列之- FSM有限状态机设计(六)(总结篇)
- UVa 211 - The Domino Effect [DFS剪枝]
- hdu 5172 线段树
- hiho #1079 离散化
- UVa #1025 A Spy in the Metro (例题9-1)
- UVA 10006 Carmichael Numbers (伪素数)
- 关于Cocos2d-x的一些认识(3)
- Poj 3009 Curling 2.0 (Dfs)
- 机器学习领域的几种主要学习方式
- 透明加密 SDK