题意:一个200*200的迷宫,里面最多15个房间,要在里面放L型的灯泡,老板有且仅有一个灯泡是可以旋转的,其他的灯只能照到上面和右面,问至少需要几个灯泡能全部照亮房间。要求不能照到非房间的地方,可以照到边界外(因为那是墙)
思路:枚举那个特殊的灯泡,15个位置,4种方向。然后剩下的用1<<15来状态压缩暴力解决就行了,还是个搜索代码题,认真仔细就能A。
Lights Against Dudely
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2358 Accepted Submission(s): 712
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
Source
2013 Asia Hangzhou Regional Contest
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
using namespace std;
typedef long long LL;
const int INF=0x7fffffff;
const int MAX_N=10000;
int N,M;
char A[202][202];
char curA[202][202];
int did[20];
int d[4][2][2]={1,0,0,1,1,0,0,-1,0,1,-1,0,0,-1,-1,0};
struct point{
int x,y,num;
};
point p[20];
//void judge(point a){
//
//}
void putspecial(point a,int direction){//以方向direction放置特殊点
// cout<<"special"<<endl;
int nx=a.x;
int ny=a.y;
// cout<<nx<<" "<<ny<<endl;
// cout<<d[direction][0][0]<<" "<<d[direction][0][1]<<endl;
if(A[nx+d[direction][0][0]][ny+d[direction][0][1]]!='#'&&A[nx+d[direction][1][0]][ny+d[direction][1][1]]!='#'){
// cout<<"fuck"<<endl;
curA[nx][ny]=1;
curA[nx+d[direction][0][0]][ny+d[direction][0][1]]=1;
curA[nx+d[direction][1][0]][ny+d[direction][1][1]]=1;
}
// for(int i=1;i<=N;i++){
// for(int j=1;j<=M;j++){
// cout<<curA[i][j];
// }
// cout<<endl;
// }
}
void put(point a){
int x=a.x;
int y=a.y;
if(A[x-1][y]!='#'&&A[x][y+1]!='#'){
curA[x][y]=1;
curA[x-1][y]=1;
curA[x][y+1]=1;
}
// for(int i=1;i<=N;i++){
// for(int j=1;j<=M;j++){
// cout<<curA[i][j];
// }
// cout<<endl;
// }
}
int main(){
while(cin>>N>>M&&N!=0){
int n=0;
memset(A,0,sizeof(A));
memset(p,0,sizeof(p));
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
scanf(" %c",&A[i][j]);
if(A[i][j]=='.'){
p[n].x=i;
p[n].y=j;
p[n].num=n;
n++;
}
}
}
if(n==0){
printf("0\n");
continue;
}
int ans=INF;
for(int i=0;i<4;i++){
for(int j=0;j<n;j++){
// cout<<"11"<<endl;
// memset(curA,0,sizeof(curA));
// cout<<n<<endl;
for(int k=1;k<(1<<n);k++){
for(int r=0;r<n;r++){
curA[p[r].x][p[r].y]=0;
}
// cout<<"1ci"<<endl;
putspecial(p[j],i);
int m=n-1;
int c=0;
int fail=0;
int K=k;
while(K>0){
if(m==j){
K>>=1;
m--;
continue;
}
if(K&1){
c++;
put(p[m]);
}
K>>=1;
m--;
}
for(int u=0;u<n;u++){
if(curA[p[u].x][p[u].y]!=1){
fail=1;
break;
}
}
if(fail==0){
// cout<<c+1<<endl;
ans=min(ans,c+1);
}
}
}
}
if(ans==INF){
printf("-1\n");
}
else{
printf("%d\n",ans);
}
}
return 0;
}