您的位置:首页 > 其它

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

2014-12-01 17:14 375 查看
转载请注明出处: 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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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 .

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

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=30;
int a[maxn];
bool flag=0;
int ans[10][10];
int n,m,k;
void dfs(int x,int y,int left)
{
if(!left)
{
flag=1;
return;
}
for(int i=1;i<=k;i++)
if(a[i]>(left+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;
if(y<m-1)dfs(x,y+1,left-1);
else dfs(x+1,0,left-1);
if(flag)return;
a[i]++;
}
return;
}
int main()
{
//ios::sync_with_stdio(false);
int t;
scanf("%d",&t);
int cas=1;
while(t--)
{
flag=0;
scanf("%d%d%d",&n,&m,&k);
int sum=0;
int maxx=0;
int tot=n*m;
for(int i=1;i<=k;i++)
scanf("%d",&a[i]);
printf("Case #%d:\n",cas++);
dfs(0,0,tot);
if(flag)
{
printf("YES\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(j)printf(" ");
printf("%d",ans[i][j]);
}
printf("\n");
}
}
else
printf("NO\n");
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: