日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU Problem - 5113 Black And White(搜索剪枝)

發布時間:2024/4/18 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU Problem - 5113 Black And White(搜索剪枝) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

Problem Description

In mathematics, the four color theorem, or the four color map theorem, states that, given any separation of a plane into contiguous regions, producing a figure called a map, no more than four colors are required to color the regions of the map so that no two adjacent regions have the same color. — Wikipedia, the free encyclopedia In this problem, you have to solve the 4-color problem. Hey, I’m just joking. You are asked to solve a similar problem: Color an N × M chessboard with K colors numbered from 1 to K such that no two adjacent cells have the same color (two cells are adjacent if they share an edge). The i-th color should be used in exactly ci cells. Matt hopes you can tell him a possible coloring.

Input

The first line contains only one integer T (1 ≤ T ≤ 5000), which indicates the number of test cases. For each test case, the first line contains three integers: N, M, K (0 < N, M ≤ 5, 0 < K ≤ N × M ). The second line contains K integers ci (ci > 0), denoting the number of cells where the i-th color should be used. It’s guaranteed that c1 + c2 + · · · + cK = N × M .

Output

For each test case, the first line contains “Case #x:”, where x is the case number (starting from 1). In the second line, output “NO” if there is no coloring satisfying the requirements. Otherwise, output “YES” in one line. Each of the following N lines contains M numbers seperated by single whitespace, denoting the color of the cells. If there are multiple solutions, output any of them.

Sample Input

4
1 5 2
4 1
3 3 4
1 2 2 4
2 3 3
2 2 2
3 2 3
2 2 2

Sample Output

Case #1:
NO
Case #2:
YES
4 3 4
2 1 2
4 3 4
Case #3:
YES
1 2 3
2 3 1
Case #4:
YES
1 2
2 3
3 1

AC

  • dfs爆搜+剪枝
  • 如果沒用的顏色大于剩余空格的二倍,就必定相鄰
  • 從第一個位置,向右移動
#include <bits/stdc++.h> using namespace std;int n, m, k; int a[10][10]; int color[30]; bool vis[30]; bool judge(int x, int y, int tar) {if (a[x - 1][y] == tar || a[x][y - 1] == tar)return false;elsereturn true; } bool dfs(int x, int y, int num) {// 剪枝 for (int i = 1; i <= k; ++i) {if ((num + 1) / 2 < color[i])return false;}if (num == 0) return true;for (int i = 1; i <= k; ++i) {if (!color[i] || !judge(x, y, i)) continue;vis[i] = true;a[x][y] = i;color[i]--;int xx, yy;if (y == m)xx = x + 1, yy = 1;elsexx = x, yy = y + 1;if (dfs(xx, yy, num - 1))return true;else {vis[i] = false;color[i]++;a[x][y] = 0;}}return false; } int main() {int t;scanf("%d", &t);int Case = 1;while (t--) {printf("Case #%d:\n", Case++);scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= k; ++i) {scanf("%d", &color[i]);}memset(vis, false, sizeof(vis));if (dfs(1, 1, n * m)) {printf("YES\n");for (int i = 1; i <= n; ++i) {for (int j = 1; j <= m; ++j) {if (j == 1) printf("%d", a[i][j]);else printf(" %d", a[i][j]);}printf("\n");}}elseprintf("NO\n");}return 0; }

總結

以上是生活随笔為你收集整理的HDU Problem - 5113 Black And White(搜索剪枝)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。