UVA 211 The Domino Effect 【搜索】
2017-04-30 14:20
465 查看
题目链接:https://vjudge.net/problem/UVA-211
题意:
每个多米诺骨牌可以由两个数字组成,给你一个7*8的网格,问能构成多少个骨牌号码,号码不能重复
题解:
首先大家不要D我的翻译,,
还有,lrj先生的紫书里面没提到不能用重复的,mdzz..
剩下的就是简单的搜索就行了。
映射的求法?刚开始也困扰我一些时间。
比对骨牌号码和数字,我们发现一个规律。
答案就是:
代码:
题意:
每个多米诺骨牌可以由两个数字组成,给你一个7*8的网格,问能构成多少个骨牌号码,号码不能重复
题解:
首先大家不要D我的翻译,,
还有,lrj先生的紫书里面没提到不能用重复的,mdzz..
剩下的就是简单的搜索就行了。
映射的求法?刚开始也困扰我一些时间。
比对骨牌号码和数字,我们发现一个规律。
答案就是:
int c = 0; for ( int i = 0; i < 7; i ++ ) { for ( int j = 0; j < 7; j ++ ) mp[i][j] = mp[j][i] = ++c; }
代码:
// by DenyTianly #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int inf = 1 << 26; int g[505][505], mp[505][505]; int vis[505][505], choose[505]; int d[][2] = { {1, 0}, {0, 1} }; int tot = 0; void initmp() { // 求映射 int c = 0; for ( int i = 0; i < 7; i ++ ) { for ( int j = i; j < 7; j ++ ) mp[i][j] = mp[j][i] = ++c; } } void outans() { // 输出 for ( int i = 0; i < 7; i ++ ) { for ( int j = 0; j < 8; j ++ ){ printf("%4d", vis[i][j]); } puts(""); } puts(""); } void dfs(int x, int y, int cnt) { // 搜索 if(cnt == 28) { // 7*8/2 = 28,即搜索完成 tot ++; outans(); return ; } if(y == 8) { // 换行 y = 0; x ++; } if(vis[x][y]){ dfs(x, y+1, cnt); return ; } // 访问过该点直接换下一个 for ( int i = 0; i < 2; i ++ ) { // 骨牌可以向下或者向右放置 int fx = x+d[i][0], fy = y+d[i][1]; if(fx >= 7 || fy >= 8 || vis[fx][fy]) continue; int k = mp[g[x][y]][g[fx][fy]]; if(choose[k] == 1) continue; vis[fx][fy] = vis[x][y] = k; choose[k] = 1; dfs(x, y+1, cnt+1); vis[fx][fy] = vis[x][y] = 0; choose[k] = 0; } return ; } int cas = 0; int main(){ // freopen("UVA211.in", "r", stdin); initmp(); while( scanf("%d", &g[0][0]) != EOF ) { memset(vis, 0, sizeof(vis)); memset(choose, 0, sizeof(choose)); tot = 0; ++ cas; if(cas > 1) puts("\n\n"); printf("Layout #%d:\n\n", cas); for ( int i = 0; i < 7; i ++ ) { for ( int j = 0; j < 8; j ++ ) if(!(!i&&!j)) scanf("%d", &g[i][j]); } for ( int i = 0; i < 7; i ++ ) { for ( int j = 0; j < 8; j ++ ) { printf("%4d", g[i][j]); } puts(""); } printf("\nMaps resulting from layout #%d are:\n\n", cas); dfs(0, 0, 0); printf("There are %d solution(s) for layout #%d.\n", tot, cas); } return 0; }
相关文章推荐
- uva 211 - The Domino Effect(DFS)
- 【习题 7-3 UVA - 211】The Domino Effect
- UVA 211 The Domino Effect
- UVA 211 The Domino Effect
- UVa 211 - The Domino Effect [DFS剪枝]
- UVa 211 - The Domino Effect [DFS剪枝]
- The Domino Effect UVA - 211
- uva-211-The Domino Effect
- UVa 211 - The Domino Effect(枚举+回溯)
- UVa211 - The Domino Effect(DFS)
- UVA 211(p215)----The Domino Effect
- UVA 211 The Domino Effect 多米诺效应 (回溯)
- UVA 211 The Domino Effect
- uva-211-The Domino Effect
- The Domino Effect UVA - 211
- 习题7-3 多米诺效应(The Domino Effect, ACM/ICPC World Finals 1991, UVa211)
- UVa 211 - The Domino Effect (DFS)
- 211 - The Domino Effect(暴力)
- uva 539 - The Settlers of Catan(搜索)
- UVa 11214 - Guarding the Chessboard(迭代加深搜索)