hdu 4770 Lights Against Dudely

Posted slgkaifa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 4770 Lights Against Dudely相关的知识,希望对你有一定的参考价值。

用离中心距离为1的L去覆盖最多十五个点,#不能被覆盖,能够覆盖的地方能够越界。有一个L能够是旋转0,90,180,270去覆盖的

问,最少要多少个L能够实现全覆盖。

枚举可旋转的L所在的位置,然后直接爆搜……

没想到直接A了……

数据比較弱么……

#include<iostream>
#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
int n,row,line,goal,all;
bool islight[210][210];
bool iscan[210][210];
bool used[25];
int dx[4][2];
int dy[4][2];
void make()
{
	dx[0][0]=-1;dx[0][1]=0;dx[1][0]=0;dx[1][1]=1;
	dy[0][0]=0;dy[0][1]=1;dy[1][0]=1;dy[1][1]=0;
	dx[2][0]=1;dx[2][1]=0;dx[3][0]=0;dx[3][1]=-1;
	dy[2][0]=0;dy[2][1]=-1;dy[3][0]=-1;dy[3][1]=0;
}
struct node
{
	int x,y;
	node(){}
	node(int a,int b){x=a;y=b;}
}a[25];
bool dfs(int s,int cnt,int step)
{
	int i,t;
	bool flag,flag1,flag2,flag3;
	if(step==goal)
		return cnt==all;
	for(i=s;i<n;i++)
	{
		if(used[i])
			continue;
		t=cnt;
		flag1=flag2=flag3=0;
		if(a[i].x>0)
		{
			if(!iscan[a[i].x-1][a[i].y])
				continue;
			if(!islight[a[i].x-1][a[i].y])
			{
				t++;
				islight[a[i].x-1][a[i].y]=1;
				flag1=1;
			}
		}
		if(a[i].y<line-1)
		{
			if(!iscan[a[i].x][a[i].y+1])
				continue;
			if(!islight[a[i].x][a[i].y+1])
			{
				t++;
				islight[a[i].x][a[i].y+1]=1;
				flag2=1;
			}
		}
		if(!islight[a[i].x][a[i].y])
		{
			t++;
			islight[a[i].x][a[i].y]=1;
			flag3=1;
		}
		flag=dfs(i+1,t,step+1);
		if(flag1)
			islight[a[i].x-1][a[i].y]=0;
		if(flag2)
			islight[a[i].x][a[i].y+1]=0;
		if(flag3)
			islight[a[i].x][a[i].y]=0;
		if(flag)
			return 1;
	}
	return 0;
}
bool isbeyond(int x,int y)
{
	return x<0||y<0||x>=row||y>=line;
}
int main()
{
	string s;
	int i,j,k,cnt,ans;
	make();
	while(cin>>row>>line)
	{
		if(row==0&&line==0)
			break;
		n=all=0;
		memset(iscan,0,sizeof(iscan));
		for(i=0;i<row;i++)
		{
			cin>>s;
			for(j=0;j<line;j++)
			{
				if(s[j]==‘.‘)
				{
					a[n++]=node(i,j);
					iscan[i][j]=1;
					all++;
				}
			}
		}
		ans=16;
		for(i=0;i<n;i++)
		{
			for(j=0;j<4;j++)
			{
				cnt=1;
				memset(islight,0,sizeof(islight));
				for(k=0;k<2;k++)
				{
					if(isbeyond(a[i].x+dx[j][k],a[i].y+dy[j][k]))
						continue;
					if(!iscan[a[i].x+dx[j][k]][a[i].y+dy[j][k]])
						break;
					islight[a[i].x+dx[j][k]][a[i].y+dy[j][k]]=1;
					cnt++;
				}
				if(k<2)
					continue;
				islight[a[i].x][a[i].y]=1;
				for(goal=1;goal<=n;goal++)
				{
					if(dfs(0,cnt,1))
					{
						ans=min(ans,goal);
						break;
					}
				}
			}
		}
		if(n==0)
			ans=0;
		else if(ans==16)
			ans=-1;
		cout<<ans<<endl;
	}
	return 0;
}

Lights Against Dudely

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


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




























以上是关于hdu 4770 Lights Against Dudely的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3404 Switch lights(Nim积)题解

Codeforces 954 D Fight Against Traffic

Codeforces 954D Fight Against Traffic(BFS 最短路)

Android system :灯光系统_HAL_lights

洛谷 P2962 [USACO09NOV]灯Lights

Unity2.9 光源(Lights)