Party Lamps POJ - 1176

Party Lamps POJ - 1176

题目描述
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

思路
模拟,有4种操作,1,2,3,4,易知如果进行了两次相同的操作,则这两次相同的操作对结果不会产生影响,所以当给你的操作次数c大于4时必定会有重复的操作,所以我们可以将操作次数c-2直到c小于4.然后暴力dfs即可,最后记得去重。

下面是代码

#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(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 1000000007
#define vi vector<int>
#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;
int n,c,on[105],off[105],fn,ff;
char s[105],jieguo[500][105],fuzhu[105];
int k=0;
int cmp(char ss1[105],char ss2[105])
{
    for(int i=0;i<n;i++)
    {
        if(ss1[i]-'0'>ss2[i]-'0') 
        {
            return 1;
        }
         if(ss1[i]-'0'<ss2[i]-'0')
        {
            return 0;
        }
    }
}

void dfs(char s1[105],int step)
{

    int flag;
    if(c==step)
    {
        flag=1;
        for(int i=0;i<fn;i++)
        {
            if(s1[on[i]]!='1')
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {
            for(int i=0;i<ff;i++)
            if(s1[off[i]]!='0')
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
        {

             for(int i=1;i<=n;i++)
             jieguo[k][i]=s1[i];
             jieguo[k][n+1]='\0';
              k++;  
        }
        return ;    
     }
     for(int i=1;i<=4;i++)
     {
        if(i==1)
        {
            for(int j=1;j<=n;j++)
            {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
             }
             dfs(s1,step+1);
             for(int j=1;j<=n;j++)
             {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
             }
        }
        else if(i==2)
        {
            int j=1;
            while(j<=n)
            {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
                j+=2;
            }
             dfs(s1,step+1);
            j=1;
            while(j<=n)
            {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
                j+=2;
            }
        }
        else if(i==3)
        {
            int j=2;
            while(j<=n)
            {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
                j+=2;
            }
             dfs(s1,step+1);
            j=2;
            while(j<=n)
            {
                if(s1[j]=='0') s1[j]='1';
                else if(s1[j]=='1') s1[j]='0';
                j+=2;
            }
        }
        else if(i==4)
        {
            int j=0;
            while((3*j+1)<=n)
            {

                if(s1[3*j+1]=='0') s1[3*j+1]='1';
                else if(s1[3*j+1]=='1') s1[3*j+1]='0';
                j++;
            }
            dfs(s1,step+1);
            j=0;
            while((3*j+1)<=n)
            {

                if(s1[3*j+1]=='0') s1[3*j+1]='1';
                else if(s1[3*j+1]=='1') s1[3*j+1]='0';
                j++;
            }
        }
     }
}
int main()
{
     int x;
     for(int i=0;i<105;i++)
     s[i]='1';
     cin>>n>>c;
     memset(jieguo,'\0',sizeof(jieguo));
     while(c>4)
     {
        c=c-2;
     }
     fn=ff=0;
     while(1)
     {
        cin>>x;
        if(x==-1) break;
        on[fn++]=x;

     } 
     while(1)
     {
        cin>>x;
        if(x==-1) break;
        off[ff++]=x;
     }

    dfs(s,0);
    for(int i=0;i<k;i++)
    {
        int f=i;
        for(int j=i+1;j<k;j++)
        if(cmp(jieguo[f],jieguo[j])==1) f=j;
        if(f!=i)
        {
            for(int j=1;j<=n;j++)
            fuzhu[j]=jieguo[i][j];
            for(int j=1;j<=n;j++)
            jieguo[i][j]=jieguo[f][j];
            for(int j=1;j<=n;j++)
            jieguo[f][j]=fuzhu[j];
        }
    }

    for(int i=1;i<=n;i++)
    fuzhu[i]=jieguo[0][i];
    for(int i=1;i<=n;i++)
    printf("%c",fuzhu[i]);
    cout<<endl;
    for(int i=1;i<k;i++)
    {
        int flag=0;
        for(int j=1;j<=n;j++)
        if(fuzhu[j]!=jieguo[i][j])
        {
            flag=1;
            break;
         } 
         if(flag==1)
         {  
            for(int j=1;j<=n;j++)
            {
                fuzhu[j]=jieguo[i][j];
                printf("%c",fuzhu[j]);
            }
           cout<<endl;
         }
     }
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值