HDU 4770 Lights Against DudelyLights
Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 525 Accepted Submission(s): 157
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.
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
If there are no vulnerable rooms, print 0.
If Dumbledore has no way to light up all vulnerable rooms, print -1.
##
##
2 3
#..
..#
3 3
###
#.#
###
0 0
2
-1
解题思路:杭州的A题,看到网上很多大牛用状态压缩来写,表示YM,虽然我也想到了压缩,不过这一题不用压缩就能写,所以我没用压缩,直接爆搜加优化就可以了。详见代码
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 205
using namespace std;
char maze[N][N];
int x[20],y[20],visit[N][N],ans;
bool vis[N][N];
void recover(int k)
{
int i;
for(i=0;i<k;i++)
visit[x[i]][y[i]]=0,vis[x[i]][y[i]]=false;
}
void dfs(int cnt,int now,int k,int dep)
{
if(cnt>=ans-1&&now!=k||cnt>dep)
//优化3:已经放的灯数比现在的答案大就直接返回
return ;
if(now==k)
{
ans=min(ans,cnt);
return ;
}
for(int i=0;i<k;i++)
{
int flag1=0,flag2=0,flag3=0,flag=0;
if(maze[x[i]-1][y[i]]!='#'&&maze[x[i]][y[i]+1]!='#'&&!vis[x[i]][y[i]]&&(visit[x[i]][y[i]]==0||(visit[x[i]][y[i]+1]==0&&maze[x[i]][y[i]+1]=='.')||(visit[x[i]-1][y[i]]==0&&maze[x[i]-1][y[i]]=='.')))
//vis数组记录该位置放灯了没,visit数组记录该位置是否被照亮,优化2:放下一个灯的时候至少能多照亮一个位置才放灯
{
if(visit[x[i]][y[i]]==0)
flag1=1;
if(visit[x[i]][y[i]+1]==0)
flag2=1;
if(visit[x[i]-1][y[i]]==0)
flag3=1;
flag=flag1+flag2+flag3;
visit[x[i]][y[i]]++;
visit[x[i]-1][y[i]]++;
visit[x[i]][y[i]+1]++;
vis[x[i]][y[i]]=true;
if(maze[x[i]-1][y[i]]=='.'&&maze[x[i]][y[i]+1]=='.')
dfs(cnt+1,now+flag,k,dep);
else if(maze[x[i]-1][y[i]]=='.')
dfs(cnt+1,flag1+flag3+now,k,dep);
else if(maze[x[i]][y[i]+1]=='.')
dfs(cnt+1,flag1+flag2+now,k,dep);
else
dfs(cnt+1,flag1+now,k,dep);
visit[x[i]][y[i]]--;
visit[x[i]-1][y[i]]--;
visit[x[i]][y[i]+1]--;
vis[x[i]][y[i]]=false;
}
}
return ;
}
int main()
{
int m,n,i,j,k,t;
bool flag;
while(scanf("%d%d",&m,&n),m+n)
{
flag=true;
k=t=0;
ans=100;
getchar();
for(i=0;i<=m+1;i++)
for(j=0;j<=n+1;j++)
{
maze[i][j]='@';
visit[i][j]=0;
vis[i][j]=false;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf("%c",&maze[i][j]);
if(maze[i][j]=='.')
x[k]=i,y[k++]=j;
}
getchar();
}
if(k==0)
{
printf("0\n");
continue;
}
int Min=k%3?k/3+1:k/3;//优化1:n个可以放灯的点至少需要[n/3]展灯
for(i=0;i<k;i++)
{
if(maze[x[i]-1][y[i]]!='#'&&maze[x[i]][y[i]+1]!='#')
{
visit[x[i]][y[i]]++;
visit[x[i]-1][y[i]]++;
visit[x[i]][y[i]+1]++;
vis[x[i]][y[i]]=true;
if(maze[x[i]-1][y[i]]=='@'&&maze[x[i]][y[i]+1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,1,k,j);
}
else if(maze[x[i]-1][y[i]]=='@'||maze[x[i]][y[i]+1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,2,k,j);
}
else
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,3,k,j);
}
}
recover(k);
flag=true;
if(maze[x[i]][y[i]+1]!='#'&&maze[x[i]+1][y[i]]!='#')
{
visit[x[i]][y[i]]++;
visit[x[i]+1][y[i]]++;
visit[x[i]][y[i]+1]++;
vis[x[i]][y[i]]=true;
if(maze[x[i]+1][y[i]]=='@'&&maze[x[i]][y[i]+1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,1,k,j);
}
else if(maze[x[i]+1][y[i]]=='@'||maze[x[i]][y[i]+1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,2,k,j);
}
else
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,3,k,j);
}
}
recover(k);
flag=true;
if(maze[x[i]+1][y[i]]!='#'&&maze[x[i]][y[i]-1]!='#')
{
visit[x[i]][y[i]]++;
visit[x[i]+1][y[i]]++;
visit[x[i]][y[i]-1]++;
vis[x[i]][y[i]]=true;
if(maze[x[i]+1][y[i]]=='@'&&maze[x[i]][y[i]-1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,1,k,j);
}
else if(maze[x[i]+1][y[i]]=='@'||maze[x[i]][y[i]-1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,2,k,j);
}
else
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,3,k,j);
} }
recover(k);
flag=true;
if(maze[x[i]][y[i]-1]!='#'&&maze[x[i]-1][y[i]]!='#')
{
visit[x[i]][y[i]]++;
visit[x[i]-1][y[i]]++;
visit[x[i]][y[i]-1]++;
vis[x[i]][y[i]]=true;
if(maze[x[i]-1][y[i]]=='@'&&maze[x[i]][y[i]-1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,1,k,j);
}
else if(maze[x[i]-1][y[i]]=='@'||maze[x[i]][y[i]-1]=='@')
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,2,k,j);
}
else
{
for(j=Min;j<=min(ans,k);j++)
dfs(1,3,k,j);
}
}
recover(k);
flag=true;
}
if(ans==100)
printf("-1\n");
else
printf("%d\n",ans);
}
return 0;
}
HDU 4770 Lights Against DudelyLights的更多相关文章
- hdu 4770 Lights Against Dudely(回溯)
pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...
- HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ cz ...
- HDU 4770 Lights Against Dudely
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4770 Lights Against Dudely(暴力+状压)
思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...
- hdu 4770 13 杭州 现场 A - Lights Against Dudely 暴力 bfs 状态压缩DP 难度:1
Description Harry: "But Hagrid. How am I going to pay for all of this? I haven't any money.&quo ...
- hdu 4770(枚举 + dfs爆搜)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 思路:由于最多只有15个".",可以直接枚举放置的位置,然后判断是否能够全部 ...
- HDOJ 4770 Lights Against Dudely
状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 5820 Lights(扫描线+zkw线段树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5820 [题目大意] 在一个大小为50000*50000的矩形中,有n个路灯. 询问是否每一对路灯之 ...
随机推荐
- java.lang.StringBuilder
1.StringBuilder 的对象和 String 的对象类似,并且 StringBuilder 的对象能被修改.Internally,这个对象被当做一个包含一系列字符的可变长度的数组对待.这个序 ...
- Thinkphp中自己组合的数据怎样使用框架的分页
做项目有时候,需要自己处理组合数据,不是直接从表中提取出来的.不能按照手册得方法分页显示数据.这时候就得想办法,正好看到他人的方法.地址为:http://www.thinkphp.cn/code/27 ...
- spring security为不同用户显示各自的登录成功页面
一个常见的需求是,普通用户登录之后显示普通用户的工作台,管理员登陆之后显示后台管理页面.这个功能可以使用taglib解决. 其实只要在登录成功后的jsp页面中使用taglib判断当前用户拥有的权限进行 ...
- 获取Java系统相关信息
package com.test; import java.util.Properties; import java.util.Map.Entry; import org.junit.Test; pu ...
- linux重新设定分区大小
一.目的 在使用CentOS6.3版本Linux系统的时候,发现根目录(/)的空间不是很充足,而其他目录空间有很大的空闲,所以本文主要是针对现在已有的空间进行调整.首先,先来查看一下系统的空间分配情况 ...
- 房间安排-nyoj168
描述 2010年上海世界博览会(Expo2010),是第41届世界博览会.于2010年5月1日至10月31日期间,在中国上海市举行.本次世博会也是由中国举办的首届世界博览会.上海世博会以“城市,让生活 ...
- 在HTML文件的表单中添加{%csrf_token%}便可以解决问题
原因是django为了在用户提交表单时防止跨站攻击所做的保护 只需在HTML文件的表单中添加{%csrf_token%}便可以解决问题 ------------------------if判断{% i ...
- canvas 线条不清楚的问题
对于canvas 画出的1px线条不清楚的问题, 一般是坐标点+0.5像素的问题, 但是有时要考虑viewpoint的问题,让canvas的width = 980,同时viewpoint = 980 ...
- thinkphp3.2整合phpexcel
由于thinkphp3.2使用命名空间,而 PHPExcel没有使用,那么两者整合的最重要问题就是如何导入的问题. 一.PHPExcel.php和PHPExcel文件夹都放在ThinkPHP/Libr ...
- linux命令学习(2):wc 命令
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数.字数.行数,并将统计结果显示输出. 1.命令格式: wc [选项]文件... 2.命令功能: 统计指定文件中的字节数. ...