Black And White
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 3937 Accepted Submission(s): 1082
Special Judge
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.
— 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 .
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.
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
//题意:第一行一个 T ,然后3个整数 n,m,k 代表有个 n 行 m 列的矩阵,k 代表有k种颜色,然后是 k 种颜色分别有多少个
要用这些颜色涂满矩阵,相邻颜色不能相同,问能否填出
DFS+剪枝,剪枝就是,如果有一种颜色比剩下的格子+1的一半还多的话,就 return
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <math.h> 6 #include <algorithm> 7 #include <map> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <vector> 12 using namespace std; 13 #define LL long long 14 #define PI acos(-1.0) 15 #define lowbit(x) (x&(-x)) 16 #define INF 0x7f7f7f7f // 21 E 17 #define MEM 0x7f // memset 都变为 INF 18 #define MOD 4999 // 模 19 #define eps 1e-9 // 精度 20 #define MX 10 // 数据范围 21 22 int read() { //输入外挂 23 int res = 0, flag = 0; 24 char ch; 25 if((ch = getchar()) == '-') flag = 1; 26 else if(ch >= '0' && ch <= '9') res = ch - '0'; 27 while((ch = getchar()) >= '0' && ch <= '9') res = res * 10 + (ch - '0'); 28 return flag ? -res : res; 29 } 30 // code... ... 31 int n,m,k; 32 int ok; 33 int color[MX*MX]; 34 int num[MX][MX]; 35 36 void dfs(int x,int y) 37 { 38 if (x>n) ok=1; 39 for (int i=1;i<=k;i++) 40 { 41 int remain = n*m-((x-1)*m+y-1)+1; 42 if (color[i]>remain/2) return; 43 } 44 for (int i=1;i<=k;i++) 45 { 46 if (color[i]>0&&num[x-1][y]!=i&&num[x][y-1]!=i) 47 { 48 num[x][y]=i; 49 color[i]--; 50 if (y==m) dfs(x+1,1); 51 else dfs(x,y+1); 52 color[i]++; 53 if (ok) return; 54 } 55 } 56 } 57 58 int main() 59 { 60 int T=read(); 61 for (int cnt=1;cnt<=T;cnt++) 62 { 63 n=read();m=read();k=read(); 64 for (int i=1;i<=k;i++) 65 color[i]=read(); 66 ok = 0; 67 memset(num,0,sizeof(num)); 68 dfs(1,1); 69 printf("Case #%d: ",cnt); 70 if (ok) 71 { 72 printf("YES "); 73 for (int i=1;i<=n;i++) 74 { 75 for (int j=1;j<m;j++) 76 printf("%d ",num[i][j]); 77 printf("%d ",num[i][m]); 78 } 79 } 80 else printf("NO "); 81 } 82 return 0; 83 }