假期训练——POJ - 1176 Party Lamps 思维+暴力+DFS

To brighten up the gala dinner of the IOI'98 we have a set of N coloured 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 3K+1 (with K >= 0), i.e., 1,4,7,... 
There is a counter C which 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 and information on the final state of some of the lamps. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.
Input
Your program is to read from standard input. The input contains four lines, describing the number N of lamps available, the number C of button presses, and the state of some of the lamps in the final configuration. 
The first line contains the number N and the second line the final value of counter C. The third line lists the lamp numbers you are informed to be ON in the final configuration, separated by one space and terminated by the integer -1. The fourth line lists the lamp numbers you are informed to be OFF in the final configuration, separated by one space and terminated by the integer -1. 

The parameters N and C are constrained by: 
10 <= N <= 100 
1 <= C <= 10000 
The number of lamps you are informed to be ON, in the final configuration, is less than or equal to 2.The number of lamps you are informed to be OFF, in the final configuration, is less than or equal to 2.
Output
Your program is to write to standard output. The output must contain all the possible final configurations (without repetitions) of all the lamps. There is at least one possible final configuration. Each possible configuration must be written on a different line. 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. Configurations should be listed in binary ascending order.
Sample Input
10
1
-1
7 -1
Sample Output
0000000000
0101010101
0110110110

其实想一下,,不管操作多少次,就有8种情况,把操作数分成奇数和偶数就好了

所以当c大于4的时候令c等于4,然后暴力一下就好了

#include <iostream> 
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define mod  1e10+7
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  vector<int> vi ;
typedef  long long ll;
typedef  unsigned long long  ull; 
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

set<string>  aa;

set<string>::iterator it;
int n,c,t;
vi a;
vi b;
void dfs(string &s,int step);
int main()
{
	string s;
	ios::sync_with_stdio(false);
	cin >> n>>c;
	for(int i=0;i<n;i++)
	{
		s+="1";
	} 
	while(cin>>t)
	{
		if(t==-1)
			break;
		a.push_back(t-1);
	}
	while(cin>>t)
	{
		if(t==-1)
			break;
		b.push_back(t-1);
	}
	if(c>4)
	  c = 4;
	dfs(s,0); 
	
	for(it = aa.begin();it!=aa.end();it++)
	{
		cout<<*it<<endl;
	}
	
	return 0;
} 


void dfs(string &s,int step)
{
	//cout<<step<<endl;
	if(step>=c)
	{
		int flag = 1;
		for(int i = 0;i<a.size();i++)
		{
			if(s[a[i]]=='0')
			{
				flag = 0;
				break;
			}
		}
		
		for(int i = 0;i<b.size();i++)
		{
			//cout<<b[i]<<endl;
			if(s[b[i]]=='1')
			{
				flag = 0;
				break;
			}
		}
		if(flag)
			aa.insert(s);
		return ;
	}
	
	for(int i=0;i<s.size();i+=2)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	dfs(s,step+1);
	for(int i=0;i<s.size();i+=2)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	for(int i=1;i<s.size();i+=2)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	dfs(s,step+1);
	for(int i=1;i<s.size();i+=2)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	
	
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	dfs(s,step+1);
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	
	for(int i=0;i<s.size();i+=3)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	dfs(s,step+1);
	for(int i=0;i<s.size();i+=3)
	{
		if(s[i]=='0')
			s[i]='1';
		else	
			s[i]='0';
	}
	
	
}






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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值