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

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

2013 Asia Hangzhou Regional Contest

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

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<string>
 10 //#include<pair>
 11
 12 #define N 205
 13 #define M 15
 14 #define mod 10000007
 15 //#define p 10000007
 16 #define mod2 100000000
 17 #define ll long long
 18 #define LL long long
 19 #define maxi(a,b) (a)>(b)? (a) : (b)
 20 #define mini(a,b) (a)<(b)? (a) : (b)
 21
 22 using namespace std;
 23
 24 int n,m;
 25 int ans;
 26 int k;
 27 int vis[N][N];
 28 char s[N][N];
 29 int c[N];
 30 int step[4][3][2]={  {-1,0,0,0,0,1},
 31                     {0,-1,-1,0,0,0},
 32                     {0,-1,0,0,1,0},
 33                     {0,0,0,1,1,0}
 34                  };
 35 typedef struct
 36 {
 37     int x;
 38     int y;
 39 }PP;
 40
 41 PP p[N];
 42
 43 void change(int x,int y,int st[3][2],int s)
 44 {
 45     int i;
 46     int nx,ny;
 47     for(i=0;i<3;i++){
 48         nx=x+st[i][0];
 49         ny=y+st[i][1];
 50         if(nx<0 || nx>=n) continue;
 51         if(ny<0 || ny>=m) continue;
 52
 53         vis[nx][ny]=s;
 54     }
 55 }
 56
 57 int judge(int x,int y,int st[3][2])
 58 {
 59     int i;
 60     int nx,ny;
 61     for(i=0;i<3;i++){
 62         nx=x+st[i][0];
 63         ny=y+st[i][1];
 64         if(nx<0 || nx>=n || ny<0 || ny>=m) continue;
 65         if(s[nx][ny]==‘#‘){
 66             return 0;
 67         }
 68     }
 69     return 1;
 70 }
 71
 72 void ini()
 73 {
 74     int i,j;
 75     memset(c,0,sizeof(c));
 76     ans=20;
 77     k=0;
 78     for(i=0;i<n;i++){
 79         scanf("%s",s[i]);
 80     }
 81     for(i=n-1;i>=0;i--){
 82         for(j=0;j<m;j++){
 83             if(s[i][j]==‘.‘){
 84                 k++;
 85                 p[k].x=i;p[k].y=j;
 86             }
 87         }
 88     }
 89
 90     for(i=1;i<=k;i++){
 91         if(judge(p[i].x,p[i].y,step[0])==1){
 92             c[i]=1;
 93         }
 94         else{
 95             c[i]=-1;
 96         }
 97     }
 98 }
 99
100
101 void dfs(int i,int f,int re)
102 {
103     if(re>=ans){
104         return;
105     }
106     if(i==k+1){
107         ans=re;
108         return;
109     }
110     if(vis[ p[i].x ][ p[i].y ]==1)
111     {
112         dfs(i+1,f,re);
113     }
114     if(f!=i && c[i]==1){
115         change(p[i].x,p[i].y,step[0],1);
116         dfs(i+1,f,re+1);
117         change(p[i].x,p[i].y,step[0],0);
118     }
119 }
120
121 void solve()
122 {
123     int i,j;
124     if(k==0){
125         ans=0;return;
126     }
127     memset(vis,0,sizeof(vis));
128     dfs(1,0,0);
129     for(j=1;j<=3;j++)
130     {
131         memset(vis,0,sizeof(vis));
132         for(i=1;i<=k;i++){
133             if(judge( p[i].x,p[i].y,step[j] )==1){
134                 change(p[i].x,p[i].y,step[j],1);
135                 dfs(1,i,1);
136                 change(p[i].x,p[i].y,step[j],0);
137             }
138         }
139     }
140 }
141
142
143 void out()
144 {
145     if(ans==20) ans=-1;
146     printf("%d\n",ans);
147 }
148
149 int main()
150 {
151    // freopen("data.in","r",stdin);
152     //freopen("data.out","w",stdout);
153     //scanf("%d",&T);
154    // for(int ccnt=1;ccnt<=T;ccnt++)
155    // while(T--)
156     while(scanf("%d%d",&n,&m)!=EOF)
157     {
158         if(n==0 && m==0 ) break;
159         //printf("Case %d: ",ccnt);
160         ini();
161         solve();
162         out();
163     }
164
165     return 0;
166 }
时间: 2024-10-09 21:21:16

HDU 4770 Lights Against Dudely 暴力枚举+dfs的相关文章

hdu 4770 Lights Against Dudely 暴力搜索

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4770 题目大意是让你放一些灯照亮一些房间 灯不可以照到某些特殊房间 但可以照出边界 灯光必须覆盖所有可以放灯的房间 求至少需要多少灯 搜索题 应该注意到冗长而无聊的题面中有告诉你最多只有15个可以放灯的房间 所以2^15来枚举 还应该注意到冗长而无聊的题面中有告诉你最多只有1个灯可以转向 所以还应该枚举哪个灯来转向 转向再枚举4个方向 然后判断是否成立 为了简化问题 单独把那些可放灯空间拿出来 最多

HDU 4770 Lights Against Dudely(暴力)

HDU 4770 Lights Against Dudely 题目链接 题意:给定灯,有一盏灯可以旋转,问最少几个灯可以照亮.的位置,并且不能照到# 思路:暴力求解,先枚举特殊的灯,再枚举正常的灯,要加剪枝,不然会TLE 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; const int N =

hdu 4770 Lights Against Dudely(回溯)

题目链接:hdu 4770 Lights Against Dudely 题目大意:在一个N*M的银行里,有N*M个房间,'#'代表坚固的房间,'.'代表的是脆弱的房间,脆弱的房间个数不会超过15个,现在为了确保安全,要在若干个脆弱的房间上装灯,普通的灯是照亮{0, 0}, {-1, 0}, {0, 1}(和题目中坐标有点出入),然后可以装一个特殊的,可以照射 { {0, 0}, {0, 1}, {1, 0} }, { {0, 0}, {-1, 0}, {0, -1} }, { {0, 0}, {

HDU 4770 Lights Against Dudely

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

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

题目链接:传送门 题意:一个n*n的区域,有m个位置是可以放稻草人的,其余都是玉米.对于每个位置(x,y)所放稻草人都有个作用范围ri, 即abs(x-i)+abs(y-j)<=r,(i,j)为作用范围内.问至少要在几个位置上放稻草人,才能覆盖所有的玉米,若不可能则输出-1. 有一个trick,就是放稻草人的位置不用被覆盖 eg: input: 2 4 1 1 1 2 2 1 2 2 0 0 0 0 output: 0 0 代码如下: #include <iostream> #inclu

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h

hdu 4932 Miaomiao&#39;s Geometry 暴力枚举

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 694    Accepted Submission(s): 180 Problem Description There are N point

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

题目传送门 题意:有n*m的房间,'.'表示可以被点亮,'#'表示不能被点亮,每点亮一个房间会使旁边的房间也点亮,有意盏特别的灯可以选择周围不同方向的房间点亮.问最少需要多少灯使得所有房间点亮 分析:需要被点亮的房间最多只有15个,所以考虑状压,然后暴力枚举选择哪一个当作特殊灯和枚举选择哪个方向使旁边的房间亮,注意清空vis数组需要优化,memset超时.上交6分钟1Y,Orz... /************************************************ * Auth

HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7963    Accepted Submission(s): 1920 Problem Description When Teddy was a child , he was always thinking about some simple math p