• HDOJ 4770 Lights Against Dudely


    状压+暴力搜索

    Lights Against Dudely

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


    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
     
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 
      5 using namespace std;
      6 
      7 int dir_x[4]={-1,0,1,0},dir_y[4]={0,1,0,-1};
      8 
      9 int n,m,cnt=0,ans;
     10 char G[250][250];
     11 bool den[250][250];
     12 
     13 bool inside(int x,int y)
     14 {
     15     return (x>=0&&x<n)&&(y>=0&&y<m);
     16 }
     17 
     18 void SHOWden()
     19 {
     20     for(int i=0;i<n;i++,putchar(10))
     21         for(int j=0;j<m;j++)
     22             cout<<den[i][j]<<" ";
     23     cout<<"    ...   
    ";
     24 }
     25 
     26 void SHOWST(int x)
     27 {
     28     for(int i=0;i<cnt;i++)
     29     {
     30         if(x&(1<<i)) cout<<"1 ";
     31         else cout<<"0 ";
     32     }
     33     cout<<endl;
     34 }
     35 
     36 struct LP
     37 {
     38     int x,y;
     39 }lp[20];
     40 
     41 void dfs(int state,int lan,int n)
     42 {
     43     //SHOWST(state);
     44     //cout<<lan<<" ...... "<<n<<endl;
     45     if(n>=ans) return ;
     46     if(lan==cnt)
     47     {
     48         ans=n; return ;
     49     }
     50     for(int i=0;i<cnt;i++)
     51     {
     52         if(state&(1<<i)) continue;
     53         int lan0=0,lan1=0,lan2=0;
     54         if(den[lp[i].x][lp[i].y]==0)
     55         {
     56             lan0++; den[lp[i].x][lp[i].y]=1;
     57         }
     58         int x1=lp[i].x+dir_x[0],x2=lp[i].x+dir_x[1];
     59         int y1=lp[i].y+dir_y[0],y2=lp[i].y+dir_y[1];
     60         if(!inside(x1,y1)||(inside(x1,y1)&&G[x1][y1]=='.'))
     61         {
     62             if(!inside(x2,y2)||(inside(x2,y2)&&G[x2][y2]=='.'))
     63             {
     64                 if(inside(x1,y1)&&den[x1][y1]==0)
     65                 {
     66                     lan1++; den[x1][y1]=1;
     67                 }
     68                 if(inside(x2,y2)&&den[x2][y2]==0)
     69                 {
     70                     lan2++; den[x2][y2]=1;
     71                 }
     72                 dfs(state|(1<<i),lan+lan2+lan0+lan1,n+1);
     73             }
     74         }
     75         if(lan0) den[lp[i].x][lp[i].y]=0;
     76         if(lan1) den[x1][y1]=0;
     77         if(lan2) den[x2][y2]=0;
     78     }
     79 }
     80 
     81 int main()
     82 {
     83     while(scanf("%d%d",&n,&m)!=EOF)
     84     {
     85         if(n==0&&m==0) break;
     86         getchar();
     87         memset(G,' ',sizeof(G));
     88         cnt=0;
     89         for(int i=0;i<n;i++)
     90         {
     91             scanf("%s",G[i]);
     92         }
     93 /*
     94         for(int i=0;i<n;i++)
     95         {
     96             for(int j=0;j<m;j++)
     97                 cout<<" "<<G[i][j];
     98             cout<<endl;
     99         }
    100         cout<<endl;
    101 */
    102         memset(lp,0,sizeof(lp));
    103         memset(den,0,sizeof(den));
    104         for(int i=0;i<n;i++)
    105         {
    106             for(int j=0;j<m;j++)
    107             {
    108                 if(G[i][j]=='.')
    109                 {
    110                     lp[cnt].x=i;lp[cnt].y=j;
    111                     cnt++;
    112                 }
    113             }
    114         }
    115         if(cnt==0)
    116         {
    117             printf("0
    "); continue;
    118         }
    119         ans=0x3f3f3f3f;
    120         for(int i=0;i<cnt;i++)
    121         {
    122             den[lp[i].x][lp[i].y]=1;
    123             //cout<<lp[i].x<<" ..... "<<lp[i].y<<endl;
    124             for(int j=0;j<4;j++)
    125             {
    126                 int x1=lp[i].x+dir_x[j%4],x2=lp[i].x+dir_x[(j+1)%4];
    127                 int y1=lp[i].y+dir_y[j%4],y2=lp[i].y+dir_y[(j+1)%4];
    128                 if((inside(x1,y1)&&G[x1][y1]=='.')||!inside(x1,y1))
    129                 {
    130                     if((inside(x2,y2)&&G[x2][y2]=='.')||!inside(x2,y2))
    131                     {
    132                         int lan=1;
    133                         if(inside(x1,y1)) { lan++; den[x1][y1]=1; }
    134                         if(inside(x2,y2)) { lan++; den[x2][y2]=1; }
    135                         //cout<<inside(x1,y1)<<"  "<<x1<<","<<y1<<endl;
    136                         //cout<<inside(x2,y2)<<"  "<<x2<<","<<y2<<endl;
    137                         //cout<<"  ..    
    ";
    138                         //SHOWden();
    139                         dfs((1<<i),lan,1);
    140                         //SHOWden();
    141                         if(inside(x1,y1)) den[x1][y1]=0;
    142                         if(inside(x2,y2)) den[x2][y2]=0;
    143                     }
    144                 }
    145             }
    146             den[lp[i].x][lp[i].y]=0;
    147         }
    148         if(ans==0x3f3f3f3f) puts("-1");
    149         else printf("%d
    ",ans);
    150     }
    151     return 0;
    152 }
  • 相关阅读:
    2018.11.15 RF antenna impedance-matching
    2018.11.14 Chopin’s
    2018.11.13 N4010A 通信设置
    2018.11.12 RF debug
    2018.11 企业战略课堂笔记4 -内部条件分析
    2018.11 企业战略课堂笔记3 五力模型
    2018.11 企业战略课堂笔记2 SWOT-4C战略
    2018.11 企业战略课堂笔记1 概论
    PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件操作控制类属性movable和tabsClosable介绍
    PyQt(Python+Qt)学习随笔:QTabWidget选项卡部件外观展示类属性elideMode、documentMode、tabBarAutoHide、tabShape介绍
  • 原文地址:https://www.cnblogs.com/CKboss/p/3416225.html
Copyright © 2020-2023  润新知