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
 
感想:这道题本来应该快速解决的
读题错误:0 只有15个需亮点 1 可以重复照明 2 可以照出区域
写错的地方: 1 ind数组没有清 2 k没有清 3 i 重定义
耗时的地方: 事先没有考虑清楚需要分别记录使用和不使用特殊灯的最小灯数

思路:因为只有15个需要照亮的地方,所以用2^15来记录状态,再用一个bool值记录特殊灯是否已经使用,然后DP即#include <cstdio>

#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int,int >P;
#define EMPTY(x,j) (~((x)&(1<<(j))))//是否编号为j的需照亮点未被照明
const int inf=0x7ffffff;
int n,m;
char maz[201][201];int ind[201][201],k;//maz 记录迷宫状态 ind 记录相应的需亮点序号 k 记录需亮点个数
int px[15],py[15];//记录需亮点坐标,x纵y横
int dis[128000][2];//记录达到状态i的最小灯数,dis[i][0]没有使用特殊灯 dis[i][1]使用了
bool vis[128000][2];//bfs判重节约空间时间
bool inmaze(int x,int y){
return x>=0&&x<n&&y>=0&&y<m;
}
bool ok(int x,int y,int x2,int y2){//是否可以照
if(inmaze(x,y)&&maz[x][y]=='#')return false;
if(inmaze(x2,y2)&&maz[x2][y2]=='#')return false;
return true;
}
const int dx[4][2]={{0,1},{1,0},{-1,0},{0,-1}};//特殊灯造成的 影响
const int dy[4][2]={{1,0},{0,-1},{0,1},{-1,0}};
int main(){
while(scanf("%d%d",&n,&m)==2&&m){//输入
k=0;
memset(ind,0,sizeof(ind));
for(int i=0;i<n;i++)scanf("%s",maz[i]);
for(int i=0;i<n;i++)for(int j=0;j<m;j++){//统计需亮点
if(maz[i][j]=='.'){
px[k]=i;
py[k]=j;
ind[i][j]=k++;
}
}
if(k==0){puts("0");continue;}//初始化
fill(dis[0],dis[0]+(1<<(k+1)),inf);
fill(vis[0],vis[0]+(1<<(k+1)),0);
vis[0][0]=vis[0][1]=true;
dis[0][0]=0;
queue<P> que;
que.push(P(0,false));
while(!que.empty()){//bfs寻找
int sta=que.front().first;int fl=que.front().second;que.pop();
if(sta==(1<<k)-1)break;//全部都被照亮,因为每次步数为1,所以一定是最优解
vis[sta][fl]=false;
for(int i=0;i<k;i++){
{
int sx=px[i],sy=py[i];
if(ok(sx-1,sy,sx,sy+1)){//在这个点放正常灯的影响状态
int tsta=sta|(1<<i);
if(inmaze(sx-1,sy))tsta|=(1<<ind[sx-1][sy]);
if(inmaze(sx,sy+1))tsta|=(1<<ind[sx][sy+1]);
if(dis[tsta][fl]>dis[sta][fl]+1){
dis[tsta][fl]=dis[sta][fl]+1;
if(!vis[tsta][fl]){
vis[tsta][fl]=true;
que.push(P(tsta,fl));
}
}
}
if(fl)continue;
for(int j=0;j<4;j++){//放特殊灯
int tx[2]={sx+dx[j][0],sx+dx[j][1]},ty[2]={sy+dy[j][0],sy+dy[j][1]};
if(ok(tx[0],ty[0],tx[1],ty[1])){//特殊灯可以有四个方向(其中有个方向没用)
int tsta=sta|(1<<i);
if(inmaze(tx[0],ty[0]))tsta|=(1<<ind[tx[0]][ty[0]]);
if(inmaze(tx[1],ty[1]))tsta|=(1<<ind[tx[1]][ty[1]]);
if(dis[tsta][1]>dis[sta][0]+1){
dis[tsta][1]=dis[sta][0]+1;
if(!vis[tsta][1]){
vis[tsta][1]=true;
que.push(P(tsta,1));
}
}
}
}
}
}
}
int ans=min(dis[(1<<k)-1][0],dis[(1<<k)-1][1]);
printf("%d\n",ans==inf?-1:ans);
}
return 0;
}

  

hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1的更多相关文章

  1. hdu 4771 13 杭州 现场 B - Stealing Harry Potter's Precious 暴力bfs 难度:0

    Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. W ...

  2. hdu 3682 10 杭州 现场 C - To Be an Dream Architect 简单容斥 难度:1

    C - To Be an Dream Architect Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d &a ...

  3. HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)

    题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...

  4. HDU 1074 (状态压缩DP)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...

  5. HDU 4511 (AC自动机+状态压缩DP)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=4511 题目大意:从1走到N,中间可以选择性经过某些点,比如1->N,或1->2-> ...

  6. hdu 5025 Saving Tang Monk 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092939.html 题目链接:hdu 5025 Saving Tang Monk 状态压缩 ...

  7. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  8. HDU 3681 Prison Break(状态压缩dp + BFS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3681 前些天花时间看到的题目,但写出不来,弱弱的放弃了.没想到现在学弟居然写出这种代码来,大吃一惊附加 ...

  9. hdu 4057 AC自己主动机+状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 Problem Description Dr. X is a biologist, who likes r ...

随机推荐

  1. 安装java项目开发环境

    搭建java 查看本机是否已有java 如果有,请卸载. 下载jdk 复制到服务器中 临时配置你在shell里面改,只是做了临时更改啊,一重启就没了 配置到系统的环境变量里 export JAVA_H ...

  2. js的同步异步

    由于js没有多线程,所以处理多任务的时候,可以用异步回调来解决.js中setTimeout.setInterval.ajax(jq中可以选择同步或异步)均会开启异步.遇到异步模块,会将其推入值任务队列 ...

  3. bootstrap常用知识点总结

    api地址:https://v3.bootcss.com/css/#forms 栅格参数: bootstrap 其实 是把 网页等 分为 了 12分,bootstrap把 根据屏 幕 大小 把屏 幕分 ...

  4. Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A Is it rated?

    地址:http://codeforces.com/contest/807/problem/C 题目: C. Success Rate time limit per test 2 seconds mem ...

  5. Codeforces Round #408 (Div. 2) D - Police Stations

    地址:http://codeforces.com/contest/796/problem/D 题目: D. Police Stations time limit per test 2 seconds ...

  6. ZOJ - 4048 Red Black Tree (LCA+贪心) The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    题意:一棵树上有m个红色结点,树的边有权值.q次查询,每次给出k个点,每次查询有且只有一次机会将n个点中任意一个点染红,令k个点中距离红色祖先距离最大的那个点的距离最小化.q次查询相互独立. 分析:数 ...

  7. nmon监控及简要分析

    性能测试中,各个服务器资源占用统计分析是一个很重要的组成部分,通常我们使用nmon这个工具来进行监控以及监控结果输出. 一. 在监控阶段使用类似下面的命令 ./nmon -f write_3s_20v ...

  8. selenium的基本介绍

    应吴姑娘(漂亮的姑娘)之邀,加上我师兄(屌丝)和国新(屌丝),组了个四黑小团伙,每周二分享点东西,感觉就是四个辣鸡相互取暖.可惜,今天早上直接是睡过去了,下午都捐给了<白夜追凶>---没办 ...

  9. Web前端页面的浏览器兼容性测试心得(三)总结一些IE8兼容问题的解决方案

    由于IE8不支持HTML5,而它又是Win7的默认浏览器,我们即使讨厌它,在这几年却也拿它没办法. 最近做了个需要兼容IE8的项目,不可避免地用了HTML5+CSS3,甚至canvas和svg,做兼容 ...

  10. Windows Update error 80070003

    上次更新完成一半,这次更新便会出错.办法:删除上次更新残余文件. 删除Windows 用于标识计算机更新的临时文件.需要先停止Windows Update 服务: 在开始菜单的“搜索程序和文件”框输入 ...