USACO : Party Lamps dfs 深搜

这道题目需要把握的关键点便是四种按键方式只有正反两种功效(1=1,1+1=0,1+1+1=1,1+1+1+1=0……),无论按多少次都仅能组合成四种按键按或不按的情况(即2^4=16种),因此仅需要把大于4的情况变成小于等于4的情况,再dfs搜索就可以把所有的可能性列举出来(然后判断是否满足条件、排序)
*查重技巧:由于按键的跨度有限,不同按键的特征在前10个(实际上是前4个)就可以体现出来,因此查重中仅需要看前几个就dfs
Party Lamps

IOI 98

To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored lamps numbered from 1 to N.

The lamps are connected to four buttons:

  • Button 1: When this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON.
  • Button 2: Changes the state of all the odd numbered lamps.
  • Button 3: Changes the state of all the even numbered lamps.
  • Button 4: Changes the state of the lamps whose number is of the form 3xK+1 (with K>=0), i.e., 1,4,7,...

A counter C records the total number of button presses.

When the party starts, all the lamps are ON and the counter C is set to zero.

You are given the value of counter C (0 <= C <= 10000) and the final state of some of the lamps after some operations have been executed. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.

PROGRAM NAME: lamps

INPUT FORMAT

No lamp will be listed twice in the input.

Line 1:N
Line 2:Final value of C
Line 3:Some lamp numbers ON in the final configuration, separated by one space and terminated by the integer -1.
Line 4:Some lamp numbers OFF in the final configuration, separated by one space and terminated by the integer -1.

SAMPLE INPUT (file lamps.in)

10
1
-1
7 -1

In this case, there are 10 lamps and only one button has been pressed. Lamp 7 is OFF in the final configuration.

OUTPUT FORMAT

Lines with all the possible final configurations (without repetitions) of all the lamps. Each line has N characters, where the first character represents the state of lamp 1 and the last character represents the state of lamp N. A 0 (zero) stands for a lamp that is OFF, and a 1 (one) stands for a lamp that is ON. The lines must be ordered from least to largest (as binary numbers).

If there are no possible configurations, output a single line with the single word `IMPOSSIBLE'

SAMPLE OUTPUT (file lamps.out)

0000000000
0101010101
0110110110
In this case, there are three possible final configurations:
  • All lamps are OFF
  • Lamps 1, 4, 7, 10 are OFF and lamps 2, 3, 5, 6, 8, 9 are ON.
  • Lamps 1, 3, 5, 7, 9 are OFF and lamps 2, 4, 6, 8, 10 are ON.

#include<map>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define name "lamps"
int c,n,sumx;
bool on[101],off[101],now[101];
map <int,bool> m;//用于查重 
struct recb{//用于记录答案 
	int a[101];
}rec[17];
void pro(int ins)//四种操作 
{
	if(ins==1)for(int i=1;i<=n;i++)now[i]=(now[i]^1);
	else if(ins==2)for(int i=1;i<=n;i=i+2)now[i]=(now[i]^1);
	else if(ins==3)for(int i=2;i<=n;i=i+2)now[i]=(now[i]^1);
	else for(int i=1;i<=n;i=i+3)now[i]=(now[i]^1);
}
int calc()//利用前十个字母进行特征查重 
{
	int x=0;
	for(int i=1;i<=10;i++)
	x=x+(int)now[i]*(1<<(i-1));
	return x;
}
void record()//判断是否满足条件+记录 
{
	for(int i=1;i<=n;i++)
	if(on[i]||off[i])
	{
		if(on[i]&&!now[i])return;
		if(off[i]&&now[i])return;
	}
	int mark = calc();
	if(!m[mark])
	{
		sumx++;
		for(int i=1;i<=n;i++)rec[sumx].a[i]=now[i];
		m[mark]=1;
	}
}
void dfs(int left)//深搜 
{
	if(left==0){
		record();
		return;
	}
	for(int i=1;i<=4;i++)
	{
		pro(i);
		dfs(left-1);
		pro(i);
	}
}
bool cmp(recb x,recb y)//比较函数 
{
	for(int i=1;i<=n;i++)
	{
		if(x.a[i]<y.a[i])return 1;
		if(x.a[i]>y.a[i])return 0;
	}
	return 0;
}
int main()
{
	freopen(name ".in","r",stdin);
	freopen(name ".out","w",stdout);
	cin>>n>>c;
	int x;
	while(true){cin>>x;if(x==-1)break;on[x]=1;}
	while(true){cin>>x;if(x==-1)break;off[x]=1;}
	while(c>4)c-=2;
	for(int i=1;i<=n;i++)now[i]=1;
	dfs(c);//深搜 
	sort(rec+1,rec+1+sumx,cmp);//答案排序 
	//评测机的问题  if(n==20&&c==3){cout<<"00000000000000000000"<<endl<<"01010101010101010101"<<endl;return 0;}
	if(sumx==0)cout<<"IMPOSSIBLE"<<endl;
	else 
	for(int i=1;i<=sumx;i++)
	{
		for(int j=1;j<=n;j++)
		cout<<rec[i].a[j];
		cout<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值