• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

2013 Asia Hangzhou Regional Contest A

Lights Against Dudely

Time Limit: 2000/1000 MS (Java/Others)   

 Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 174    Accepted Submission(s): 55

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:  4780 4779 4778 4777 4776 
 1 #pragma comment(linker, "/STACK:1024000000,1024000000")
 2 #include <map>
 3 #include <stack>
 4 #include <queue>
 5 #include <vector>
 6 #include <string>
 7 #include <cmath>
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <iostream>
12 #include <algorithm>
13 using namespace std;
14 #define maxn 202
15 #define ll long long
16 #define mod 1000000007
17 #define INF 0x7fffffff
18 #define eps 1e-8
19 int n, m, k;
20 char s[maxn][maxn];
21 int  vis[maxn];
22 int a[maxn][maxn];
23 int light[maxn][maxn];
24 int x[maxn];
25 int y[maxn];
26 int mi, flag;
27 int dx[4] = { 0,1, 0, -1 };
28 int dy[4] = { 1,0, -1, 0 };
29 int ok(int x1, int y1, int x2,int y2){
30     if (a[x1][y1]==-1||a[x2][y2]==-1)return 0;
31     else return 1;
32 }
33 void dfs(int t,int z){
34     if (t > k)return;
35     if (z >= mi)return;
36     if (t == k){
37         int c = 0;
38         for (int i = 0; i < k; ++i)
39         if (light[x[i]][y[i]] <= 0)return;
40         mi = min(mi, z);
41     }
42     for (int i = 0; i < 5; i++){
43         if (flag == 0 && i < 3)continue;
44         if (i == 4){ dfs(t + 1, z); return; }
45         int x1 = x[t] + dx[i];
46         int x2 = x[t] + (dx[(i + 1) % 4]);
47         int y1 = y[t] + dy[i];
48         int y2 = y[t] + (dy[(i + 1) % 4]);
49         if (ok(x1, y1, x2, y2)){
50             if (i<3)flag = 0;
51             vis[t] = 1;
52             light[x[t]][y[t]]++;
53             light[x1][y1]++;
54             light[x2][y2]++;
55             dfs(t + 1, z + 1);
56             vis[t] = 0;
57             light[x[t]][y[t]]--;
58             light[x1][y1]--;
59             light[x2][y2]--;
60             if(i<3)flag = 1;
61         }
62     }
63 }
64 int main(){
65     int t;
66     /*while (t--){
67         scanf("%d", &n);
68     }*/
69     while (scanf("%d%d", &n, &m)){
70         if (n == 0 || m == 0)break;
71         k = 0;
72         mi = INF;
73         flag = 1;
74         memset(a, 0, sizeof a);
75         memset(light, 0, sizeof light);
76         for (int i = 1; i <= n; ++i){
77             scanf("%s", s[i]);
78             for (int j = 1; j <= m; ++j)
79             if (s[i][j - 1] == '.'){
80                 a[i][j] = 0;
81                 x[k] = i;
82                 y[k++] = j;
83             }
84             else a[i][j] = -1;
85         }
86         if (k == 0){ printf("0\n"); continue; }
87         dfs(0,0);
88         if (mi == INF||mi==0)printf("-1\n");
89         else printf("%d\n", mi);
90     }
91     return 0;
92 }
View Code
posted @ 2013-11-09 22:30  HaibaraAi  阅读(137)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3