(5113)HDU

본문 근거http://www.cnblogs.com/fraud이 블 로 그 는 참고 로 배 웠 습 니 다.지적 재산권 이 매우 중요 합 니 다.제 가 블 로 그 를 쓰 는 목적 은 자신 을 향상 시 키 는 것 입 니 다.
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 c
i 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 c
i (c
i > 0), denoting the number of cells where the i-th color should be used.
It’s guaranteed that c
1 + c
2 + · · · + c
K = 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

 
Source
2014 CM/ICPC 아시아 지역 베 이 징 역-재현전(북 사 와 상교 에 감 사 드 립 니 다)
이것 은 항 전의 문제 면 입 니 다.이 문 제 는 바로 이 도형 을 출력 할 수 있 느 냐 는 것 입 니 다.모두 1~k 개의 색깔 이 있 고 N*M 의 행렬 을 출력 하 는 것 입 니 다.이 행렬 의 규칙 은 모든 색깔 에 Ai 개가 있 고 다 쓴 전제 에서 인접 한 색깔 이 다 르 도록 하 는 것 입 니 다.거의 이 모양 이다
#include #include #include #include #include #include #include #define ll __int64 #define MAX 1000009 using namespace std; int k; int ans[109][109]; int a[109]; int n,m; int flag; void dfs(int x,int y,int xx) {     if(!xx)//빈 자리 가 없 으 면 되 돌려 줍 니 다    {         flag = 1;         return ;     }     for(int i = 1;i<=k;i++)//남 은 개수 가 빈자리 보다 많 으 면 종료(가위질)    {         if(a[i]>(xx+1)/2)             return ;     }     for(int i = 1;i<=k;i++)     {         if(!a[i])continue;//현재 색상 순환 건 너 뛰 기        if(x&&ans[x-1][y]==i)continue;//주위 에 현재 색상 이 있 으 면 건 너 뛰 기        if(y&&ans[x][y-1]==i)continue;//주위 에 현재 색상 이 있 으 면 건 너 뛰 기        a[i]--;//하 나 를 써 버리다        ans[x][y] = i;//현재 위치 할당 i 번 째 색상        if(y

좋은 웹페이지 즐겨찾기