Black and White
Time limit:2000/2000 MS (java/others) Memory limit:512000/512000 K (java/others)
Total submission (s): 527 Accepted Submission (s): 145
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 Input41 5 24 13 3 41 2 2 42 3 32 2 23 2 32 2 2
Sample outputcase #1: nocase #2: YES4 3 1 3 4Case #3: YES1 2 3 1Case #4: YES1 22 33 1 • Learn the general idea of the code, basically the same. http://fudq.blog.163.com/blog/static/191350238201411271332692/has always been more afraid of Dfs, I can only say,,, practice bar .... The pruning of this problem is also drunk, the scene thought, Add the position of the wrong or not,,, do not know whether it is the topic of intentional, if it is too god. AC Code:
1#include <iostream>2#include <stdio.h>3#include <algorithm>4#include <cstring>5#include <string.h>6#include <math.h>7#include <queue>8#include <stack>9#include <stdlib.h>Ten#include <map> One using namespacestd; A #defineLL Long Long - #defineSF (a) scanf ("%d",& (a)); - the #defineN 21 - intR[n][n]; - intn,m,flag,k; - intf[10010]; + //staining: DFS, deep search + pruning - + A voidOutput () { atprintf"yes\n"); - for(intI=1; i<=n;i++){ - //if (i==0) printf ("%d", r[i][0]); - for(intj=1; j<=m;j++) - if(j==1) -printf"%d", r[i][j]+1); in Elseprintf"%d", r[i][j]+1); -printf"\ n"); to } + } - intJudintXintYinti) { the if(r[x-1][y] = = i)return 0; * if(r[x][y-1] = = i)return 0;//it means no! $ return 1;Panax Notoginseng } - voidDfsintXintYintCNT) { the if(flag)return ; + if(CNT = =0){ Aflag=1; the Output (); + return ; - } $ //Plus pruning . $ for(intI=0; i<k;i++){ - if(F[i] > (cnt+1)/2)return;//return directly, this is not successful! - } the for(intI=0; i<k;i++){ - if(F[i] &&Jud (X,y,i)) {WuyiR[x][y] =i; thef[i]--; - if(y==m) DFS (x+1,1, cnt-1); Wu ElseDFS (x,y+1, cnt-1); -r[x][y]=-1; Aboutf[i]++; $ } - } - - } A intMain () { + intt,cas=1; thescanf"%d",&T); - while(t--){ $printf"Case #%d:\n", cas++); thescanf" %d%d%d",&n,&m,&k); theflag=0; the for(intI=0; i<k;i++) scanf ("%d",&f[i]); thememset (r,-1,sizeof(R)); -Dfs1,1Nm); in if(!flag) printf ("no\n"); the } the About return 0; the}
Black and White (Dfs + pruning)