HDU 4770 Lights Against Dudely 暴力枚举+dfs
又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性。
| 11779687 | 2014-10-02 20:57:53 | Accepted | 4770 | 0MS | 496K | 2976 B | G++ | czy |
Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360 Accepted Submission(s): 392
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.好像状压更简单:
http://www.cnblogs.com/kuangbin/p/3416163.html
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<string>
//#include<pair> #define N 205
#define M 15
#define mod 10000007
//#define p 10000007
#define mod2 100000000
#define ll long long
#define LL long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,m;
int ans;
int k;
int vis[N][N];
char s[N][N];
int c[N];
int step[][][]={ {-,,,,,},
{,-,-,,,},
{,-,,,,},
{,,,,,}
};
typedef struct
{
int x;
int y;
}PP; PP p[N]; void change(int x,int y,int st[][],int s)
{
int i;
int nx,ny;
for(i=;i<;i++){
nx=x+st[i][];
ny=y+st[i][];
if(nx< || nx>=n) continue;
if(ny< || ny>=m) continue; vis[nx][ny]=s;
}
} int judge(int x,int y,int st[][])
{
int i;
int nx,ny;
for(i=;i<;i++){
nx=x+st[i][];
ny=y+st[i][];
if(nx< || nx>=n || ny< || ny>=m) continue;
if(s[nx][ny]=='#'){
return ;
}
}
return ;
} void ini()
{
int i,j;
memset(c,,sizeof(c));
ans=;
k=;
for(i=;i<n;i++){
scanf("%s",s[i]);
}
for(i=n-;i>=;i--){
for(j=;j<m;j++){
if(s[i][j]=='.'){
k++;
p[k].x=i;p[k].y=j;
}
}
} for(i=;i<=k;i++){
if(judge(p[i].x,p[i].y,step[])==){
c[i]=;
}
else{
c[i]=-;
}
}
} void dfs(int i,int f,int re)
{
if(re>=ans){
return;
}
if(i==k+){
ans=re;
return;
}
if(vis[ p[i].x ][ p[i].y ]==)
{
dfs(i+,f,re);
}
if(f!=i && c[i]==){
change(p[i].x,p[i].y,step[],);
dfs(i+,f,re+);
change(p[i].x,p[i].y,step[],);
}
} void solve()
{
int i,j;
if(k==){
ans=;return;
}
memset(vis,,sizeof(vis));
dfs(,,);
for(j=;j<=;j++)
{
memset(vis,,sizeof(vis));
for(i=;i<=k;i++){
if(judge( p[i].x,p[i].y,step[j] )==){
change(p[i].x,p[i].y,step[j],);
dfs(,i,);
change(p[i].x,p[i].y,step[j],);
}
}
}
} void out()
{
if(ans==) ans=-;
printf("%d\n",ans);
} int main()
{
// freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
//scanf("%d",&T);
// for(int ccnt=1;ccnt<=T;ccnt++)
// while(T--)
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n== && m== ) break;
//printf("Case %d: ",ccnt);
ini();
solve();
out();
} return ;
}
HDU 4770 Lights Against Dudely 暴力枚举+dfs的更多相关文章
- hdu 4770 Lights Against Dudely(回溯)
pid=4770" target="_blank" style="">题目链接:hdu 4770 Lights Against Dudely 题 ...
- 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
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4770 Lights Against Dudely(暴力+状压)
思路: 这个题完全就是暴力的,就是代码长了一点. 用到了状压,因为之前不知道状压是个东西,大佬们天天说,可是我又没学过,所以对状压有一点阴影,不过这题中的状压还是蛮简单的. 枚举所有情况,取开灯数最少 ...
- HDU 4462 Scaring the Birds (暴力枚举DFS)
题目链接:pid=4462">传送门 题意:一个n*n的区域,有m个位置是能够放稻草人的.其余都是玉米.对于每一个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+ab ...
- 状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely
题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析: ...
- HDOJ 4770 Lights Against Dudely
状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- HDU 4770 Lights Against DudelyLights
Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1172 猜数字(暴力枚举)
题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...
随机推荐
- faster rcnn细节总结
1.roi_pooling层是先利用spatial_scale将region proposal映射到feature map上,然后利用pooled_w.pooled_h分别将映射后的框的长度.宽度等分 ...
- 行内元素的padding和margin是否无效
html中元素分为三种:块级元素.行内元素(也叫内联元素),内联块级元素. 常用块级元素:<div>.<p>.<h1>...<h6>.<ol> ...
- python Object-Oriented Programming
Python 类的成员.成员修饰符.类的特殊成员. Python 类的成员 类的成员可以分为三大类: 字段.方法和属性. #注:所有成员中,只有普通字段的内容保存对象中,即: #根据此类创建了多少对象 ...
- 在64位的linux中运行32位的应用程序
常规做法,先添加32bit架构: sudo dpkg --add-architecture i386 sudo apt-get update sudo apt-get install libc6:i3 ...
- pre-receive hook declined
没有master分支的代码提交权限. 分配权限或者提交分支合并申请
- iOS旋屏
横竖屏切换,视图乱了怎么办? 首先,我们必须了解一下下列4种状态,它们被用来描述设备旋转方向: UIInterfaceOrientationLandscapeLeft 向左,即HOME键在右 UIIn ...
- 51nod 1135 原根 (数论)
题目链接 建议与上一篇欧拉函数介绍结合食用. 知识点:1.阶:a和模m互质,使a^d≡1(mod m)成立的最小正整数d称为a对模m的阶(指数) 例如: 2^2≡1(mod3),2对模3的阶为2; ...
- 【Java_基础】Java内部类详解
1.四种内部类 java中的四种内部类:成员内部类.静态内部类.局部内部类和匿名内部类.其中匿名内部类用到的最多. 1.1.成员内部类 若一个类定义在另一个类的内部作为实例成员,我们把这个作为实例成员 ...
- tkinter学习-文本框
阅读目录 Entry 输入框 Text 文本框 Entry: 说明:输入控件,用于显示简单的文本内容 属性:在输入框中用代码添加和删除内容,同样也是用insert()和delete()方法 from ...
- 【REDIS】 redis-cli 命令
Redis提供了丰富的命令(command)对数据库和各种数据类型进行操作,这些command可以在Linux终端使用. 在编程时,比如使用Redis 的Java语言包,这些命令都有对应的方法.下面将 ...