状压+暴力搜索

Lights Against Dudely

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

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
 
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; int dir_x[]={-,,,},dir_y[]={,,,-}; int n,m,cnt=,ans;
char G[][];
bool den[][]; bool inside(int x,int y)
{
return (x>=&&x<n)&&(y>=&&y<m);
} void SHOWden()
{
for(int i=;i<n;i++,putchar())
for(int j=;j<m;j++)
cout<<den[i][j]<<" ";
cout<<" ... \n";
} void SHOWST(int x)
{
for(int i=;i<cnt;i++)
{
if(x&(<<i)) cout<<"1 ";
else cout<<"0 ";
}
cout<<endl;
} struct LP
{
int x,y;
}lp[]; void dfs(int state,int lan,int n)
{
//SHOWST(state);
//cout<<lan<<" ...... "<<n<<endl;
if(n>=ans) return ;
if(lan==cnt)
{
ans=n; return ;
}
for(int i=;i<cnt;i++)
{
if(state&(<<i)) continue;
int lan0=,lan1=,lan2=;
if(den[lp[i].x][lp[i].y]==)
{
lan0++; den[lp[i].x][lp[i].y]=;
}
int x1=lp[i].x+dir_x[],x2=lp[i].x+dir_x[];
int y1=lp[i].y+dir_y[],y2=lp[i].y+dir_y[];
if(!inside(x1,y1)||(inside(x1,y1)&&G[x1][y1]=='.'))
{
if(!inside(x2,y2)||(inside(x2,y2)&&G[x2][y2]=='.'))
{
if(inside(x1,y1)&&den[x1][y1]==)
{
lan1++; den[x1][y1]=;
}
if(inside(x2,y2)&&den[x2][y2]==)
{
lan2++; den[x2][y2]=;
}
dfs(state|(<<i),lan+lan2+lan0+lan1,n+);
}
}
if(lan0) den[lp[i].x][lp[i].y]=;
if(lan1) den[x1][y1]=;
if(lan2) den[x2][y2]=;
}
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==&&m==) break;
getchar();
memset(G,' ',sizeof(G));
cnt=;
for(int i=;i<n;i++)
{
scanf("%s",G[i]);
}
/*
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<" "<<G[i][j];
cout<<endl;
}
cout<<endl;
*/
memset(lp,,sizeof(lp));
memset(den,,sizeof(den));
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(G[i][j]=='.')
{
lp[cnt].x=i;lp[cnt].y=j;
cnt++;
}
}
}
if(cnt==)
{
printf("0\n"); continue;
}
ans=0x3f3f3f3f;
for(int i=;i<cnt;i++)
{
den[lp[i].x][lp[i].y]=;
//cout<<lp[i].x<<" ..... "<<lp[i].y<<endl;
for(int j=;j<;j++)
{
int x1=lp[i].x+dir_x[j%],x2=lp[i].x+dir_x[(j+)%];
int y1=lp[i].y+dir_y[j%],y2=lp[i].y+dir_y[(j+)%];
if((inside(x1,y1)&&G[x1][y1]=='.')||!inside(x1,y1))
{
if((inside(x2,y2)&&G[x2][y2]=='.')||!inside(x2,y2))
{
int lan=;
if(inside(x1,y1)) { lan++; den[x1][y1]=; }
if(inside(x2,y2)) { lan++; den[x2][y2]=; }
//cout<<inside(x1,y1)<<" "<<x1<<","<<y1<<endl;
//cout<<inside(x2,y2)<<" "<<x2<<","<<y2<<endl;
//cout<<" .. \n";
//SHOWden();
dfs((<<i),lan,);
//SHOWden();
if(inside(x1,y1)) den[x1][y1]=;
if(inside(x2,y2)) den[x2][y2]=;
}
}
}
den[lp[i].x][lp[i].y]=;
}
if(ans==0x3f3f3f3f) puts("-1");
else printf("%d\n",ans);
}
return ;
}

HDOJ 4770 Lights Against Dudely的更多相关文章

  1. 状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely

    题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析: ...

  2. hdu 4770 Lights Against Dudely(回溯)

    pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...

  3. HDU 4770 Lights Against Dudely 暴力枚举+dfs

    又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...

  4. HDU 4770 Lights Against Dudely

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  5. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  6. HDU 4770 Lights Against Dudely(暴力+状压)

    思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...

  7. HDU_4770 Lights Against Dudely 状压+剪枝

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 Lights Against Dudely Time Limit: 2000/1000 MS ( ...

  8. HDU 4770 Lights Against DudelyLights

    Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  9. hdu4770:Lights Against Dudely(回溯 + 修剪)

    称号:hdu4770:Lights Against Dudely 题目大意:相同是n*m的矩阵代表room,房间相同也有脆弱和牢固之分,如今要求要保护脆弱的房间.须要将每一个脆弱的房间都照亮,可是牢固 ...

随机推荐

  1. AC日记——楼房 codevs 2995

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 地平线(x轴)上有n个矩(lou ...

  2. ASP.NET之纠错

    甘特图是:计划项目的时间安排和资源平衡 IoC容器会自动判断javabean和某些数据类型是否建立起依赖关系(装配是否成功),主要用在自动装配中. 通过设置属性<bean id="&q ...

  3. salt源码安装软件和yum安装软件

    上面简单列出了源码安装的sls文件书写思路. 涉及到一些固定的思路:如, 1,拷贝 解压安装时候需要依赖tar.gz存在 如果已安装则无需再次安装. 2,启动脚本 加入chk时候需要文件存在,如果已添 ...

  4. [LeetCode] Range Sum Query - Mutable 区域和检索 - 可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  5. [LeetCode] Invert Binary Tree 翻转二叉树

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...

  6. EF查询之性能优化技巧

    上一篇:EF使用CodeFirst方式生成数据库&技巧经验 前言 EF相信大部分同学都已经经常使用了,可是你的查询高效吗? 今天我就以个人使用经验来讲讲在使用EF做查询的时候大家都容易忽略的性 ...

  7. 更新过程 renewal process

    一类随机过程.是描述元件或设备更新现象的一类随机过程.设对某元件的工作进行观测.假定元件的使用寿命是一随机变量,当元件发生故障时就进行修理或换上新的同类元件,而且元件的更新是即时的(修理或更换元件所需 ...

  8. TextView字体阴影效果

    android:shadowDx="1" android:shadowDy="1" android:shadowColor="#8c8c8c" ...

  9. java.io.IOException: mark/reset not supported

    java.io.IOException: mark/reset not supported at java.io.InputStream.reset(InputStream.java:348) at ...

  10. 字节、字、bit、byte的关系

    字 word 字节 byte 位 bit 字长是指字的长度 1字=2字节(1 word = 2 byte) 1字节=8位(1 byte = 8bit)  一个字的字长为16 一个字节的字长是8 bps ...