Black and WhiteTime
limit:2000/2000 MS (java/others) Memory limit:512000/512000 K (java/others)
Total submission (s): 854 Accepted Submission (s): 218
Special Judge
Problem DescriptionIn Mathematics, the four-color theorem, or the four-color map theorem, states, given any separatio N of a plane into contiguous regions, producing a figure called a map, no more than four colors is required to color the Regions of the map so, no, adjacent regions have the same color.
-wikipedia, the free encyclopedia
In this problem, you has to solve the 4-color problem. Hey, I ' m just joking.
You is asked to solve a similar problem:
The color an NXM chessboard with K colors numbered from 1 to K such, that no, and adjacent cells has the same color (both cell S is adjacent if they share an edge). The i-th color should is used in exactly CI cells.
Matt hopes you can tell him a possible coloring.
Inputthe 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≤NXM).
The second line contains K integers ci (ci > 0), denoting the number of cells where the i-th color should is used.
It ' s guaranteed that C1 + C2 + + CK = Nxm.
Outputfor 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 the one line. Each of the following N lines contains M numbers seperated by single whitespace, denoting the color of the cells.
If There is multiple solutions, output any of them.
Sample Input
41 5 24 13 3 41 2 2 42 3 32 2 23 2 32 2 2
Sample Output
Case #1: nocase #2: YES4 3 1 3 4Case #3: YES1 2 3 1Case #4: YES1 22 33 1
Pruning method: If the remaining number of spaces is res, for each color of the number of T, to meet (res+1) > t.
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int Maxn=15;int ans[ Maxn][maxn],a[maxn],n,m,k,flag,t,xx,yy;void input () {flag=0; memset (ans,0,sizeof (ans)); scanf ("%d%d%d", &n,&m,&k); for (int i=1; i<=k; i++) scanf ("%d", &a[i]);} void Dfs (int x,int y,int res) {if (res==0) flag=1; if (flag) return; for (int i=1;i<=k;i++) if ((res+1)/2<a[i]) return; for (int i=1, i<=k; i++) {if (A[i] && ans[x-1][y]!=i && ans[x][y-1]!=i) {an s[x][y]=i,a[i]--; if (y+1>m) Xx=x+1,yy=1; else xx=x,yy=y+1; DFS (XX,YY,RES-1); if (flag) return; ans[x][y]=0,a[i]++; }}}void solve (int co) {printf ("Case #%d:\n", CO); DFS (1,1,N*M); if (!flag) printf ("no\n"); else {printf ("yes\n"); for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) { if (j==1) printf ("%d", ans[i][j]); else printf ("%d", ans[i][j]); } printf ("\ n"); }}}int Main () {int T; scanf ("%d", &t); for (int co=1; co<=t; co++) {input (); Solve (CO); } return 0;}
HDU 5113 Black and White (Dfs backtracking + pruning)