2014ACM/ICPC亚洲区北京站-重现赛 [B.Black And White] 涂色DFS

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5113

题目大意:在一个N * M的棋盘里涂色,要求i颜色要涂ci次。ci求和为N * M.

关键思想:从第一个点开始着色,一行一行涂,注意一旦找到答案后面就不必搜,当剩下个数为n时若有种颜色>n/2上取整就不必搜了(!)。

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std;

#define MAXN 1005
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define LL long long
#define PII pair
#define MP make_pair
#define PB push_back

int MAP[7][7];
int c[30];//i种颜色用c[i]次
bool have=false;

int T,N,M,K;

void print(){
    printf("YES\n");
    for(int i=1;i<=N;i++){
        for(int j=1;j<=M;j++){
            if(j==1)printf("%d",MAP[i][j]);
            else printf(" %d",MAP[i][j]);
        }
        printf("\n");
    }
    return ;
}

void DFS(int x,int y,int tot){
    for(int i=1;i<=K;i++){if(c[i]>(tot+1)/2)return ;}//最坏情况(剩一块矩形)的剪枝,很关键
    for(int i=1;i<=K;i++){
        if(have)return;
        if(MAP[x-1][y]!=i&&MAP[x][y-1]!=i&&c[i]!=0){//i种颜色有效
            MAP[x][y]=i,c[i]--;
            if(x==N&&y==M){
                   have=true;
                print();
                return;
            }
            if(y+1<=M)DFS(x,y+1,tot-1);//未到行尾涂本行
            else DFS(x+1,1,tot-1);//到行尾涂下一行
            MAP[x][y]=-1,c[i]++;//恢复
        }
    }
    return ;
}

int main(){
    scanf("%d",&T);
    for(int Case=1;Case<=T;Case++){
        memset(MAP,-1,sizeof(MAP));//其实按照DFS顺序无需重置
        scanf("%d%d%d",&N,&M,&K);
        bool flag=true;
        for(int i=1;i<=K;i++){
            scanf("%d",&c[i]);
            if(c[i]>(N*M+1)/2)flag=false;//黑白格都无法满足
        }
        printf("Case #%d:\n",Case);
        if(!flag){
            printf("NO\n");
            continue;
        }
        have=false;
        DFS(1,1,N*M);
    }
    return 0;
}
时间: 2024-12-16 20:37:30

2014ACM/ICPC亚洲区北京站-重现赛 [B.Black And White] 涂色DFS的相关文章

HDU 5112 A Curious Matt (2014ACM/ICPC亚洲区北京站-重现赛)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)Total Submission(s): 3058    Accepted Submission(s): 1716 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is w

K.Bro Sorting(杭电5122)(2014ACM/ICPC亚洲区北京站)

K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 67    Accepted Submission(s): 39 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubbl

2014ACM/ICPC亚洲区北京站题解

本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主要的考点. HDU 5113 Black And White HDU 5114 Collision HDU 5116 Everlasting L HDU 5117 Fluorescent HDU 5118 GRE Words Once More!

2014ACM/ICPC亚洲区北京站-A-(Curious Matt)

A Curious Matt                                   Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on the non-neg

2015ACM/ICPC亚洲区长春站-重现赛 1006 Almost Sorted Array

Problem Description: We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array. We say an array

A Curious Matt(2014ACM/ICPC亚洲区北京站)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wander

Bro Sorting(2014ACM/ICPC亚洲区北京站-K)

Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble so

A Curious Matt(2014ACM/ICPC亚洲区北京站-A)

A Curious Matt Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wander

2014ACM/ICPC亚洲区北京站-A Curious Matt

A Curious Matt Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0 Problem Description There is a curious man called Matt. One day, Matt's best friend Ted is wandering on