PekingUniversityOJ No.1222

本文最后更新于:5 年前

这是一个很经典的小游戏,在一个m*n的格子,每按一下一个格子,包括自己这个格子和上下左右共5个格子的灯都会转变状态(亮->暗/暗->亮) 记得不久之前和薛老师去玩一个以“未闻花名”为主题的密室里,也有这个游戏,我当时就瞎捣鼓,莫名其妙全点灭了,没想到这次做题竟然又遇到了这个游戏!

Description

In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual puzzle has 5 rows of 5 buttons each). Each button has a light. When a button is pressed, that button and each of its (up to four) neighbors above, below, right and left, has the state of its light reversed. (If on, the light is turned off; if off, the light is turned on.) Buttons in the corners change the state of 3 buttons; buttons on an edge change the state of 4 buttons and other buttons change the state of 5. For example, if the buttons marked X on the left below were to be pressed,the display would change to the image on the right.

The aim of the game is, starting from any initial set of lights on in the display, to press buttons to get the display to a state where all lights are off. When adjacent buttons are pressed, the action of one button can undo the effect of another. For instance, in the display below, pressing buttons marked X in the left display results in the right display.Note that the buttons in row 2 column 3 and row 2 column 5 both change the state of the button in row 2 column 4,so that, in the end, its state is unchanged.

Note:

  1. It does not matter what order the buttons are pressed.
  2. If a button is pressed a second time, it exactly cancels the effect of the first press, so no button ever need be pressed more than once.
  3. As illustrated in the second diagram, all the lights in the first row may be turned off, by pressing the corresponding buttons in the second row. By repeating this process in each row, all the lights in the first
    four rows may be turned out. Similarly, by pressing buttons in columns 2, 3 ?, all lights in the first 5 columns may be turned off.
    Write a program to solve the puzzle.
    Input

The first line of the input is a positive integer n which is the number of puzzles that follow. Each puzzle will be five lines, each of which has six 0 or 1 separated by one or more spaces. A 0 indicates that the light is off, while a 1 indicates that the light is on initially.
Output

For each puzzle, the output consists of a line with the string: “PUZZLE #m”, where m is the index of the puzzle in the input file. Following that line, is a puzzle-like display (in the same format as the input) . In this case, 1’s indicate buttons that must be pressed to solve the puzzle, while 0 indicate buttons, which are not pressed. There should be exactly one space between each 0 or 1 in the output puzzle-like display.
Sample Input

2
0 1 1 0 1 0
1 0 0 1 1 1
0 0 1 0 0 1
1 0 0 1 0 1
0 1 1 1 0 0
0 0 1 0 1 0
1 0 1 0 1 1
0 0 1 0 1 1
1 0 1 1 0 0
0 1 0 1 0 0
Sample Output

PUZZLE #1
1 0 1 0 0 1
1 1 0 1 0 1
0 0 1 0 1 1
1 0 0 1 0 0
0 1 0 0 0 0
PUZZLE #2
1 0 0 1 1 1
1 1 0 0 0 0
0 0 0 1 0 0
1 1 0 1 0 1
1 0 1 1 0 1
Source

Greater New York 2002

总体思路:二进制枚举+贪心

首先介绍一下二进制枚举算法

这里要了解位运算,尤其是按位与还有移位运算。

移位运算

a<<b运算就是,将二进制a向左移b位,也就是a右边添上b个0。如2的二进制是10,向左移一位就是100,则为十进制4,不难发现左移n位,及等价于乘上2^n
那么下面的代码即可遍历0到2^(n-1)之内的数

1
for(int i = 0; i < (1 << n); i++);

按位与运算

运算规则:0&0=0; 0&1=0; 1&0=0; 1&1=1;
对于某个二进制数10100

10100&00001=00000
10100&00010=00000
10100&00100=00100
10100&01000=00000
10100&10000=10000
举上面的例子其实就是更好得解释二进制枚举
下面展示较完整的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
int n;
cin >> n;
for(int i = 0; i < (1<<n); i++) //从0~2^n-1个状态
{
for(int j = 0; j < n; j++) //遍历二进制的每一位
{
if(i & (1 << j))//判断二进制第j位是否存在
{
.............//如果第j位存在,则改变相应状态变量【做题用】
}
}
printf("\n");
}

对于这个题目,我们其实只要二进制枚举第一行灯的按压状态,而之后的每一行直接运用贪心算法即可,即上一行哪个灯亮,就按对应下一行的灯,保证上一行的灯全暗。
其实有个小疑问,是否会出现一种初始状态,出现无解的情况呢。
下面附上全部代码,由于北大oj编译器版本较老,声明的数组维度中不能出现变量,所以代码oj是显示compile error的,但思路和解决方法我认为是没问题的。

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
#include<cstdio>
#include<cstring>
#include <iostream>
using namespace std;
int press[5][6];
int puzzle[5][6];
int countt=0;
void tanxin(int puzzle[5][6]){ //贪心算法
int i,j,flag=1;
for(i=0;i<4;i++){
for(j=0;j<6;j++){
if(puzzle[i][j]==1){
press[i+1][j]=1;
puzzle[i+1][j]=(puzzle[i+1][j]+1)%2;
if(j!=0){ //第一列
puzzle[i+1][j-1]=(puzzle[i+1][j-1]+1)%2;
}
if(j!=5){ //最后一列
puzzle[i+1][j+1]=(puzzle[i+1][j+1]+1)%2;
}
if(i!=3){ //倒数第二行
puzzle[i+2][j]=(puzzle[i+2][j]+1)%2;
}
}
}
}
for(j=0;j<6;j++){ //仅需判断最后一行是否全0即可,因为是贪心算法做的
if(puzzle[4][j]==1){
flag=0;
}
}
if(flag){
countt++;
std::cout<<"PUZZLE #"<<countt<<endl;
for(i=0;i<5;i++){
for(j=0;j<6;j++){

std::cout<<press[i][j]<<' ';

}
std::cout<<endl;
}
}

}
void solve(int map[][6]){ //枚举第一行,贪心剩余几行
int i;
int modd;
for(i=0;i<64;i++){ //i<(1<<6)
memcpy(puzzle,map,sizeof(puzzle));
memset(press,0,sizeof(press));
for(modd=0;modd<6;modd++){
if(i&(1<<modd)){//1<<mod 除了向左移的那一位1,剩余的都是0,所以我们就可以得到那一位是不是1
puzzle[0][modd]=(1+puzzle[0][modd])%2;
puzzle[1][modd]=(1+puzzle[1][modd])%2;
if(modd!=0){
puzzle[0][modd-1]=(1+puzzle[0][modd-1])%2;
}
if(modd!=5){
puzzle[0][modd+1]=(1+puzzle[0][modd+1])%2;
}
press[0][modd]=1;
}
}
tanxin(puzzle); //判断
}
}
int main(int argc, const char * argv[]) {
int times=0;
int m,i,j;
std::cin>>times;
m=5*times;
int puzzlee[m][6];
for(i=0;i<m;i++){
for(j=0;j<6;j++){
std::cin>>puzzlee[i][j];
}
}

for(i=0;i<times;i++)
solve(puzzlee+i*5); //每一次迭代,指向puzzle数组的next5行
return 0;
}

总结

因为是课上老师捎带提过的题目,思路很快就出来了,但在coding阶段主要就遇到了原来不曾用过的二进制枚举问题。我一开始的想法是通过把第一行六列所有的组合通过一个状态矩阵的判重循环出来,但其实有很大的漏洞。后来我就想到我可以求{0,1,2,3,4,5}的幂集,即构造一个二叉树,左右子树分别对应“取,舍”两种状态,然后分别对6个元素一一做取舍,最后的子树即位这2^6个集合…但,有点麻烦。求出幂集后还得变为相应二进制状态。最后查阅资料学习了二进制枚举,解决了这道题。


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!