POJ 1176 - Party Lamps

本文介绍了一种通过四种按钮控制灯泡开关状态的问题,并提供了一个有效的解决方案。文章详细解释了如何利用不同按钮组合来达到特定的灯泡开关状态,特别关注于在有限的操作次数下,如何确保指定数量的灯处于开或关的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

10
1
-1
7 -1


Sample Output

0000000000
0101010101
0110110110


题意:有这么一串灯,有开和关两种状态。还有4个按钮,功能:1,将所有灯状态变化一次(开变关,关变开);2,将所有的奇数位置变化状态一次;3,将所有的偶数位置变化状态一次;4,将3K+1(K = 1,2,3,4...)位置变化状态一次。输入第一个数 N 是灯的数量,第二个数 C 是按按钮的次数,后面两行,第一行是必须为开的位置,第二行是必须为关的位置,两行都以-1结束输入。让你输出满足要求中所有可能的情况。


首先理解一点,那就是按钮只有按和不按两种状态,所以按2次和2次以上都可以抵消到0-1次之间。再就是按钮4是以3K+1为规律的,所以灯都是按照6为周期由0和1组成的串。

将所有按按钮情况进行统计,可以得出所有的状态【ans数组】,在进行一番推到之后,可以看出按按钮4次及以上可以出现所有状态。在4次以下的,就需要来判断 C 次能否完成【can数组】。输入一个要求后,挨着看是否满足要求就好了。


#include <cstdio>

///ans 来存放所有出现的情况
char ans[8][6] = {
'0','0','0','0','0','0',
'0','0','1','1','1','0',
'0','1','0','1','0','1',
'0','1','1','0','1','1',
'1','0','0','1','0','0',
'1','0','1','0','1','0',
'1','1','0','0','0','1',
'1','1','1','1','1','1'
};

///can[i][j]判断i状态是否能按j次实现
int can[8][4] = {
0,1,1,1,
0,0,1,1,
0,1,1,1,
0,1,0,1,
0,0,1,1,
0,1,1,1,
0,0,1,1,
1,0,1,1,
};

int lamps_on[3], lamps_off[3];

int main()
{
    int n, c;
    scanf("%d%d", &n, &c);

    for (int i = 0; i < 3; ++i)
    {
        scanf("%d", &lamps_on[i]);
        if (lamps_on[i] == -1)
            break;
    }
    for (int i = 0; i < 3; ++i)
    {
        scanf("%d", &lamps_off[i]);
        if (lamps_off[i] == -1)
            break;
    }

    for (int i = 0; i < 8; ++i)
    {
        bool flag = true;
        ///判断要求是否满足
        for (int j = 0; lamps_on[j] != -1; ++j)
        {
            if (ans[i][(lamps_on[j]-1)%6] == '0')
                flag = false;
        }
        for (int j = 0; lamps_off[j] != -1; ++j)
        {
            if (ans[i][(lamps_off[j]-1)%6] == '1')
                flag = false;
        }
        if (flag)///判断在规定的步数内是否可以完成
        {
            bool final_flag;
            if (c >= 4)
                final_flag = true;
            else
            {
                if (can[i][c] == 1)
                    final_flag = true;
                else
                    final_flag = false;
            }
            if (final_flag)
            {
                for (int k = 0; k < n; ++k)
                    printf("%c", ans[i][k%6]);
                printf("\n");
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值