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

Lights Against Dudely

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 171    Accepted Submission(s): 53

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  

 

 

二进制压缩枚举所有可以放的情况。

 

灯光可以超出边界。只有不照到非法区域就可以。

 

(1<<15) 枚举放的情况,15 枚举哪一个是特殊的,4枚举特殊的那个的方向,里面在15进行处理。

复杂度可以接受的

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013-11-9 13:48:11
  4 File Name     :E:\2013ACM\专题强化训练\区域赛\2013杭州\1001.cpp
  5 ************************************************ */
  6 
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <iostream>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <queue>
 13 #include <set>
 14 #include <map>
 15 #include <string>
 16 #include <math.h>
 17 #include <stdlib.h>
 18 #include <time.h>
 19 using namespace std;
 20 
 21 char g[220][220];
 22 pair<int,int>p[20];
 23 int a[220][220];
 24 int d[220][220];
 25 bool f[20];
 26 const int INF = 0x3f3f3f3f;
 27 int main()
 28 {
 29     //freopen("in.txt","r",stdin);
 30     //freopen("out.txt","w",stdout);
 31     int n,m;
 32     while(scanf("%d%d",&n,&m) == 2)
 33     {
 34         if(n == 0 && m == 0)break;
 35         for(int i = 1;i <= n;i++)
 36             scanf("%s",g[i]+1);
 37         int cnt = 0;
 38         for(int i = 1;i <= n;i++)
 39             for(int j = 1;j <= m;j++)
 40                 if(g[i][j] == '.')
 41                 {
 42                     p[cnt] = make_pair(i,j);
 43                     d[i][j] = cnt++;
 44                 }
 45         if(cnt == 0)
 46         {
 47             printf("0\n");
 48             continue;
 49         }
 50         int ans = INF;
 51         int tot = (1<<cnt);
 52         for(int i = 0;i < tot;i++)
 53         {
 54             for(int j = 0;j < cnt;j++)
 55                 if(i & (1<<j))
 56                 {
 57                     for(int k = 0; k < 4;k++)
 58                     {
 59                         for(int tt = 0;tt < cnt;tt++)
 60                             f[tt] = false;
 61                         bool flag = true;
 62                         for(int t = 0;t < cnt;t++)
 63                             if(i & (1<<t))
 64                                 if(t != j)
 65                                 {
 66                                     int x = p[t].first;
 67                                     int y = p[t].second;
 68                                     f[d[x][y]] = true;
 69                                     if(x-1 > 0)
 70                                     {
 71                                         if(g[x-1][y] == '#')flag = false;
 72                                         else f[d[x-1][y]] = true;
 73                                     }
 74                                     if(y+1 <= m)
 75                                     {
 76                                         if(g[x][y+1] == '#')flag = false;
 77                                         else f[d[x][y+1]] = true;
 78                                     }
 79                                     if(!flag)break;
 80                                 }
 81                         if(!flag)continue;
 82                         int x = p[j].first;
 83                         int y = p[j].second;
 84                         f[d[x][y]] = true;
 85                         if(k == 0)
 86                         {
 87                             if(x-1 > 0)
 88                             {
 89                                 if(g[x-1][y] == '#')flag = false;
 90                                 else f[d[x-1][y]] = true;
 91                             }
 92                             if(y+1 <= m)
 93                             {
 94                                 if(g[x][y+1] == '#')flag = false;
 95                                 else f[d[x][y+1]] = true;
 96                             }
 97                         }
 98                         else if(k == 1)
 99                         {
100                             if(x+1 <= n)
101                             {
102                                 if(g[x+1][y] == '#')flag = false;
103                                 else f[d[x+1][y]] = true;
104                             }
105                             if(y+1 <= m)
106                             {
107                                 if(g[x][y+1] == '#')flag = false;
108                                 else f[d[x][y+1]] = true;
109                             }
110                         }
111                         else if(k == 2)
112                         {
113                             if(x+1 <= n)
114                             {
115                                 if(g[x+1][y] == '#')flag = false;
116                                 else f[d[x+1][y]] = true;
117                             }
118                             if(y-1 > 0)
119                             {
120                                 if(g[x][y-1] == '#')flag = false;
121                                 else f[d[x][y-1]] = true;
122                             }
123                         }
124                         else
125                         {
126                             if(x-1 > 0)
127                             {
128                                 if(g[x-1][y] == '#')flag = false;
129                                 else f[d[x-1][y]] = true;
130                             }
131                             if(y-1 > 0)
132                             {
133                                 if(g[x][y-1] == '#')flag = false;
134                                 else f[d[x][y-1]] = true;
135                             }
136                         }
137                         if(!flag)continue;
138                         for(int t = 0;t < cnt;t++)
139                             if(f[t] == false)
140                                 flag = false;
141                         if(!flag)continue;
142                         int num = 0;
143                         for(int t = 0;t < cnt;t++)
144                             if(i & (1<<t))
145                                 num++;
146                         ans = min(ans,num);
147                     }
148                 }
149         }
150         if(ans == INF)ans = -1;
151         cout<<ans<<endl;
152 
153     }
154     return 0;
155 }

 

 

 

 

 

 

 

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/p/3416163.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞