EXTENDED LIGHTS OUT 题解

传送门

题目描述

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.

题目翻译

给你一副5*6的01图,1表示灯亮着,0表示灯灭着。现在,你可以按动一些地方,每次按动时当前位置和当前位置的上,下,左,右位置的灯的状态会发生反转,那么请你输出在哪些位置上按下,能够使所有灯都灭掉。

解题思路

 显而易见,能够影响某一个位置的灯的状态的地方只有五个——以这个灯为中心的十字,那么,我们如果已经确定了第一行,就可以根据第一行的灯的亮灭情况和按动的情况直接推出第二行的情况,以此类推,我们根据前一行的状态一直推到最后一行,然后检查最后一行的灯能够能被完全暗灭。所以,我们直接枚举第一行的状态,然后推到最后一行,然后检查最后一行的灯是否全灭。复杂度为 O(5 × 2 ^ 6)。

F(l,r)=\left\{\begin{matrix} 
   A_{l}&l=r; \\ 
   F(l, r-1)\ mod A_{r}& l<r. 
  \end{matrix}\right.

代码(据说高斯消元也行qwq)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n;
 7 int ans[10][10],map[10][10];
 8 inline void ini(int num){
 9     for(register int i=1;i<=6;i++){
10         ans[1][i]=num&1;
11         num>>=1;
12     }
13 }
14 inline int calc(int x,int y){
15     return ans[x-1][y]+ans[x-2][y]+ans[x-1][y-1]+ans[x-1][y+1];
16 }
17 inline bool check(){
18     for(register int i=1;i<=6;i++){
19         int c=ans[5][i-1]+ans[5][i]+ans[5][i+1]+ans[4][i];
20         if((c&1)!=map[5][i])return 0;
21     }
22     return 1;
23 }
24 int main(){
25     cin>>n;
26     for(register int pu=1;pu<=n;pu++){
27         memset(map,0,sizeof(map));
28         memset(ans,0,sizeof(ans));
29         for(register int i=1;i<=5;i++){
30             for(register int j=1;j<=6;j++){
31                 scanf("%d",&map[i][j]);
32             }
33         }
34         int flag=0;
35         printf("PUZZLE #%d\n",pu);
36         for(register int ii=0;ii<(1<<6);ii++){
37             ini(ii);
38             for(register int i=2;i<=5;i++){
39                 for(register int j=1;j<=6;j++){
40                     int c=calc(i,j);
41                     ans[i][j]=map[i-1][j]^(c&1);
42                 }
43             }
44             if(check()){
45                 for(register int i=1;i<=5;i++){
46                     for(register int j=1;j<=6;j++){
47                         printf("%d ",ans[i][j]);
48                     }
49                     putchar('\n');
50                 }
51                 flag=1;
52             }
53             if(flag)break;
54         }
55     }
56 } 

猜你喜欢

转载自www.cnblogs.com/Fang-Hao/p/9089921.html