首页 > 搜索 > DFS搜索 > HDU 4770-Lights Against Dudely-DFS-[解题报告]HOJ
2015
09-17

HDU 4770-Lights Against Dudely-DFS-[解题报告]HOJ

Lights Against Dudely

问题描述 :

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:
Sword

  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.

输入:

  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

输出:

  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

样例输入:

2 2
##
##
2 3
#..
..#
3 3
###
#.#
###
0 0

样例输出:

0
2
-1

题目:http://acm.hdu.edu.cn/showproblem.php?pid=4770

因为最多只能放15盏灯,所以我们可以枚举在哪些位置放灯,又因为只有一盏灯可以旋转,所以我们可以在前面的基础上枚举旋转哪盏灯,并且枚举旋转方向.

答案最理想的情况是0,所以我们可以从0开始,不可行的话,就把答案+1;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;

#define rep(i,s,t) for(int i=s;i<t;i++)
typedef pair<int,int>ii;

int n,m,Size,len;
char s[210][210];
map<ii,bool>vis;
vector<int>v[16],choose;
vector<ii>node;
int dx[]={-1,-1,1,1};
int dy[]={-1,1,-1,1};

inline int getone(int i){
	int ret=0;
	while(i) {ret++,i-=i&(-i);}
	return ret;
}

inline void Init(){
	int all=(1<<Size);
	rep(i,1,all){
		v[getone(i)].push_back(i);
	}
}

inline bool ok(int x,int y,int xx,int yy){
	if(xx>=0 && xx<n){
		if(s[xx][y]=='#') return 0;
	}
	if(yy>=0 && yy<m){
		if(s[x][yy]=='#') return 0;
	}
	return 1;
}

inline bool flood(int now){
	rep(i,0,len){
		if(i!=now){
			int x=node[choose[i]].first,y=node[choose[i]].second;
			int xx=x-1,yy=y+1;
			if(!ok(x,y,xx,yy)) return 0;
			vis[node[choose[i]]]=vis[ii(x,yy)]=vis[ii(xx,y)]=1;
		}
	}
	return 1;
}

inline bool check(int now,int j){
	int x=node[choose[now]].first,y=node[choose[now]].second;
	vis.clear();
	int xx=x+dx[j],yy=y+dy[j];
	if(!ok(x,y,xx,yy)) return 0;
	if(!flood(now)) return 0;
	vis[ii(x,yy)]=vis[ii(xx,y)]=vis[node[choose[now]]]=1;
	rep(i,0,Size){
		if(!vis[node[i]]) return 0;
	}
	return 1;
}

inline bool dfs(int tmp){
	int S=v[tmp].size();
	rep(i,0,S){
		int st=v[tmp][i];
		choose.clear();
		rep(j,0,Size){
			if(st&(1<<j)) choose.push_back(j);
		}
		len=choose.size();
		rep(j,0,len){
			if(check(j,0)) return 1;
			if(check(j,1)) return 1;
			if(check(j,2)) return 1;
			if(check(j,3)) return 1;
		}
	}
	return 0;
}

int main(){
	while(scanf("%d%d",&n,&m), n||m){
		node.clear();
		rep(i,0,n){
			scanf("%s",s[i]);
			rep(j,0,m){
				if(s[i][j]=='.'){
					node.push_back(ii(i,j));
				}
			}
		}
		Size=node.size();
		if(Size==0){
			puts("0");continue;
		}
		rep(i,1,Size+1) v[i].clear();
		Init();
		int ans=1;
		while(ans<=Size && !dfs(ans)) ans++;
		if(ans==Size+1) puts("-1");
		else printf("%d\n",ans);
	}
	return 0;
}

参考:http://blog.csdn.net/zhuhuangjian/article/details/24672469