hdu 4770 Lights Against Dudely(暴力枚举dfs)

46 篇文章 0 订阅
15 篇文章 0 订阅

Lights Against Dudely

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

Source
2013 Asia Hangzhou Regional Contest

思路:虚弱的房间数最多只有15个,所以可以先对特殊的灯放哪个房间进行枚举,然后再枚举4个方向,最后直接dfs对所有虚弱的房间放灯即可,复杂度O(15*4*2^14)

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

#define maxn 202
const int inf=0x3f3f3f3f;
struct node
{
    int x,y;
} q[20];
int dir[][3][2]=//特殊的灯的四种放法
{
    {{0,0},{-1,0},{0,1}},
    {{0,0},{1,0},{0,1}},
    {{0,0},{1,0},{0,-1}},
    {{0,0},{-1,0},{0,-1}}
};
char mp[maxn][maxn];
bool inq[20];
int vis[maxn][maxn];
int n,m,num,ans;

void dfs(int step,int tot)
{
    if(tot>=ans)
        return ;
    if(step==num)
    {
        ans=min(ans,tot);
        return ;
    }
    for(int i=0; i<num; ++i)
    {
        if(!inq[i])
        {
            int cnt=0,xx,yy,flag=0;
            for(int j=0; j<3; ++j)
            {
                xx=q[i].x+dir[0][j][0];
                yy=q[i].y+dir[0][j][1];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                {
                    if(mp[xx][yy]=='#')
                    {
                        flag=1;
                        break;
                    }
                    else if(mp[xx][yy]=='.')
                    {
                        if(!vis[xx][yy])
                            ++cnt;
                        ++vis[xx][yy];
                    }
                }
            }
            if(!flag)
            {
                inq[i]=true;
                dfs(step+cnt,tot+1);
                inq[i]=false;
            }
            for(int j=0; j<3; ++j)
            {
                xx=q[i].x+dir[0][j][0];
                yy=q[i].y+dir[0][j][1];
                if(xx>=0&&xx<n&&yy>=0&&yy<m)
                {
                    if(mp[xx][yy]=='#')
                        break;
                    else if(mp[xx][yy]=='.'&&vis[xx][yy])
                        --vis[xx][yy];
                }
            }
        }
    }
}

int main()
{
    while(~scanf("%d%d",&n,&m),n||m)
    {
        memset(vis,0,sizeof(vis));
        memset(inq,false,sizeof(inq));
        num=0;
        for(int i=0; i<n; ++i)
        {
            scanf("%s",mp[i]);
            for(int j=0; j<m; ++j)
            {
                if(mp[i][j]=='.')
                    q[num].x=i,q[num++].y=j;
            }
        }
        if(!num)
        {
            printf("0\n");
            continue;
        }
        ans=inf;
        for(int i=0; i<num; ++i)//枚举特殊的灯放哪
        {
            for(int j=0; j<4; ++j)//枚举放的方向
            {
                int cnt=0,xx,yy,flag=0;
                for(int l=0; l<3; ++l)
                {
                    xx=q[i].x+dir[j][l][0];
                    yy=q[i].y+dir[j][l][1];
                    if(xx>=0&&xx<n&&yy>=0&&yy<m)
                    {
                        if(mp[xx][yy]=='#')
                        {
                            flag=1;
                            break;
                        }
                        else if(mp[xx][yy]=='.')
                        {
                            if(!vis[xx][yy])
                                ++cnt;
                            ++vis[xx][yy];
                        }
                    }
                }
                if(!flag)
                {
                    inq[i]=true;
                    dfs(cnt,1);
                    inq[i]=false;
                }
                for(int l=0; l<3; ++l)
                {
                    xx=q[i].x+dir[j][l][0];
                    yy=q[i].y+dir[j][l][1];
                    if(xx>=0&&xx<n&&yy>=0&&yy<m)
                    {
                        if(mp[xx][yy]=='#')
                            break;
                        else if(mp[xx][yy]=='.'&&vis[xx][yy])
                            --vis[xx][yy];
                    }
                }
            }
        }
        if(ans==inf)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}



总结:数据小,所以暴力啊!(可是就是没想到。。。)

dfs时要注意,一定要少写循环!一定要少写if!
我的代码跑了500ms+,然而别人的轻轻松松30ms+。。。(只在判断是否照亮所有房间这个地方用的方法不一样)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值