HDU 4012Paint on a Wall

22 篇文章 0 订阅

Paint on a Wall

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65768/65768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
Problem Description
Annie wants to paint her wall to an expected pattern. The wall can be represented as a 2*n grid and each grid will be painted only one color. Annie has a brush which could paint a rectangular area of the wall at a single step. The paint could be overlap and the newly painted color will replace the older one.
  For a given pattern of the wall, your task is to help Annie find out the minimum possible number of painting steps. You can assume that the wall remains unpainted until Annie paint some colors on it.
 

Input
There are multiple test cases in the input. The first line contains the number of test cases. For each test case, the first line contains the only integer n indicating the length of the wall. (1 <= n <= 8) Two lines follow, denoting the expected pattern of the wall. The color is represented by a single capital letter. See the sample input for further details.
 

Output
For each test case, output only one integer denoting the minimum number of steps.
 

Sample Input
  
  
3 3 ABA CBC 3 BAA CCB 3 BBB BAB
 

Sample Output
  
  
Case #1: 3 Case #2: 3 Case #3: 2
 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup
 

广搜题目,枚举起点,染色为对的颜色,然后向左至对的颜色,向右至对的颜色  然后上下两行一起

状态压缩   每行最多八个点  所以2^16可以将状态存储


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

int t,n,tt;
bool vis[2][8],tmp[2][8];
char ch[2][8];
bool flag[1<<16];//标记判重
struct node
{
    int flag;
    int t;
} st,ed;

void set_vis(node a)  //获得状态矩阵值
{
    memset(vis,false ,sizeof(vis));
    int i=0;
    while(i<n)
    {
        vis[0][i]=(a.flag&1);
        a.flag/=2;
        i++;
    }
    i=0;
    while(i<n)
    {
        vis[1][i]=(a.flag&1);
        a.flag/=2;
        i++;
    }
    return ;
}

int get_flag()  //获得状态压缩值
{
    int ans=0;
    for(int i=0; i<n; i++)
    {
        if(tmp[0][i])
            ans|=(1<<i);
        if(tmp[1][i])
            ans|=(1<<(i+n));
    }
    return ans;
}

int l,r;
void set_tmp(int x,int y)  //找到最大改变区间
{
    l=y,r=y;
    while(l>=0&&!vis[x][l])
        l--;
    while(r<n&&!vis[x][r])
        r++;
    for(int i=l+1; i<r; i++)
        tmp[x][i]=(ch[x][y]==ch[x][i]);
    return ;
}

void bfs()
{
    queue<node>q;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        set_vis(st);
        if(st.flag==(1<<(n*2))-1)  //全部染色对的情况
        {
            printf("Case #%d: %d\n",tt++,st.t);
            return ;
        }
        for(int i=0; i<2; i++)
        {
            for(int j=0; j<n; j++)
            {
                if(!vis[i][j])
                {
                    memcpy(tmp,vis,sizeof(vis));
                    set_tmp(i,j);
                    //枚举染色点
                    for(int k=l+1;k<r;k++)
                    {
                        tmp[i][k]=(ch[i][k]==ch[i][j]);
                        ed.flag=get_flag();
                        if(!flag[ed.flag])
                        {
                            ed.t=st.t+1;
                            flag[ed.flag]=true ;
                            q.push(ed);
                        }
                    }
                    memcpy(tmp,vis,sizeof(vis));
                    //两行
                    for(int k=l+1;k<r;k++)
                    {
                        tmp[i][k]=(ch[i][k]==ch[i][j]);
                        tmp[i^1][k]=(ch[i^1][k]==ch[i][j]);
                        ed.flag=get_flag();
                        if(!flag[ed.flag])
                        {
                            ed.t=st.t+1;
                            flag[ed.flag]=true ;
                            q.push(ed);
                        }
                    }
                }
            }
        }
    }
    return ;
}

int main()
{
    cin>>t;
    tt=1;
    while(t--)
    {
        cin>>n;
        cin>>ch[0]>>ch[1];
        memset(flag,0,sizeof(flag));
        st.flag=0;
        st.t=0;
        flag[0]=true ;
        bfs();
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值