poj 1176 Party Lamps 搜索

来源:互联网 发布:开发长沙软件外包 编辑:程序博客网 时间:2024/05/21 17:17
Party Lamps
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 4119 Accepted: 1415

Description

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

Source

IOI 1998

一个按钮按了两次等于没按。

#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<cmath>#include<algorithm>#include<climits>#include<queue>#include<vector>#include<map>#include<sstream>#include<set>#include<stack>#include<cctype>#include<utility>#pragma comment(linker, "/STACK:102400000,102400000")#define PI 3.1415926535897932384626#define eps 1e-10#define sqr(x) ((x)*(x))#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)#define  lson   num<<1,le,mid#define rson    num<<1|1,mid+1,ri#define MID   int mid=(le+ri)>>1#define zero(x)((x>0? x:-x)<1e-15)#define mk    make_pair#define _f     first#define _s     secondusing namespace std;//const int INF=    ;typedef long long ll;//const ll inf =1000000000000000;//1e15;//ifstream fin("input.txt");//ofstream fout("output.txt");//fin.close();//fout.close();//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);const int INF =0x3f3f3f3f;const int maxn= 100 +10    ;//const int maxm=    ;const int N=4;int n,C;vector<int >on;vector<int >off;int use[7];int st[maxn];set<string>se;void startfun(int x){    if(x==1 )    {        for(int i=1;i<=n;i++)            st[i]^=1;    }    else if(x==2)    {        for(int i=1;i<=n;i+=2)            st[i]^=1;    }    else if(x==3)    {        for(int i=2;i<=n;i+=2)            st[i]^=1;    }    else    {        for(int i=1;i<=n;i+=3)            st[i]^=1;    }}void output(){    for(set<string>:: iterator it=se.begin();it!=se.end();it++)    {        cout<<*it<<endl;    }}void install(){    string s;    for(int i=1;i<=n;i++)    {        s+=st[i]+'0';    }    se.insert(s);}void check(){    for(int i=0;i<on.size();i++)    {        int x=on[i];        if(st[x]==0)  return;    }    for(int i=0;i<off.size();i++)    {        int x=off[i];        if(st[x]==1)  return;    }    int num=0;    for(int i=1;i<=4;i++)        num+=use[i];    int delta=C-num;    if(delta<0|| delta%2 )  return;    install();}void dfs(int step){    if(step== N+1 ) {check();  return;}        use[step]=0;        dfs(step+1);        startfun(step);        use[step]=1;        dfs(step+1);        use[step]=0;//改变了等的状态,和计数器C,需要回溯;        startfun(step);}int main(){  scanf("%d",&n);  scanf("%d",&C);  int x;  on.clear();  off.clear();  while(~scanf("%d",&x)&& (~x))  {      on.push_back(x);  }  while(~scanf("%d",&x)&& (~x))  {      off.push_back(x);  }  se.clear();  for(int i=1;i<=n;i++)    st[i]=1;  dfs(1);  output();    return 0;}



0 0