第十四届华中科技大学程序设计竞赛决赛同步赛(舞蹈链+A*搜索)

4 篇文章 0 订阅
1 篇文章 0 订阅
链接: https://www.nowcoder.com/acm/contest/119/J
来源:牛客网

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.
There is an N×M grid, some are black and some are white. You can choose some grids that are black initially, and change the color of these grids and their adjacent grids to white at the same time. You have to figure out the minimum grids you need to choose to change the color of all grids into white.

输入描述:

There are two integers N and M(N+M≤25) in the first line, as described above.
Then there is an N×M grid followed, indicating the color of each grid. ‘#’ denotes black grid and ‘.’denotes white grid.

输出描述:

Output the minimum grids you need to choose.

题解:
与数独问题类似,每个#建立一行,所在位置建为1,
若有#与之相邻,则在这一行相应的位置也置为1,即
转化为求精确覆盖问题。注意这里当去掉其中一行时
并不能把矛盾的几行 都去掉,而是把矛盾的行中
矛盾的元素去掉。然后A*搜索优化一下。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxnode=100100;
char t[33][33];
int a[33][33];
int d[5][2]={{-1,0},{0,-1},{0,0},{0,1},{1,0}};
struct node
{
    int n,m,ansd,len;
    int U[maxnode],D[maxnode],L[maxnode],R[maxnode];
    int col[maxnode],row[maxnode];
    int H[maxnode],S[maxnode];
    void init(int _n,int _m)
    {
        n=_n,m=_m;
        for(int i=0;i<=m;i++)
        {
            U[i]=D[i]=i;
            R[i]=i+1;
            L[i]=i-1;
            S[i]=0;
        }
        R[m]=0;L[0]=m;
        ansd=1e9;
        for(int i=1;i<=n;i++)
            H[i]=-1;
        len=m;
    }
    void Link(int r,int c)
    {
        D[++len]=D[c];
        U[D[c]]=len;
        U[len]=c;
        D[c]=len;
        row[len]=r;
        col[len]=c;
        S[c]++;
        if(H[r]<0)
            H[r]=L[len]=R[len]=len;
        else
        {
            R[len]=R[H[r]];
            L[R[H[r]]]=len;
            L[len]=H[r];
            R[H[r]]=len;
        }
    }
    void remove(int c)
    {
        for(int i=D[c];i!=c;i=D[i])
        {
            L[R[i]]=L[i];
            R[L[i]]=R[i];
        }
    }
    void resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i])
        {
            L[R[i]]=i;
            R[L[i]]=i;
        }
    }
    bool v[maxnode];
    int f()
    {
        for(int i=R[0];i!=0;i=R[i])
        {
            v[i]=1;
        }
        int ans=0;
        for(int i=R[0];i!=0;i=R[i])
        {
            if(v[i])
            {
                ans++;
                v[i]=0;
                for(int j=D[i];j!=i;j=D[j])
                {
                    for(int k=R[j];k!=j;k=R[k])
                        v[col[k]]=0;
                }
            }
        }
        return ans;
    }
    void Dance(int d)
    {
        if(d+f()>=ansd)return;
        if(R[0]==0)
        {
            ansd=min(ansd,d);
            return;
        }
        int c=R[0];
        for(int i=R[0];i!=0;i=R[i])
            if(S[i]<S[c])
               c=i;
        for(int i=D[c];i!=c;i=D[i])
        {
            remove(i);
            for(int j=R[i];j!=i;j=R[j])
                remove(j);
            Dance(d+1);
            for(int j=L[i];j!=i;j=L[j])
                resume(j);
            resume(i);
        }
    }
}g;
int main()
{
    int n,m;scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
        scanf("%s",&t[i]);
    int k=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(t[i][j]=='#')
                a[i][j]=++k;
        }
    }
    g.init(k,k);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            if(t[i][j]=='#')
            {
                for(k=0;k<5;k++)
                {
                    int x=i+d[k][0],y=j+d[k][1];
                    if(x>=0&&x<n&&y>=0&&y<m&&t[x][y]=='#')
                    {
                        g.Link(a[i][j],a[x][y]);
                    }
                }
            }
        }
    }
    g.Dance(0);
    printf("%d\n",g.ansd);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值