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

来源:互联网 发布:海外网络推广 编辑:程序博客网 时间:2024/05/21 20:56

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
101-17 -1
Sample Output
000000000001010101010110110110

其实想一下,,不管操作多少次,就有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|1const 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';elses[i]='0';}dfs(s,step+1);for(int i=0;i<s.size();i+=2){if(s[i]=='0')s[i]='1';elses[i]='0';}for(int i=1;i<s.size();i+=2){if(s[i]=='0')s[i]='1';elses[i]='0';}dfs(s,step+1);for(int i=1;i<s.size();i+=2){if(s[i]=='0')s[i]='1';elses[i]='0';}for(int i=0;i<s.size();i++){if(s[i]=='0')s[i]='1';elses[i]='0';}dfs(s,step+1);for(int i=0;i<s.size();i++){if(s[i]=='0')s[i]='1';elses[i]='0';}for(int i=0;i<s.size();i+=3){if(s[i]=='0')s[i]='1';elses[i]='0';}dfs(s,step+1);for(int i=0;i<s.size();i+=3){if(s[i]=='0')s[i]='1';elses[i]='0';}}






0 0