Lights Against Dudely

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
 

暴力枚举。

include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#define Min(a ,b ) ((a)<(b)?(a):(b))
using namespace std ;
const int Max_N= ;
const int inf= ;
char str[Max_N][Max_N] ;
int N , M ;
struct Point{
int X ;
int Y ;
Point(){} ;
Point(int x ,int y):X(x),Y(y){} ;
};
vector<Point>Light ;
vector<int>now ; bool is_light[Max_N][Max_N] ;
int rote[] ;
int cango(int x ,int y){
return <=x&&x<=N&&<=y&&y<=M ;
}
int judge(){
for(int i=;i<Light.size();i++){
int x=Light[i].X ;
int y=Light[i].Y ;
is_light[x][y]= ;
}
for(int i= ; i<now.size() ; i++){
int x=Light[ now[i] ].X ;
int y=Light[ now[i] ].Y ;
if(rote[i]==){
if(cango(x-,y)&&str[x-][y]=='#')
return ;
is_light[x-][y]= ;
if(cango(x,y+)&&str[x][y+]=='#')
return ;
is_light[x][y+]= ;
is_light[x][y]= ;
}
else if(rote[i]==){
if(cango(x+,y)&&str[x+][y]=='#')
return ;
is_light[x+][y]= ;
if(cango(x,y+)&&str[x][y+]=='#')
return ;
is_light[x][y+]= ;
is_light[x][y]= ;
}
else if(rote[i]==){
if(cango(x+,y)&&str[x+][y]=='#')
return ;
is_light[x+][y]= ;
if(cango(x,y-)&&str[x][y-]=='#')
return ;
is_light[x][y-]= ;
is_light[x][y]= ;
}
else if(rote[i]==){
if(cango(x-,y)&&str[x-][y]=='#')
return ;
is_light[x-][y]= ;
if(cango(x,y-)&&str[x][y-]=='#')
return ;
is_light[x][y-]= ;
is_light[x][y]= ;
}
}
for(int i=;i<Light.size();i++){
int x=Light[i].X ;
int y=Light[i].Y ;
if(is_light[x][y]==)
return ;
}
return ;
}
int main(){
while(cin>>N>>M){
if(N==&&M==)
break ;
for(int i= ; i<=N ;i++)
scanf("%s",str[i]+) ;
Light.clear() ;
for(int i=;i<=N ;i++)
for(int j= ; j<=M;j++){
if(str[i][j]=='.')
Light.push_back(Point(i,j)) ;
}
if(Light.size()==){
puts("") ;
continue ;
}
int ans=inf ;
memset(rote,,sizeof(rote)) ;
for(int k= ; k<Light.size() ;k++){
for(int kind=;kind<=;kind++){
rote[k]=kind ;
for(int i=;i<(<<Light.size());i++){
now.clear() ;
for(int j=;j<Light.size() ; j++){
if(i&(<<j))
now.push_back(j) ;
}
if(judge()){
ans=Min(ans,now.size()) ;
}
}
}
rote[k]= ;
}
printf("%d\n",ans==inf? -:ans) ;
}
return ;
}

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

  1. hdu 4770 Lights Against Dudely(回溯)

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

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

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

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

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

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

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

  5. HDOJ 4770 Lights Against Dudely

    状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. HDU 4770 Lights Against DudelyLights

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

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

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

  8. HDU_4770 Lights Against Dudely 状压+剪枝

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

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

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

随机推荐

  1. 【shell】read

    read:read命令接收标准输入(键盘)的输入,或其他文件描述符的输入(后面在说).得到输入后,read命令将数据放入一个标准变量中. [参数][变量]  注意:变量要在参数的后面 主要参数: -t ...

  2. [转]phonegap 2.9 IOS Xcode 搭建环境

    phonegap 2.9 IOS Xcode 搭建环境   一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载ph ...

  3. 【1-5】jQuery对象和DOM对象

    1 jQuery对象转化为DOM对象: var $cr = $("#cr");//获得jQuery对象 var cr = $cr[0];//转化为DOM对象 或者:var cr = ...

  4. RDP setting group policy

    RDP setting group policy 1.Login to domain controller and go to Group Policy Management tool2.Click ...

  5. SVN服务器几种备份策略---重点svnsync备份---OK

    配置管理的一个重要使命是保证数据的安全性,防止服务器应硬盘损坏.误操作造成数据无法恢复的灾难性后果.因此制定一个完整的备份策略非常重要. 一般来说,备份策略应规定如下几部分内容:备份频度.备份方式.备 ...

  6. 黄聪:Wordpress 模版技术手册 - WordPress Theme Technical manuals

    WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css : CSS(样式表)文件 index.php : 主页模板 archive.php : Arc ...

  7. [技巧]实际项目中background-image应写在页面上

    摘自:http://www.zhangxinxu.com 因为实际项目中(数据对接时),这肯定是个动态的URL地址,css文件似乎不支持动态URL 地址. <img src="../i ...

  8. NeHe OpenGL教程 第二十九课:Blt函数

    转自[翻译]NeHe OpenGL 教程 前言 声明,此 NeHe OpenGL教程系列文章由51博客yarin翻译(2010-08-19),本博客为转载并稍加整理与修改.对NeHe的OpenGL管线 ...

  9. redis使用日志(一) 安装,调试

    第一步:下载,安装 root@iZ28fgejjw7Z:/# wget http://download.redis.io/releases/redis-.tar.gz root@iZ28fgejjw7 ...

  10. 转-Activity之间数据传递之Intent数据传递

    Intent意图 可用于Activity之间的数据传递,一般可分为下面两种情况,从当前Activity传递到目标Activity后有无返回值: 1.传递后无返回值的情况: 1 2 3 4 5 6 7 ...