USACO Section 2.2 Party Lamps(dfs)

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 个灯,依次编号从 1 至 n ,现在只有 4 个操作,操作 1 改变所有的灯状态,操作 2 改变编号为基数的灯状态,操作 3 改变编号为偶数的灯的状态,操作 4 改变编号为 3*k+1 的灯的状态。现在让你操作 c 次之后使得给定的一些灯是亮的和一些灯是关着的。输出所有 c 次操作后满足条件的的灯的状态。

分析:只有4个操作,直接DFS枚举。

View Code
  1 /*
  2   ID: dizzy_l1
  3   LANG: C++
  4   TASK: lamps
  5 */
  6 #include<iostream>
  7 #include<algorithm>
  8 #include<cstring>
  9 #include<string>
 10 #include<cstdio>
 11 #include<cmath>
 12 #define MAXN 110
 13 
 14 using namespace std;
 15 
 16 string tans[300];
 17 int ans[300][MAXN],_open[MAXN],_close[MAXN],a[MAXN];
 18 int n,c,cnt;
 19 
 20 bool judge()
 21 {
 22     int i;
 23     i=0;
 24     while(_open[i]!=-1)
 25         if(a[_open[i++]-1]!=1)
 26             return false;
 27     i=0;
 28     while(_close[i]!=-1)
 29         if(a[_close[i++]-1]!=0)
 30             return false;
 31     return true;
 32 }
 33 
 34 void DFS(int p)
 35 {
 36     int i,t[MAXN];
 37     if(p==c)
 38     {
 39         if(judge())
 40         {
 41             for(i=0; i<n; i++) ans[cnt][i]=a[i];
 42             cnt++;
 43         }
 44         return ;
 45     }
 46 
 47     for(i=0; i<n; i++) t[i]=a[i];
 48     for(i=0; i<n; i++)
 49         a[i]^=1;
 50     DFS(p+1);
 51 
 52     for(i=0; i<n; i++) a[i]=t[i];
 53     for(i=0; i<n; i+=2)
 54         a[i]^=1;
 55     DFS(p+1);
 56 
 57     for(i=0; i<n; i++) a[i]=t[i];
 58     for(i=1; i<n; i+=2)
 59         a[i]^=1;
 60     DFS(p+1);
 61 
 62     for(i=0; i<n; i++) a[i]=t[i];
 63     for(i=0; i<n; i+=3)
 64         a[i]^=1;
 65     DFS(p+1);
 66 }
 67 
 68 void output()
 69 {
 70     int i,j;
 71     for(i=0;i<cnt;i++)
 72     {
 73         tans[i]="";
 74         for(j=0;j<n;j++)
 75             tans[i]+=(ans[i][j]+'0');
 76     }
 77     sort(tans,tans+cnt);
 78     cout<<tans[0]<<endl;
 79     for(i=0,j=1;j<cnt;j++)
 80         if(tans[i]!=tans[j])
 81         {
 82             i=j;
 83             cout<<tans[i]<<endl;
 84         }
 85 }
 86 
 87 int main()
 88 {
 89     int i,j;
 90     freopen("lamps.in","r",stdin);
 91     freopen("lamps.out","w",stdout);
 92     while(scanf("%d",&n)==1)
 93     {
 94         scanf("%d",&c);
 95         i=0;
 96         cnt=0;
 97         do
 98         {
 99             scanf("%d",&_open[i]);
100         }
101         while(_open[i++]!=-1);
102         i=0;
103         do
104         {
105             scanf("%d",&_close[i]);
106         }
107         while(_close[i++]!=-1);
108         while(c>4) c-=2;
109         for(i=0;i<n;i++)a[i]=1;
110         DFS(0);
111         if(cnt==0)
112         {
113             printf("IMPOSSIBLE\n");
114             continue;
115         }
116         output();
117     }
118     return 0;
119 }
posted @ 2012-09-02 21:02  mtry  阅读(393)  评论(0编辑  收藏  举报