HDU 5113 -- Black And White (search + pruning), hdu5113 -- black
Question Link
Problem DescriptionIn 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 shocould be 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 ≤ N x M ).
The second line contains K integers ci (ci> 0), denoting the number of cells where the I-th color shoshould be used.
It's guaranteed that c1 + c2 + · + cK = N × M.
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 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 Input41 5 24 13 3 41 2 2 42 3 32 2 23 2 32 2 2
Sample OutputCase #1: NOCase #2: YES4 3 42 1 24 3 4 Case #3: YES1 2 32 3 1 Case #4: YES1 22 33 1
There is an N * M square board. Now we need to apply color to each square above. K colors are available, c [1] times and c [2] times respectively for each color ...... C [K] times, c [1] + c [2] + ...... + C [K] = N * M
The color of each square should be different from that of the upper, lower, and lower squares. If YES can be output and one of them can be output, if NO, NO can be output;
Idea: brute-force search, but this will time out, you can add pruning in the search: the remaining number of squares res and the remaining number of colors must meet (res + 1) /2> = c [I]
Otherwise, the correct method is not available for further searching under the current situation;
The Code is as follows:
# Include <iostream> # include <algorithm> # include <cstdio> # include <cstring> using namespace std; int N, K, M; int c [30]; int mp [10] [10]; int check (int x, int y, int k) {int f = 1; if (mp [x-1] [y] = k) f = 0; if (mp [x] [Y-1] = k) f = 0; return f;} int cal (int x, int y) {if (x> N) return 1; int res = (N-x) * M + M-y + 2; // the remaining number of squares + 1; for (int I = 1; I <= K; I ++) if (res/2 <c [I]) return 0; // pruning, the number of remaining squares in a certain color> (number of remaining squares + 1)/2 is definitely incorrect; for (int I = 1; I <= K; I ++) {Int f = 0; if (c [I] & check (x, y, I) {mp [x] [y] = I; c [I] --; if (y = M) f = cal (x + 1, 1); else f = cal (x, y + 1 ); c [I] ++;} if (f) return 1;} return 0;} int main () {int T, Case = 1; cin> T; while (T --) {scanf ("% d", & N, & M, & K); for (int I = 1; I <= K; I ++) scanf ("% d", & c [I]); printf ("Case # % d: \ n", Case ++); if (! Cal (1, 1) {puts ("NO"); continue;} puts ("YES"); for (int I = 1; I <= N; I ++) for (int j = 1; j <= M; j ++) printf ("% d % c", mp [I] [j], (j = M )? '\ N': '');} return 0 ;}