Lights Against Dudely

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

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
 

二进制压缩枚举所有可以放的情况。

灯光可以超出边界。只有不照到非法区域就可以。

(1<<15) 枚举放的情况,15 枚举哪一个是特殊的,4枚举特殊的那个的方向,里面在15进行处理。

复杂度可以接受的

 /* ***********************************************
Author :kuangbin
Created Time :2013-11-9 13:48:11
File Name :E:\2013ACM\专题强化训练\区域赛\2013杭州\1001.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; char g[][];
pair<int,int>p[];
int a[][];
int d[][];
bool f[];
const int INF = 0x3f3f3f3f;
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m) == )
{
if(n == && m == )break;
for(int i = ;i <= n;i++)
scanf("%s",g[i]+);
int cnt = ;
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
if(g[i][j] == '.')
{
p[cnt] = make_pair(i,j);
d[i][j] = cnt++;
}
if(cnt == )
{
printf("0\n");
continue;
}
int ans = INF;
int tot = (<<cnt);
for(int i = ;i < tot;i++)
{
for(int j = ;j < cnt;j++)
if(i & (<<j))
{
for(int k = ; k < ;k++)
{
for(int tt = ;tt < cnt;tt++)
f[tt] = false;
bool flag = true;
for(int t = ;t < cnt;t++)
if(i & (<<t))
if(t != j)
{
int x = p[t].first;
int y = p[t].second;
f[d[x][y]] = true;
if(x- > )
{
if(g[x-][y] == '#')flag = false;
else f[d[x-][y]] = true;
}
if(y+ <= m)
{
if(g[x][y+] == '#')flag = false;
else f[d[x][y+]] = true;
}
if(!flag)break;
}
if(!flag)continue;
int x = p[j].first;
int y = p[j].second;
f[d[x][y]] = true;
if(k == )
{
if(x- > )
{
if(g[x-][y] == '#')flag = false;
else f[d[x-][y]] = true;
}
if(y+ <= m)
{
if(g[x][y+] == '#')flag = false;
else f[d[x][y+]] = true;
}
}
else if(k == )
{
if(x+ <= n)
{
if(g[x+][y] == '#')flag = false;
else f[d[x+][y]] = true;
}
if(y+ <= m)
{
if(g[x][y+] == '#')flag = false;
else f[d[x][y+]] = true;
}
}
else if(k == )
{
if(x+ <= n)
{
if(g[x+][y] == '#')flag = false;
else f[d[x+][y]] = true;
}
if(y- > )
{
if(g[x][y-] == '#')flag = false;
else f[d[x][y-]] = true;
}
}
else
{
if(x- > )
{
if(g[x-][y] == '#')flag = false;
else f[d[x-][y]] = true;
}
if(y- > )
{
if(g[x][y-] == '#')flag = false;
else f[d[x][y-]] = true;
}
}
if(!flag)continue;
for(int t = ;t < cnt;t++)
if(f[t] == false)
flag = false;
if(!flag)continue;
int num = ;
for(int t = ;t < cnt;t++)
if(i & (<<t))
num++;
ans = min(ans,num);
}
}
}
if(ans == INF)ans = -;
cout<<ans<<endl; }
return ;
}

HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)的更多相关文章

  1. HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. HDU 4778 Gems Fight! (2013杭州赛区1009题,状态压缩,博弈)

    Gems Fight! Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)T ...

  3. hdu 4770 Lights Against Dudely(回溯)

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

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

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

  5. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  6. HDU 4770 Lights Against Dudely

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

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

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

  8. HDU 6270 Marriage (2017 CCPC 杭州赛区 G题,生成函数 + 容斥 + 分治NTT)

    题目链接  2017 CCPC Hangzhou Problem G 题意描述很清晰. 考虑每个家庭有且仅有$k$对近亲的方案数: $C(a, k) * C(b, k) * k!$ 那么如果在第$1$ ...

  9. hdu 5288||2015多校联合第一场1001题

    pid=5288">http://acm.hdu.edu.cn/showproblem.php?pid=5288 Problem Description OO has got a ar ...

随机推荐

  1. JSBinding+Bridge.NET:Inspector拖变量支持

    之前的文档说了,JSB的设计是不允许gameObject上挂逻辑脚本的.原因很简单,在Js工程中根本就不存在C#形式的逻辑脚本,如果在Cs工程中挂上了,到了Js工程这边,直接Missing. 实际在使 ...

  2. Xcode多种Build Configuration配置使用

    Build Configuration? Xcode默认会有2个编译模式,一个是Debug,一个是Release.Release下不能调试程序,编译时有做编译优化,会比用Debug打包出来的运行快,另 ...

  3. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  4. python中的__new__、__init__和__del__

    __new__.__init__.__del__三个方法用于实例的创建和销毁,在使用python的类中,我们最常用的是__init__方法,通常称为构造方法,__new__方法几乎不会使用,这篇文章是 ...

  5. matrix 矩阵(多维DP)

    题面 \(solution:\) 这一题其实就是一个非常明显的三维背包问题(但博主太弱了就10分QAQ) \(F[i][j][k]:\)表示走到\((i,j)\)这个位置并且背包容量为 \(k\) 时 ...

  6. Hibernate的批量查询

    Hibernate的查询大致分为以下三种场景, 1. HQL查询-hibernate Query Language(多表查询,但不复杂时使用)    2. Criteria查询(单表条件查询) 3. ...

  7. 012_iTerm2 快捷键大全

    标签 新建标签:command + t 关闭标签:command + w 切换标签:command + 数字 command + 左右方向键 切换全屏:command + enter 查找:comma ...

  8. Window8.1下oracle数据库报:ora-12170 操作超时

    PLSQL 链接本机:oracle11g 服务名:orcl   一直链接不上,等了大概3分钟, 提示:ora-12170操作超时: 重启了数据库 问题还是无法解决;上网搜了一下,发现报ora-1217 ...

  9. MXNet深度学习库简介

    MXNet深度学习库简介 摘要: MXNet是一个深度学习库, 支持C++, Python, R, Scala, Julia, Matlab以及JavaScript等语言; 支持命令和符号编程; 可以 ...

  10. snmp信息的查询命令snmpwalk

    在日常监控中,经常会用到 snmp 服务,而 snmpwalk 命令则是测试系统各种信息最有效的方法,现总结一些常用的方法如下: 获取所有信息snmpwalk -v 2c -c public 52.0 ...