hdu 4770 Lights Against Dudely(状态压缩dfs)

Lights Against Dudely

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


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
 

Recommend
We have carefully selected several similar problems for you:   6119  6118  6117  6116  6115 
 

Statistic |  Submit |  Discuss |  Note

题目大意:

给你一个N*M的地图,其中点表示需要光照到的地方,保证这样的地方最多有15个。

我们可以在点的坐标上建立一个灯(灯当然可以照射这一个格子),一个普通的灯能够照到的地方是其上边和右边的点的格子,不能去照射#的格子,我们还有一个特殊的灯,有且只有一个,当然也可以不去使用,其可以旋转,就是按照普通的灯一样的照射效果,但是我们可以将其旋转90/180/270度去建立。问最少需要使用多少个这样的灯去照射到所有的点。注意,一个格子只允许放一个灯。


思路:


1、既然一共最多只有15个点,那么我们将所有的点坐标处理到一个数组中pos【i】【2】,其中pos【i】【0】表示第i个点坐标的x,pos【i】【1】表示第i个点坐标的y。


2、然后我们暴力枚举2^15种情况,每一个点都枚举建立一个灯,或者是不建立一个灯,然后在这些建立的灯里边再考虑两种情况,一种是不建立特殊灯,或者是建立特殊灯,如果建立特殊灯,我们在从这最多15个点中枚举出来一个灯作为特殊灯,当然同时要给其设定一个方向,这里当然也是枚举处理,一共三种情况,90度/180度/270度。


3、然后我们在处理当前情况一共照射了多少个灯的时候,因为一共最多只有15个点需要被照亮,那么我们一定注意不要memset(200*200的一个数组),这样会超时,那么我们对应设定一个数组mp【i】【j】表示点(i,j)是否被照亮,那么我们初始化的时候,只需要初始化最多15个坐标即可,然后O(n)暴力将灯的照射模拟一下,判断能否将所有的点的格子都照上即可,如果可以,当前就是一个可行解,过程中维护最优解即可。


#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <string.h>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <list>
#include <bitset>
#include <stack>
#include <stdlib.h>
#define lowbit(x) (x&-x)
#define e exp(1.0)
const int mod=1e9+7;
const int inf=0x3f3f3f;
//ios::sync_with_stdio(false);
//    auto start = clock();
//    cout << (clock() - start) / (double)CLOCKS_PER_SEC;
typedef long long ll;
typedef long long LL;
using namespace std;
const int maxn=225;
char s[maxn][maxn];
int n,m;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
int vis[maxn][maxn];
int point;//number of point
struct node
{
    int x;
    int y;
}p[16];
bool change(int x,int y,int k,int t)
{
    int dxx[]={-1,1,1,-1};
    int dyy[]={1,-1,1,-1};
    if(s[x][y]!='.')
        return 0;
    if(x+dxx[k]>=0 and x+dxx[k]<n and s[x+dxx[k]][y]!='.')
        return 0;
    if(y+dyy[k]>=0 and y+dyy[k]<m and s[x][y+dyy[k]]!='.')
        return 0;
    vis[x][y]+=t;
    if(x+dxx[k]>=0)
        vis[x+dxx[k]][y]+=t;
    if(y+dyy[k]>=0)
        vis[x][y+dyy[k]]+=t;
    return 1;
}
int dfs(int x,int ans,int k)
{
    int minn=inf;
    if(x==point)
    {
        for(int i=0;i<point;i++)
            if(vis[p[i].x][p[i].y]==0)
                return inf;
        return ans;
    }
    minn=min(minn,dfs(x+1,ans,k));
    if(change(p[x].x,p[x].y,0,1))
    {
        minn=min(minn,dfs(x+1,ans+1,k));
        change(p[x].x,p[x].y,0,-1);
    }
    if(k)
    {
        for(int i=1;i<4;i++)
            if(change(p[x].x,p[x].y,i,1))
            {
                minn=min(minn,dfs(x+1,ans+1,0));
                change(p[x].x,p[x].y,i,-1);
            }
    }
    return minn;
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n>>m && n && m)
    {
        point=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            {
                cin>>s[i][j];
                if(s[i][j]=='.')
                {
                    p[point].x=i;
                    p[point++].y=j;
                }
            }
        memset(vis,0,sizeof(vis));
        int ans=dfs(0,0,1);
        ans<=17?cout<<ans<<endl:cout<<"-1"<<endl;
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值