Hdu3498-whosyourdaddy(精确覆盖模板题)

本文介绍了一个基于Warcraft游戏背景的精确覆盖问题求解方法,通过使用DLX算法优化来找出最少攻击次数消灭所有敌方单位的方案。
Problem Description
sevenzero liked Warcraft very much, but he haven't practiced it for several years after being addicted to algorithms. Now, though he is playing with computer, he nearly losed and only his hero Pit Lord left. sevenzero is angry, he decided to cheat to turn defeat into victory. So he entered "whosyourdaddy", that let Pit Lord kill any hostile unit he damages immediately. As all Warcrafters know, Pit Lord masters a skill called Cleaving Attack and he can damage neighbour units of the unit he attacks. Pit Lord can choice a position to attack to avoid killing partial neighbour units sevenzero don't want to kill. Because sevenzero wants to win as soon as possible, he needs to know the minimum attack times to eliminate all the enemys.
 

 

Input
There are several cases. For each case, first line contains two integer N (2 ≤ N ≤ 55) and M (0 ≤ M ≤ N*N),and N is the number of hostile units. Hostile units are numbered from 1 to N. For the subsequent M lines, each line contains two integers A and B, that means A and B are neighbor. Each unit has no more than 4 neighbor units. The input is terminated by EOF.
 

 

Output
One line shows the minimum attack times for each case.
 

 

Sample Input
5 4
1 2
1 3
2 4
4 5
6 4
1 2
1 3
1 4
4 5
 

 

Sample Output
2
3

解析:裸的精确覆盖问题,不过要加优化,不过也是模板。

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int INF=1e9+7;
const int ms=60;
const int maxn=ms*ms;
int N,M,ans;
struct DLX
{
    int n,id;
    int L[maxn],R[maxn],U[maxn],D[maxn];
    int C[maxn],S[maxn],loc[maxn][2];
    void init(int nn=0) //传列长
    {
        n=nn;
        for(int i=0;i<=n;i++) U[i]=D[i]=i,L[i]=i-1,R[i]=i+1;
        L[0]=n; R[n]=0;
        id=n;
        memset(S,0,sizeof(S));
    }
    void AddRow(int x,int col,int A[]) //传入参数是行标号,列长,列数组
    {
        bool has=false;
        int first=id+1;
        for(int y=1;y<=col;y++)
        {
            if(A[y]==0) continue;
            has=true;
            ++id;
            L[id]=id-1; R[id]=id+1;
            D[id]=y; U[id]=U[y];
            D[U[y]]=id; U[y]=id;
            loc[id][0]=x,loc[id][1]=y;
            C[id]=y; S[y]++;
        }
        if(!has) return;
        R[id]=first; L[first]=id;
    }
    void Remove(int Size)
    {
        for(int j=D[Size];j!=Size;j=D[j])//将左右两边连接
            L[R[j]]=L[j],R[L[j]]=R[j];
    }
    void Resume(int Size)
    {
        for(int j=U[Size];j!=Size;j=U[j])//恢复
            L[R[j]]=R[L[j]]=j;
    }
    bool vis[ms];//标记行是否访问过
    int h() //启发式函数
    {
        int ret=0;
        int i,j,k;
        memset(vis,0,sizeof(vis));
        for(i=R[0];i;i=R[i])
        {
           if(vis[i]) continue;
           ret++;
           for(j=D[i];j!=i;j=D[j]) //所有关联的标记了
               for(k=R[j];k!=j;k=R[k]) vis[C[k]]=1;
        }
        return ret;
    }
    void dfs(int step)
    {
        if(step+h()>=ans) return;
        if(R[0]==0){ ans=min(ans,step); return; }
        int Min=INF,c=-1;
        for(int i=R[0];i;i=R[i]) if(Min>S[i]){ Min=S[i]; 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);
            dfs(step+1);
            for(int j=L[i];j!=i;j=L[j]) Resume(j);
            Resume(i);
        }
        return;
    }
}dlx;
int Mart[ms][ms];
int main()
{
    while(scanf("%d%d",&N,&M)!=EOF)
    {
        dlx.init(N);
        ans=55;
        memset(Mart,0,sizeof(Mart));
        for(int i=1;i<=N;i++) Mart[i][i]=1;
        while(M--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            Mart[x][y]=Mart[y][x]=1;
        }
        for(int i=1;i<=N;i++) dlx.AddRow(i,N,Mart[i]);
        dlx.dfs(0);
        printf("%d\n",ans);
    }
    return 0;
}
View Code

 

代码

 

转载于:https://www.cnblogs.com/wust-ouyangli/p/5747090.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值