[hdu5113]Black And White2014北京赛区现场赛B题(搜索加剪枝)

时间:2020-11-28 22:34:47

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud

Black And White

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
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.

 
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 .

 
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
 
题意:有一个n*m个地图,用k中颜色来进行填充,每种颜色可以使用的次数为ci次,∑ci=n*m,要求相邻的格子的颜色不能相同,问是否存在满足要求的染色方案,若存在,则输出其中一种。
分析:注意到n,m≤5,图较小,考虑用dfs来搞,但是光是dfs会T,所以需要加上一个剪枝。
若当前某种颜色的剩余数目大于剩余格子数目的一半,则必定不能完成填充方案,直接跳出。
 //gaoshenbaoyou  ------ pass system test
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
const int maxn=;
int a[maxn];
bool flag=;
int ans[][];
int n,m,k;
void dfs(int x,int y,int left)
{
if(!left)
{
flag=;
return;
}
for(int i=;i<=k;i++)
if(a[i]>(left+)/)return;
for(int i=;i<=k;i++)
{
if(!a[i])continue;
if(x&&ans[x-][y]==i)continue;
if(y&&ans[x][y-]==i)continue;
a[i]--;
ans[x][y]=i;
if(y<m-)dfs(x,y+,left-);
else dfs(x+,,left-);
if(flag)return;
a[i]++;
}
return;
}
int main()
{
//ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
int cas=;
while(t--)
{
flag=;
scanf("%d%d%d",&n,&m,&k);
int sum=;
int maxx=;
int tot=n*m;
for(int i=;i<=k;i++)
scanf("%d",&a[i]);
printf("Case #%d:\n",cas++);
dfs(,,tot);
if(flag)
{
printf("YES\n");
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(j)printf(" ");
printf("%d",ans[i][j]);
}
printf("\n");
}
}
else
printf("NO\n");
}
return ;
}

代码君