HDU_4770 Lights Against Dudely 状压+剪枝

本文探讨了一个关于哈利波特世界中Gringotts巫师银行的算法问题。该问题要求使用最少数量的魔法灯来照亮所有易损房间,同时避免照亮不可摧毁的房间。通过状态压缩和剪枝策略实现解决方案。

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

Lights Against Dudely

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2021    Accepted Submission(s): 595


Problem Description
Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money." 
Hagrid: "Well there's your money, Harry! Gringotts, the wizard bank! Ain't no safer place. Not one. Except perhaps Hogwarts." 
— Rubeus Hagrid to Harry Potter. 
  Gringotts Wizarding Bank is the only bank of the wizarding world, and is owned and operated by goblins. It was created by a goblin called Gringott. Its main offices are located in the North Side of Diagon Alley in London, England. In addition to storing money and valuables for wizards and witches, one can go there to exchange Muggle money for wizarding money. The currency exchanged by Muggles is later returned to circulation in the Muggle world by goblins. According to Rubeus Hagrid, other than Hogwarts School of Witchcraft and Wizardry, Gringotts is the safest place in the wizarding world.
  The text above is quoted from Harry Potter Wiki. But now Gringotts Wizarding Bank is not safe anymore. The stupid Dudley, Harry Potter's cousin, just robbed the bank. Of course, uncle Vernon, the drill seller, is behind the curtain because he has the most advanced drills in the world. Dudley drove an invisible and soundless drilling machine into the bank, and stole all Harry Potter's wizarding money and Muggle money. Dumbledore couldn't stand with it. He ordered to put some magic lights in the bank rooms to detect Dudley's drilling machine. The bank can be considered as a N × M grid consisting of N × M rooms. Each room has a coordinate. The coordinates of the upper-left room is (1,1) , the down-right room is (N,M) and the room below the upper-left room is (2,1)..... A 3×4 bank grid is shown below:

  Some rooms are indestructible and some rooms are vulnerable. Dudely's machine can only pass the vulnerable rooms. So lights must be put to light up all vulnerable rooms. There are at most fifteen vulnerable rooms in the bank. You can at most put one light in one room. The light of the lights can penetrate the walls. If you put a light in room (x,y), it lights up three rooms: room (x,y), room (x-1,y) and room (x,y+1). Dumbledore has only one special light whose lighting direction can be turned by 0 degree,90 degrees, 180 degrees or 270 degrees. For example, if the special light is put in room (x,y) and its lighting direction is turned by 90 degrees, it will light up room (x,y), room (x,y+1 ) and room (x+1,y). Now please help Dumbledore to figure out at least how many lights he has to use to light up all vulnerable rooms.
  Please pay attention that you can't light up any indestructible rooms, because the goblins there hate light. 

 

Input
  There are several test cases.
  In each test case:
  The first line are two integers N and M, meaning that the bank is a N × M grid(0<N,M <= 200).
  Then a N×M matrix follows. Each element is a letter standing for a room. '#' means a indestructible room, and '.' means a vulnerable room. 
  The input ends with N = 0 and M = 0
 

Output
  For each test case, print the minimum number of lights which Dumbledore needs to put.
  If there are no vulnerable rooms, print 0.
  If Dumbledore has no way to light up all vulnerable rooms, print -1.
 

Sample Input
2 2 ## ## 2 3 #.. ..# 3 3 ### #.# ### 0 0
 

Sample Output
0 2 -1
 
此题的关键在于点最多有15个,很容易想到使用状压。枚举装填与否的状态额,再枚举哪一个使用特殊的L,再枚举特殊L的姿态。但是,丧心病狂的出题人就会让你这么容易的过题吗!?于是漫长的剪枝就开始了:
1.首先预处理出每个点是否可以放一个普通的L,并用一个二进制状态can存下。每次先将特殊的姿态所在的位置加入can,这样每枚举出的状态i,如果i|can>can,说明这个i是没有意义的。
2.发现如果一个点的周围都是井号,那么就没有必要再找下去了,这个可以剪掉。
3.很给力的剪枝是先将二进制按含1的个数排序,再去寻找。
详见代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cstdio>
#define MAX_N 205
#define MAX_M 205
#define MAX_L 20
using namespace std;

char grid[MAX_N][MAX_M];
bool used[MAX_N][MAX_M];
int N,M;
int tot=0,x[MAX_L],y[MAX_L];
int dx[4]={1,1,-1,-1},dy[4]={1,-1,1,-1};
int ddx[4]={0,1,0,-1},ddy[4]={1,0,-1,0};
int can=0;

struct node
{
    int one,value;
};

node two[16][1<<15];

bool cmp(node a,node b)
{
    return a.one<b.one;
}

int main()
{
    for(int i=0;i<16;i++)
    {
        for(int j=0;j<(1<<i);j++)
        {
            two[i][j].one=0;
            for(int k=0;k<i;k++)
                if((1<<k)&j)two[i][j].one++;
            two[i][j].value=j;
        }
        sort(two[i],two[i]+(1<<i),cmp);
    }
    /*for(int i=0;i<5;i++,cout<<"------"<<endl)
        for(int j=0;j<(1<<i);j++)
        cout<<two[i][j].value<<" "<<two[i][j].one<<endl;*/
    while(~scanf("%d%d",&N,&M))
    {
        int ans=100;
        can=0;
        if(N==0&&M==0)return 0;
        memset(used,0,sizeof(used));
        memset(x,0,sizeof(x));
        memset(y,0,sizeof(y));
        tot=0;
        for(int i=0;i<N;i++)
            for(int j=0;j<M;j++)
            {
                char c;
                scanf(" %c",&c);
                grid[i][j]=c;
                if(c=='.')
                    x[tot]=i,y[tot++]=j;
            }
        if(tot==0){printf("0\n");continue;}
        bool fl=true;
        for(int i=0;i<tot;i++)
        {
            int nearNum=0,daNum=0;
            for(int j=0;j<4;j++)
            {
                int nnx=ddx[j]+x[i],nny=ddy[j]+y[i];
                if(nnx>=0&&nnx<N&&nny>=0&&nny<M)
                {
                    nearNum++;
                    daNum+=grid[nnx][nny]=='#';
                }
            }
            //cout<<nearNum<<" "<<daNum<<endl;
            if((nearNum==daNum)&&(nearNum>2)){printf("-1\n");fl=0;break;}

            int nx=x[i]+dx[2],ny=y[i]+dy[2];
            if((nx>=0&&nx<N&&grid[nx][y[i]]=='#')||(ny>=0&&ny<M&&grid[x[i]][ny]=='#'))can&=~(1<<i);
            else
                can|=(1<<i);
        }

        if(!fl)continue;
        //cout<<can<<endl;
        bool ffl=1;
        for(int q=0;q<(1<<tot)&&ffl;q++)
            for(int j=0;j<tot&&ffl;j++)
            {
                int i=two[tot][q].value;
                int tmpCan=can|(1<<j);
                if(!((1<<j)&i))continue;
                if((tmpCan|i)>tmpCan)continue;
                for(int k=0;k<4&&ffl;k++)
                {
                    memset(used,0,sizeof(used));
                    int tmp=0,tmpAns=0;
                    bool flag=true;
                    for(int t=0;t<tot&&ffl;t++)
                    {
                        if(!((1<<t)&i))continue;
                        int nx=x[t]+(t==j?dx[k]:dx[2]),ny=y[t]+(t==j?dy[k]:dy[2]);
                        if(nx>=0&&nx<N&&grid[nx][y[t]]=='#'){flag=false;break;}
                        if(ny>=0&&ny<M&&grid[x[t]][ny]=='#'){flag=false;break;} 
                        if(nx>=0&&nx<N){tmp+=!used[nx][y[t]];used[nx][y[t]]=1;}
                        if(ny>=0&&ny<M){tmp+=!used[x[t]][ny];used[x[t]][ny]=1;}
                        tmp+=!used[x[t]][y[t]];used[x[t]][y[t]]=1;
                    }
                    if(tmp==tot&&flag){ans=two[tot][q].one;ffl=0;break;}
                }
            }
        //cout<<ans<<endl;
        if(ans==100)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}



内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值