状压+暴力搜索

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. MMORPG大型游戏设计与开发(服务器 AI 事件)

    AI中的事件与场景中的事件大致相同,都是由特定的条件触发的.只不过AI的事件与其他事件不同的是,对于AI的事件往往是根据不同的AI类型,和动态的触发条件下才产生的.其实不管AI多么智能,它对应的触发条 ...

  2. 【转】C# 中 10 个你真的应该学习(和使用!)的功能

    如果你开始探索C#或决定扩展你的知识,那么你应该学习这些有用的语言功能,这样做有助于简化代码,避免错误,节省大量的时间. 1)async / await 使用async / await-pattern ...

  3. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  4. [LeetCode] Simplify Path 简化路径

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  5. 格式化 float 类型,保留小数点后1位

    """  练习 :   小明的成绩从去年的72分提升到了今年的85分,请计算小明成绩提升的百分点,   并用字符串格式化显示出'xx.x%',只保留小数点后1位: &qu ...

  6. python基础补漏-06-内置模块

    1> sys 模块 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.exit(n) 退出程序,正常退出时exit(0) sys.version 获取Python解释程序的 ...

  7. Python3 面向对象(基础篇)

    面向对象 关于面向对象的标准定义网上有很多,不再讲述,现在我们来通俗点理解: 面向对象编程相对于面向过程编程和函数式编程来说,看的更长远,实现功能相对更简单. 面向对象:对象就是物体,这种编程思想就是 ...

  8. 关系数据库—SQL学习笔记

    SQL的特点: 综合统一 高度非过程化(存取路径的选择以及SQL的操作过程由系统自动完成) 面向集合的操作方式,以同一种语法结构提供多种使用方法(可以在终端键盘上直接键入SQL命令对数据库进行操作,也 ...

  9. JQuery ajax调用一直回调error函数

    使用jquery的ajax调用,发现一直回调error函数,ajax调用代码如下,后台返回是正确的,为什么会报错呢?  var descValue = $('#descEditArea').val() ...

  10. Javascript模板引擎插件收集

    为什么要用JS的模板引擎,打个比方,如果你要通过接口绑定数据,最终要加进去DOM中,我们普遍的做法就是不断的+,最终append进去,但是这样的做法就是后续人员压根就没法维护.所以这时模板引擎出来了. ...