关键在于发现几个规律:
某一按钮,按偶数次等同于没按。
按钮的先后次序不影响结果。
灯的状态6位一组不变(解题时没发现这个规律,开了个100大小的bool数组)
剩下就是几个操作的排列组合了。代码写得很繁...
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("lamps.in");
ofstream fout("lamps.out");
#ifdef _DEBUG
#define out cout
#define in cin
#else
#define out fout
#define in fin
#endif
struct state_node{
bool states[101];
int len;
bool operator<(const state_node & n1) const{
for(int i=1;i<=len;++i){
if(states[i]!=n1.states[i])
return states[i]<n1.states[i];
}
return false;
}
bool operator==(const state_node & n1) const{
for(int i=1;i<=len;++i){
if(states[i]!=n1.states[i])
return false;
}
return true;
}
};
state_node node;
int n,c;
vector<int>finalon,finaloff;
vector<state_node> result;
void op(int kind)
{
switch(kind){
case 1:
for(int i=1;i<=n;++i)
node.states[i]=!node.states[i];
break;
case 2:
for(int i=1;i<=n;++i){
if(i&1)
node.states[i]=!node.states[i];
}
break;
case 3:
for(int i=1;i<=n;++i){
if(!(i&1))
node.states[i]=!node.states[i];
}
break;
case 4:
for(int i=1;i<=n;++i){
if(i%3==1){
node.states[i]=!node.states[i];
}
}
break;
}
}
bool isok()
{
for(int i=0;i<finalon.size();++i){
if(!node.states[finalon[i]])
return false;
}
for(int i=0;i<finaloff.size();++i){
if(node.states[finaloff[i]])
return false;
}
return true;
}
void do0()
{
if(isok())
result.push_back(node);
}
void do1()
{
for(int i=1;i<=4;++i){
op(i);
if(isok()){
result.push_back(node);
}
op(i);
}
}
void do2()
{
for(int i=1;i<=3;++i){
for(int j=i+1;j<=4;++j){
op(i);
op(j);
if(isok()){
result.push_back(node);
}
op(i);
op(j);
}
}
}
void do3()
{
//操作1,2,3,4各执行一次,前三个操作抵消
op(4);
do1();
}
void do4()
{
op(4);
if(isok()){
result.push_back(node);
}
op(4);
}
void action(int i)
{
switch(i){
case 0: do0();break;
case 1: do1();break;
case 2: do2();break;
case 3: do3();break;
case 4: do4();break;
}
}
void solve()
{
in>>n>>c;
node.len = n;
for(int i=1;i<=n;++i)
node.states[i] = true;
int t;
do{
in>>t;
if(t!=-1)
finalon.push_back(t);
}while(t!=-1);
do{
in>>t;
if(t!=-1)
finaloff.push_back(t);
}while(t!=-1);
bool has_result = false;
for(int i=0;i<=4;++i){
if(c>=i&&(c-i)%2==0){
action(i);
}
}
sort(result.begin(),result.end());
vector<state_node>::iterator end =
unique(result.begin(),result.end());
for(vector<state_node>::iterator i = result.begin();
i!=end; ++i){
has_result = true;
for(int j=1;j<=n;++j){
out<<i->states[j];
}
out<<endl;
}
if(!has_result){
out<<"IMPOSSIBLE"<<endl;
}
}
int main(int argc,char *argv[])
{
solve();
return 0;
}