又一发吐血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

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
 
Recommend
We have carefully selected several similar problems for you:  5057 5055 5054 5053 5052 
 
 
思路:暴力枚举每一个款式特殊灯的位置(转自:http://blog.csdn.net/u014737310/article/details/39249581

好像状压更简单:

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的更多相关文章

  1. hdu 4770 Lights Against Dudely(回溯)

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

  2. HDU 4770 Lights Against Dudely (2013杭州赛区1001题,暴力枚举)

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

  3. HDU 4770 Lights Against Dudely

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

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

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

  5. HDU 4462 Scaring the Birds (暴力枚举DFS)

    题目链接:pid=4462">传送门 题意:一个n*n的区域,有m个位置是能够放稻草人的.其余都是玉米.对于每一个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+ab ...

  6. 状态压缩 + 暴力 HDOJ 4770 Lights Against Dudely

    题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析: ...

  7. HDOJ 4770 Lights Against Dudely

    状压+暴力搜索 Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  8. HDU 4770 Lights Against DudelyLights

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

  9. hdu 1172 猜数字(暴力枚举)

    题目 这是一道可以暴力枚举的水题. //以下两个都可以ac,其实差不多一样,呵呵 //1: //4 wei shu #include<stdio.h> struct tt { ],b[], ...

随机推荐

  1. UVA 11324 The Largest Clique (强连通分量,dp)

    给出一个有向图,求一个最大的结点集合,任意两个点u,v.u可到达v或v可到达u. 一个强连通分量肯定一起选的.而且只能在一条路径上. 所以先找出所有scc,然后缩点找一条最大权的路径,按拓扑序跑DAG ...

  2. A. Pride (emmmm练习特判的好题)

    题目连接 : http://codeforces.com/problemset/problem/891/A You have an array a with length n, you can per ...

  3. DP玄学优化——斜率优化

    --以此博客来悼念我在\(QBXT\)懵逼的时光 \(rqy\; tql\) (日常%\(rqy\)) 概念及用途 斜率优化是\(DP\)的一种较为常用的优化(据说在高中课本里稍有提及),它可以用于优 ...

  4. javaEE(12)_数据库连接池

    一.直接获取数据库连接和通过池获取示意图: 二.编写数据库连接池 1.实现DataSource接口,并实现连接池功能的步骤: •在DataSource构造函数中批量创建与数据库的连接,并把创建的连接加 ...

  5. vue建项目并使用

    今天来回顾下vue项目的建立和使用,好久不用感觉不会用了. 下面两个都要全局安装 首先安装git,地址  https://gitforwindows.org/ 安装node, 地址 https://n ...

  6. 【Java_基础】Java内部类详解

    1.四种内部类 java中的四种内部类:成员内部类.静态内部类.局部内部类和匿名内部类.其中匿名内部类用到的最多. 1.1.成员内部类 若一个类定义在另一个类的内部作为实例成员,我们把这个作为实例成员 ...

  7. python数据类型、字符编码、文件处理

    介绍: 1.什么是数据? 例:x=10,10是我们要存储的数据 2.为何数据要分不同的类型? 数据是用来表示状态的,不同的状态用不同的类型的数据去表示 1.数据类型 1.数字(整形,长整形,浮点型,复 ...

  8. 自定义View画一条线

    #import "PublishContextView.h" @implementation PublishContextView -(void)drawRect:(CGRect) ...

  9. PAT Basic 1048

    1048 数字加密 本题要求实现一种数字加密方法.首先固定一个加密用正整数 A,对任一正整数 B,将其每 1 位数字与 A 的对应位置上的数字进行以下运算:对奇数位,对应位的数字相加后对 13 取余— ...

  10. java的synchronized可重入锁

    在java内部,同一线程在调用自己类中其他synchronized方法/块或调用父类的synchronized方法/块都不会阻碍该线程的执行,就是说同一线程对同一个对象锁是可重入的,而且同一个线程可以 ...