HDU 4770

news/2024/11/24 20:24:23/

        这道题利用DFS进行枚举就可以了。

       由于图中的点很多,但是vulnerable room 很少,所以要把这些点保存起来。此外,由于回溯的时候判断哪些room任然被灯照射有些麻烦,所以用状态压缩的方式表示一个vulnerable room 被照射的状态,0表示未被照射,大于0表示至少有一盏灯照射,这样方便些。此外还有一点,就是允许灯照到房间之外。

代码(C++):

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>#define MAX 205
#define INF (1<<30)
using namespace std;const int r[4][2]={{-1,1},{1,1},{-1,-1},{1,-1}};
struct Point{int x;int y;
} point[18];char g[MAX][MAX];
int light[MAX][MAX],c,ans;void dfs(int ip,int num,bool tag)
{int x,y,i;if(ip==c){for(i=0;i<c;i++){if(light[point[i].x][point[i].y]==0) return;}ans=min(ans,num);return;}if(num>ans) return;dfs(ip+1,num,tag);    //不放灯//放灯x=point[ip].x;y=point[ip].y;for(i=0;i<4;i++){if(tag==false&&i>0) break;if(g[x+r[i][0]][y]=='.'&&g[x][y+r[i][1]]=='.'){light[x+r[i][0]][y]|=1<<ip;light[x][y+r[i][1]]|=1<<ip;light[x][y]|=1<<ip;if(i>0) dfs(ip+1,num+1,false);else dfs(ip+1,num+1,tag);light[x+r[i][0]][y]^=1<<ip;light[x][y+r[i][1]]^=1<<ip;light[x][y]^=1<<ip;}}
}int main()
{//freopen("in.txt","r",stdin);int n,m,i,j;while(scanf("%d %d",&n,&m)&&n+m!=0){for(i=1;i<=n;i++) scanf("%s",g[i]+1);for(i=0;i<n+2;i++) g[i][0]=g[i][m+1]='.';for(i=0;i<m+2;i++) g[0][i]=g[n+1][i]='.';c=0;for(i=1;i<=n;i++){for(j=1;j<=m;j++){if(g[i][j]=='.'){point[c].x=i;point[c++].y=j;}}}if(c==0){printf("0\n");continue;}memset(light,0,sizeof(light));ans=INF;dfs(0,0,true);if(ans==INF) printf("-1\n");else printf("%d\n",ans);}return 0;
}

题目( http://acm.hdu.edu.cn/showproblem.php?pid=4770):

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


http://www.ppmy.cn/news/147399.html

相关文章

BZOJ4770 图样

牛逼的DP&#xff0c;一股TC气息 求期望的话可以先求出所有情况的和再除以情况数 考虑 f[i][j] 表示 i 个点每个点的权值都是j位的二进制数&#xff0c;最小生成树的和 一定是最高位为0的和为1的分别的形成生成树&#xff0c;然后两部分之间连一条边 那么枚举 i 个点里有多…

群晖服务器性能测试,原创首发!群晖J3455 G4560 I7 4770HQ功耗性能测试!

本帖最后由 -我傻也要中二 于 2019-4-28 16:58 编辑 写在最前&#xff1a;本人也是新手入得群晖&#xff0c;一开始直接买的J3455&#xff0c;群晖的强大的功能&#xff0c;嘿嘿舒服。不过因为我本身是有一台windows服务器用来搭建ERP的&#xff0c;考虑到群晖可以运行虚拟机就…

4770: 栈操作

定义一个栈&#xff0c;可以对栈进行“压入堆栈”、“弹出栈顶元素”、“清空堆栈”、“获取栈顶元素”等操作。键盘输入一些命令&#xff0c;可以执行上述操作。本题中&#xff0c;栈元素均为整数。栈的最大元素个数为1000。 输入 输入各个命令&#xff0c;它们对应的格式如…

碧砚适合佳能328 4452 ICD520 4472 4450 硒鼓4700一体机墨盒4770

转载于:https://www.cnblogs.com/zengkefu/p/6036577.html

【JavaSE】方法的使用--05

目录 一、方法的概念与使用 1.1 什么是方法 1.2 方法的定义 1.3 方法调用的执行过程 1.4 实参和形参的关系&#xff08;重要&#xff09; 1.5 有无返回值的方法 二、方法的重载 2.1 方法重载的概念 2.2 方法重载的要求 2.3 方法签名 前言&#xff1a; 之前很久没写这…

更新中-深度学习实战中遇到的一些概念+少量代码

onnx ONNX 是一种用于机器学习模型的开放式表示格式&#xff0c;它可以让不同的深度学习框架之间共享模型。 import onnxruntime # 加载模型 session onnxruntime.InferenceSession(model.onnx) # 运行模型。第一个参数是输出变量列表&#xff0c;不指定的话返回所有值 outp…

代码随想录算法训练营第三十一天 | 力扣 455.分发饼干, 376. 摆动序列, 53. 最大子序和

贪心算法 贪心的本质是选择每一阶段的局部最优&#xff0c;从而达到全局最优 贪心算法一般分为如下四步&#xff1a; 将问题分解为若干个子问题找出适合的贪心策略求解每一个子问题的最优解将局部最优解堆叠成全局最优解 这个四步其实过于理论化了&#xff0c;我们平时在做…

华硕笔记本k555拆机图解_华硕K751大屏笔记本拆机解析

虽然目前笔记本的发展趋势是轻薄便携化&#xff0c;但是大尺寸笔记本依然具有一定的市场&#xff0c;近期华硕就推出一款17英寸的大屏幕影音笔记本——K751&#xff0c;这款笔记本采用17.3英寸全高清雾面显示屏&#xff0c;内置SonicMaster音响系统&#xff0c;可以为用户提供更…