UVa211 The Domino Effect (DFS)

链接:http://vjudge.net/problem/35575

分析:dfs暴力。

 1 #include <cstdio>
 2 #include <cstring>
 3
 4 const int r = 7;
 5 const int c = 8;
 6 const int m = 30;
 7 const int dir[2][2] = {
 8     {0, 1},
 9     {1, 0},
10 };
11
12 int map[r][c], id[r][r], checked[m], vis[r][c], ans;
13
14 void build() {
15     int num = 1;
16     for (int i = 0; i < r; i++)
17         for (int j = i; j < r; j++)
18             id[i][j] = id[j][i] = num++;
19 }
20
21 bool init() {
22     ans = 0;
23     memset(checked, 0, sizeof(checked));
24     memset(vis, 0, sizeof(vis));
25     for (int i = 0; i < r; i++)
26         for (int j = 0; j < c; j++)
27             if (scanf("%d", &map[i][j]) != 1) return false;
28     return true;
29 }
30
31 void print() {
32     for (int i = 0; i < r; i++) {
33         for (int j = 0; j < c; j++) printf("%4d", vis[i][j]);
34         printf("\n");
35     }
36     printf("\n");
37 }
38
39 void dfs(int x, int y, int cnt) {
40     if (cnt == 28) {
41         ans++;
42         print();
43         return;
44     }
45     if (y == c) { x++; y = 0; }
46
47     if (vis[x][y]) dfs(x, y + 1, cnt);
48     else {
49         for (int i = 0; i < 2; i++) {
50             int nx = x + dir[i][0], ny = y + dir[i][1];
51             if (nx >= r || ny >= c || vis[nx][ny]) continue;
52             int n = id[map[x][y]][map[nx][ny]];
53             if (checked[n]) continue;
54             vis[x][y] = vis[nx][ny] = n; checked[n] = 1;
55             dfs(x, y + 1, cnt + 1);
56             vis[x][y] = vis[nx][ny] = 0; checked[n] = 0;
57         }
58     }
59 }
60
61 int main() {
62     build();
63     int kase = 0;
64     while (init()) {
65         if (kase) printf("\n\n\n");
66         printf("Layout #%d:\n\n", ++kase);
67         for (int i = 0; i < r; i++) {
68             for (int j = 0; j < c; j++)
69                 printf("%4d", map[i][j]);
70             printf("\n");
71         }
72         printf("\nMaps resulting from layout #%d are:\n\n", kase);
73         dfs(0, 0, 0);
74         printf("There are %d solution(s) for layout #%d.\n", ans, kase);
75     }
76     return 0;
77 }
时间: 2024-12-23 05:31:12

UVa211 The Domino Effect (DFS)的相关文章

POJ 1135 Domino Effect(最短路 多米诺骨牌)

题意 题目描述: 你知道多米诺骨牌除了用来玩多米诺骨牌游戏外,还有其他用途吗?多米诺骨牌游戏:取一 些多米诺骨牌,竖着排成连续的一行,两张骨牌之间只有很短的空隙.如果排列得很好,当你推 倒第 1张骨牌,会使其他骨牌连续地倒下(这就是短语"多米诺效应"的由来). 然而当骨牌数量很少时,这种玩法就没多大意思了,所以一些人在 80 年代早期开创了另一个 极端的多米诺骨牌游戏:用上百万张不同颜色.不同材料的骨牌拼成一幅复杂的图案.他们开创 了一种流行的艺术.在这种骨牌游戏中,通常有多行骨牌同时

POJ 1135.Domino Effect

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10325   Accepted: 2560 Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing

POJ 1135 Domino Effect (Dijkstra 最短路)

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9335   Accepted: 2325 Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing t

poj 1135 Domino Effect (dijkratra算法)

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8848   Accepted: 2210 Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing t

POJ1135 Domino Effect(SPFA)

题目大概是,普通骨牌连接两张关键骨牌,一旦一张关键骨牌倒下与其相邻的普通骨牌也倒下,普通骨牌倒下与其相邻的骨牌也倒下.给出所有有普通骨牌相连的两个关键骨牌之间普通骨牌倒下所需时间,问1号关键骨牌开始倒下后最后一张骨牌倒下的时间和位置. 居然是最短路..记dist[u]为起点骨牌1到关键骨牌u的最短时间,跑个单源最短路可得出.最后倒下的骨牌,有两种情况: 倒下的是关键骨牌u,那么倒下的时间就是dist[u] 倒下的是关键骨牌u和v之间某张普通骨牌,那么倒下的时间就是 (dist[u]+dist[v

POJ 1135 Domino Effect(最短路径)

题意有m排放置好的多米诺骨牌,每排的头尾骨牌称为“关键骨牌”,共有n个关键骨牌,每排多米诺倒下需要L秒,关键骨牌倒下的时间忽略不计.推倒关键骨牌1,求最后一个骨牌倒下的时间及位置.(若最后一个倒下的骨牌不是关键骨牌,则按升序输出这个骨牌所在的那一排的两端的关键骨牌) 样例输入2 11 2 273 31 2 51 3 52 3 50 0 样例输出System #1The last domino falls after 27.0 seconds, at key domino 2. System #2

uva-211-The Domino Effect

http://uva.onlinejudge.org/external/2/211.html http://uva.onlinejudge.org/external/2/211.pdf 题意:每一种骨牌(Bone) 相应了两个球(Pip). 球的数值从0-6.骨牌从1-28. 然后给你一个包括球数值的矩阵(7*8).问你什么样的骨牌会形成这种球的矩阵. 注意.题目有一个信息没有讲明确,那就是每种骨牌仅仅能取一个,而且要取到全部的28种骨牌.7*8/28 = 2. 思路:对于每种骨牌.先从矩阵中寻

uva 211(dfs)

211 - The Domino Effect Time limit: 3.000 seconds A standard set of Double Six dominoes contains 28 pieces (called bones) each displaying two numbersfrom 0 (blank) to 6 using dice-like pips. The 28 bones, which are unique, consist of the followingcom

POJ1135_Domino Effect(最短路)

Domino Effect Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8224   Accepted: 2068 Description Did you know that you can use domino bones for other things besides playing Dominoes? Take a number of dominoes and build a row by standing t