usaco2.2.4 Party Lamps

一 原题

Party Lamps
IOI 98

To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored 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 3xK+1 (with K>=0), i.e., 1,4,7,...

A counter C 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 (0 <= C <= 10000) and the final state of some of the lamps after some operations have been executed. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.

PROGRAM NAME: lamps

INPUT FORMAT

No lamp will be listed twice in the input.

Line 1:N
Line 2:Final value of C
Line 3:Some lamp numbers ON in the final configuration, separated by one space and terminated by the integer -1.
Line 4:Some lamp numbers OFF in the final configuration, separated by one space and terminated by the integer -1.

SAMPLE INPUT (file lamps.in)

10
1
-1
7 -1

In this case, there are 10 lamps and only one button has been pressed. Lamp 7 is OFF in the final configuration.

OUTPUT FORMAT

Lines with all the possible final configurations (without repetitions) of all the lamps. 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. The lines must be ordered from least to largest (as binary numbers).

If there are no possible configurations, output a single line with the single word `IMPOSSIBLE'

SAMPLE OUTPUT (file lamps.out)

0000000000
0101010101
0110110110
In this case, there are three possible final configurations:
  • All lamps are OFF
  • Lamps 1, 4, 7, 10 are OFF and lamps 2, 3, 5, 6, 8, 9 are ON.
  • Lamps 1, 3, 5, 7, 9 are OFF and lamps 2, 4, 6, 8, 10 are ON.

二 分析

有n个灯,起始全是亮着的,有4种操作:1.改变所有灯状态 2. 改变奇数号灯状态 3.改变偶数号灯状态 4.改变(3k+1)号灯状态。给定目标状态中部分灯是暗还是亮。问有多少种可能的结果。
同一操作进行两次就相当于没操作。因此只需要枚举2^4=16种情况就行了。由于限制了总操作次数,有两种情况需要直接判为不可能:
1.枚举的是4种操作分别需要(n1, n2, n3, n4)次,而给定的总操作次数小于n1 + n2 + n3 + n4
2.枚举的是4种操作分别需要(n1, n2, n3, n4)次,而给定的总操作次数为n1 + n2 + n3 + n4 + 1


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: lamps
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4188 KB]
   Test 2: TEST OK [0.000 secs, 4188 KB]
   Test 3: TEST OK [0.000 secs, 4188 KB]
   Test 4: TEST OK [0.000 secs, 4188 KB]
   Test 5: TEST OK [0.000 secs, 4188 KB]
   Test 6: TEST OK [0.000 secs, 4188 KB]
   Test 7: TEST OK [0.000 secs, 4188 KB]
   Test 8: TEST OK [0.000 secs, 4188 KB]

All tests OK.

Your program ('lamps') produced all correct answers! This is your submission #4 for this problem. Congratulations!

AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:lamps
*/

#include<cstdio>
#include<vector>
#include<cstring>
#include<set>
#include<fstream>
#include<algorithm>
using namespace std;

int n, c;
vector<int> on;
vector<int> off;
bool mark[4];
bool ori[105], fin[105];
set<string> ans;

bool ok() {
    for(int i = 0; i < on.size(); i++) {
        if(!fin[on[i]]) return false;
    }
    for(int i = 0; i < off.size(); i++) {
        if(fin[off[i]]) return false;
    }
    return true;
}

void make_ans() {
    string s = "";
    for(int i = 0; i < n; i++) {
        if(fin[i]) s += "1";
        else s += "0";
    }
    ans.insert(s);
}

void trans(int idx) {
    switch(idx) {
        case 0:
            for(int i = 0; i < n; i++) fin[i] = !fin[i];
            break;
        case 1:
            for(int i = 0; i < n; i += 2) fin[i] = !fin[i];
            break;
        case 2:
            for(int i = 1; i < n; i += 2) fin[i] = !fin[i];
            break;
        case 3:
            for(int i = 0; i < n; i += 3) fin[i] = !fin[i];
            break;
    }
}

void judge() {
    int min_c = 0;
    for(int i = 0; i < 4; i++) {
        if(mark[i]) min_c++;
    }
    if(min_c > c) return;
    if(c - min_c == 1) return;
    for(int i = 0; i < n; i++) fin[i] = ori[i];
    for(int i = 0; i < 4; i++) {
        if(mark[i]) trans(i);
    }
    if(ok()) {
        make_ans();
    }
}

void dfs(int depth) {
    if(depth == 4) {
        judge();
        return;
    }
    dfs(depth + 1);
    mark[depth] = true;
    dfs(depth + 1);
    mark[depth] = false;
}

int main() {
    freopen("lamps.in", "r", stdin);
    ofstream fout;
    fout.open("lamps.out");
    int id;
    scanf("%d%d%d", &n, &c, &id);
    while(id != -1) {
        on.push_back(id - 1);
        scanf("%d", &id);
    }
    scanf("%d", &id);
    while(id != -1) {
        off.push_back(id - 1);
        scanf("%d", &id);
    }
    for(int i = 0; i < n; i++) ori[i] = true;
    dfs(0);
    if(ans.size() == 0) fout << "IMPOSSIBLE" << endl;
    else {
        set<string>::iterator it;
        for(it = ans.begin(); it != ans.end(); it++) {
            fout << *it << endl;
        }
    }
    fout.close();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值