【POJ】[3279]Fliptile

Problem Description

Loading...

Problem Analysis

This is a relatively hard problem to think of, but it is also a very classic one.
The classic game “Light Up the Bulbs” is this kind.
That is, when you press a switch, this light and the lights above, below, left, and right change state.
Because pressing a switch twice is meaningless,
there are only two states for each switch:
pressed or not pressed. So this problem asks us to output
whether each switch is pressed (1) or not pressed (0).

The problem asks whether all 1s can be turned into 0s through operations.
If not, output IMPOSSIBLE.
If there are multiple operations, output the one that requires the fewest presses (1).
If the counts are equal, output the one with the lexicographically smallest result.
(If there are 00111 and 11100, output 00111.)

Idea:
If we enumerate on/off for every cell, there are $2^{n*m}$ states.
So within the range 1≤M≤15,1≤N≤15, it will definitely explode.
So think about whether it can be optimized.

In fact, once the first row is determined, the press pattern for the next row is necessarily determined,
because the only thing that affects the first row is the corresponding light in the second row.
So if a light in the first row is off, the corresponding switch in the second row should be pressed to turn it on.
And so on to obtain all states.
If the last row is also all on after pressing,
then this solution is valid.
So we can enumerate the first-row operations, which have $2^m$ states.

Code:
To conveniently represent the first-row light operations,
we can use binary to represent whether to operate.

1
2
3
4
0000(0) 表示 否否否否
0001(1) 表示 否否否是
0010(2) 表示 否否是否
0011(3) 表示 否否是是

And so on; a loop can represent all operations.
There are $2^m$ iterations in total.
For each i, you can perform the AND operation i&1 to get the last binary digit.
To determine whether a light is on or off, we can use the XOR operation,
because we have the operation rules

1
2
3
4
0^1==1
1^1==0
0^0==0
1^0==1

That is, XOR with 1 toggles it, and XOR with 0 leaves it unchanged.
This is the same as whether the light is toggled.

After obtaining the first-row operations for each state,
we can judge downward in order to update the answer.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<stdio.h>
#include<string.h>
int map[20][20];
int temp[20][20];
int N,M;
void Pr(int x) {
    for(int i=0; i<M; i++) {
        map[0][i] ^= (x>>(M-1-i))&1;
        if((x>>(M-1-i))&1) {
            if(i>0)
                map[0][i-1] ^= 1;
            if(i<M-1)
                map[0][i+1] ^= 1;
            if(N>1)
                map[1][i] ^= 1;
        }
        printf("%d",(x>>(M-1-i))&1);
        printf("%c",i==M-1?'\n':' ');
    }
    for(int i=1; i<N; i++) {
        for(int j=0; j<M; j++) {
            printf("%d",map[i-1][j]);
            if(map[i-1][j]==1) {
                map[i-1][j] ^= 1;
                map[i][j] ^= 1;
                if(j>0)
                    map[i][j-1] ^= 1;
                if(j<M-1)
                    map[i][j+1] ^= 1;
                if(i<N-1)
                    map[i+1][j] ^= 1;
            }
            printf("%c",j==M-1?'\n':' ');
        }
    }
}
int main() {
    while(scanf("%d %d",&N,&M)!=EOF) {
        for(int i=0; i<N; i++)
            for(int j=0; j<M; j++)
                scanf("%d",&map[i][j]);
        int res=99999999,rest=-1;
        int T=(1<<M),max=T-1;
        while(T--) {
            memcpy(temp,map,sizeof(map));
            int t=T^max;
            int cnt=0;
            for(int i=0; i<M; i++) {
                temp[0][i] ^= (t>>(M-1-i))&1;
                if((t>>(M-1-i))&1) {
                    cnt++;
                    if(i>0)
                        temp[0][i-1] ^= 1;
                    if(i<M-1)
                        temp[0][i+1] ^= 1;
                    if(N>1)
                        temp[1][i] ^= 1;
                }
            }
            for(int i=1; i<N; i++) {
                for(int j=0; j<M; j++) {
                    if(temp[i-1][j]==1) {
                        cnt++;
                        temp[i-1][j] ^= 1;
                        temp[i][j] ^= 1;
                        if(j>0)
                            temp[i][j-1] ^= 1;
                        if(j<M-1)
                            temp[i][j+1] ^= 1;
                        if(i<N-1)
                            temp[i+1][j] ^= 1;
                    }
                }
            }
            int i;
            for(i=0; i<M; i++)
                if(temp[N-1][i]==1)
                    break;
            if(i==M&&cnt<res)
                res=cnt,rest=t;
        }
        if(res==99999999)
            printf("IMPOSSIBLE\n");
        else
            Pr(rest);
    }
    return 0;
}
This post is licensed under CC BY-NC-SA 4.0. If it helped, or if you have anything to say, the comment box below is open.
Page views —
Unique visitors —

Loading...