转载请注明出处: 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
— 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.
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 .
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.
//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 ;
}
代码君