我是靠谱客的博主 刻苦小兔子,这篇文章主要介绍Hdu OJ 5113 Black And White (2014ACM/ICPC亚洲区北京站) (搜索),现在分享给大家,希望可以做个参考。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113

题目大意:有k种颜色的方块,每种颜色有ai个, 现在有n*m的矩阵, 问这k种颜色的方块能否使任意两个相连的方块颜色不一样填满整个矩阵,如果可以输出任意一种。

解题思路:由于n,m在[1, 5]内, 所以直接暴力枚举; 需要注意的是:需要剪枝一下。

代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<stdio.h> #include<string.h> #include<cmath> #include<queue> #include<algorithm> using namespace std; typedef long long ll; const int N = 103; int n, m, k; int a[N], mp[N][N]; bool is; bool judge(int x, int y, int s) { bool p = true, q = true; if(x - 1 >= 1 && mp[x-1][y] == s) p = false; if(y - 1 >= 1 && mp[x][y-1] == s) q = false; return p && q; } void dfs(int x, int y, int cou) { int sum = 0, mx = 0; for(int i=1; i<=k; ++ i) { sum += a[i]; if(a[i] > mx) mx = a[i]; } if(sum - mx < mx - 1) return; if(cou == n*m) { is = true; return ; } for(int i=1; i<=k; ++ i) { if(a[i] && judge(x, y, i)) { a[i] --; mp[x][y] = i; if(y == m) dfs(x+1, 1, cou + 1); else dfs(x, y+1, cou + 1); if(is) return ; a[i] ++; } } } void solve(int cases) { scanf("%d%d%d", &n, &m, &k); int sum = 0, mx = 0; for(int i=1; i<=k; ++ i) { scanf("%d", &a[i]); sum += a[i]; if(a[i] > mx) mx = a[i]; } printf("Case #%d:n", cases); if(sum - mx < mx - 1) printf("NOn"); else { printf("YESn"); memset(mp, -1, sizeof(mp)); is = false; dfs(1, 1, 0); for(int i=1; i<=n; ++ i) for(int j=1; j<=m; ++ j) printf(j == m? "%dn" : "%d ", mp[i][j]); } } int main() { int t; scanf("%d", &t); for(int i = 1; i <= t; ++ i) { solve(i); } return 0; }
View Code

 

转载于:https://www.cnblogs.com/aiterator/p/5897232.html

最后

以上就是刻苦小兔子最近收集整理的关于Hdu OJ 5113 Black And White (2014ACM/ICPC亚洲区北京站) (搜索)的全部内容,更多相关Hdu内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(89)

评论列表共有 0 条评论

立即
投稿
返回
顶部