• HDU 4770 Lights Against Dudely


    Lights Against Dudely

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


    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
     

     暴力枚举。

    include <iostream>
    #include <stdio.h>
    #include <string>
    #include <string.h>
    #include <math.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #define Min(a ,b )  ((a)<(b)?(a):(b))
    using namespace std ;
    const int Max_N=208 ;
    const int inf=208 ;
    char str[Max_N][Max_N]  ;
    int N , M  ;
    struct Point{
          int X  ;
          int Y ;
          Point(){} ;
          Point(int x ,int y):X(x),Y(y){}  ;
    };
    vector<Point>Light ;
    vector<int>now ;
    
    bool is_light[Max_N][Max_N]  ;
    int rote[18]  ;
    int cango(int x ,int y){
         return 1<=x&&x<=N&&1<=y&&y<=M ;
    }
    int judge(){
         for(int i=0;i<Light.size();i++){
                int x=Light[i].X   ;
                int y=Light[i].Y   ;
                is_light[x][y]=0  ;
         }
         for(int i=0 ; i<now.size() ; i++){
                int x=Light[ now[i] ].X   ;
                int y=Light[ now[i] ].Y   ;
                if(rote[i]==0){
                        if(cango(x-1,y)&&str[x-1][y]=='#')
                              return  0 ;
                        is_light[x-1][y]=1  ;
                        if(cango(x,y+1)&&str[x][y+1]=='#')
                              return  0 ;
                        is_light[x][y+1]=1  ;
                        is_light[x][y]=1 ;
                }
                else  if(rote[i]==1){
                        if(cango(x+1,y)&&str[x+1][y]=='#')
                              return  0 ;
                        is_light[x+1][y]=1  ;
                        if(cango(x,y+1)&&str[x][y+1]=='#')
                              return  0 ;
                        is_light[x][y+1]=1  ;
                        is_light[x][y]=1 ;
                }
                else  if(rote[i]==2){
                        if(cango(x+1,y)&&str[x+1][y]=='#')
                              return  0 ;
                        is_light[x+1][y]=1  ;
                        if(cango(x,y-1)&&str[x][y-1]=='#')
                              return  0 ;
                        is_light[x][y-1]=1  ;
                        is_light[x][y]=1 ;
                }
                else  if(rote[i]==3){
                        if(cango(x-1,y)&&str[x-1][y]=='#')
                              return  0 ;
                        is_light[x-1][y]=1  ;
                        if(cango(x,y-1)&&str[x][y-1]=='#')
                              return  0 ;
                        is_light[x][y-1]=1  ;
                        is_light[x][y]=1 ;
                }
         }
         for(int i=0;i<Light.size();i++){
                int x=Light[i].X   ;
                int y=Light[i].Y   ;
               if(is_light[x][y]==0)
                     return   0  ;
         }
         return  1  ;
    }
    int main(){
        while(cin>>N>>M){
             if(N==0&&M==0)
                   break   ;
             for(int i=1 ; i<=N ;i++)
                  scanf("%s",str[i]+1)   ;
             Light.clear()  ;
             for(int i=1;i<=N ;i++)
                 for(int j=1 ; j<=M;j++){
                        if(str[i][j]=='.')
                               Light.push_back(Point(i,j))   ;
            }
             if(Light.size()==0){
                  puts("0")   ;
                  continue   ;
              }
             int ans=inf ;
             memset(rote,0,sizeof(rote))  ;
             for(int k=0 ; k<Light.size() ;k++){
                     for(int kind=0;kind<=3;kind++){
                             rote[k]=kind  ;
                             for(int i=1;i<(1<<Light.size());i++){
                                   now.clear()  ;
                                   for(int j=0;j<Light.size() ; j++){
                                         if(i&(1<<j))
                                               now.push_back(j)   ;
                                   }
                                   if(judge()){
                                           ans=Min(ans,now.size())  ;
                                   }
                             }
                     }
                     rote[k]=0  ;
             }
             printf("%d
    ",ans==inf?  -1:ans)  ;
        }
        return  0 ;
    }
  • 相关阅读:
    openstack-ntp时间同步服务
    如何将icon图标库引入自己的项目中
    微信小程序实现滑动tab切换和点击tab切换并显示相应的数据(附源代码)
    微信小程序分享至朋友圈的方法
    微信小程序--分享功能
    mpvue-新建页面、页面跳转、自适应单位
    微信小程序mpvue-动态改变navigationBarTitleText值
    mpvue中使用flyjs全局拦截
    H5 布局 -- 让容器充满屏幕高度或自适应剩余高度
    使用mpvue开发小程序如何定义全局变量
  • 原文地址:https://www.cnblogs.com/liyangtianmen/p/3416045.html
Copyright © 2020-2023  润新知