【高斯消元】Poj 1222:EXTENDED LIGHTS OUT



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.


  翻译:给你一个5*6的初始状态,要求你给出一个5*6的操作矩阵,要求:当操作是1时,代表把棋盘的对应位置和它相邻的地方的状态改变(1变为0,0变为1),0则不进行操作。要求操作矩阵满足操作后棋盘状态全部为0.保证有解且唯一

  提示:按两下和不按是一样的,所以只有按不按,没有按几下的区别。也没有先按后按的区别。

  就是解异或方程。。

  每个点按不按^周围的点按不按^最开始情况=0

  转换一下。。

  周围的点^每个点按不按=最开始情况

  枚举每个点,之后就是喜闻乐见的高斯消元时间了。。(ps.如果消i元素,但是你的f[i][i]=0的话,需要找一行不等于0的行swap一下)

  代码:

  

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5
 6 using namespace std;
 7
 8 int f[6][7],ans[6][7],ga[101][101],cnt=0;
 9
10 void print()
11 {
12     cnt++;
13     printf("PUZZLE #%d\n",cnt);
14     for(int i=1;i<=5;i++)
15     {
16         for(int j=1;j<=6;j++)
17             printf("%d ",ans[i][j]);
18         printf("\n");
19     }
20 }
21
22 void Solve()
23 {
24     for(int i=30;i>=1;i--)
25     {
26         int x=i/6+1,y=i%6;
27         if(!y)    y+=6,x--;
28         ans[x][y]=ga[i][31];
29         for(int j=i+1;j<=30;j++)
30             if(ga[i][j]){
31                 int x1=j/6+1,y1=j%6;
32                 if(!y1) y1+=6,x1--;
33                 ans[x][y]=ans[x][y]^ans[x1][y1];
34             }
35     }
36 }
37
38 void swapp(int l,int r)
39 {
40     for(int i=1;i<=31;i++)
41         swap(ga[l][i],ga[r][i]);
42 }
43
44 void find(int n)
45 {
46     for(int i=n+1;i<=30;i++)
47         if(ga[i][n]){swapp(i,n);return;}
48 }
49
50 void Guass()
51 {
52     for(int i=2;i<=30;i++)//消第几个元
53     {
54         if(!ga[i-1][i-1])find(i-1);
55         if(!ga[i-1][i-1])continue;
56         for(int j=i;j<=30;j++)//第几个方程
57         {
58             if(!ga[j][i-1])continue;
59             for(int k=i;k<=31;k++)//方程的第几项
60                 ga[j][k]=ga[j][k]^ga[i-1][k];
61         }
62     }
63     Solve();
64 }
65
66 void set()
67 {
68     for(int i=1;i<=5;i++)
69         for(int j=1;j<=6;j++){
70             ga[(i-1)*6+j][31]=f[i][j];
71             ga[(i-1)*6+j][(i-1)*6+j]=1;                //自己和上下左右是对自己有影响的点
72             if(j!=1) ga[(i-1)*6+j][(i-1)*6+j-1]=1;
73             if(j!=6) ga[(i-1)*6+j][(i-1)*6+j+1]=1;
74             if(i!=5) ga[(i-1)*6+j][i*6+j]=1;
75             if(i!=1) ga[(i-1)*6+j][(i-2)*6+j]=1;
76         }
77     return;
78 }
79
80 int main()
81 {
82     int T;
83     scanf("%d",&T);
84     while(T--)
85     {
86         for(int i=1;i<=5;i++)
87             for(int j=1;j<=6;j++)
88                 scanf("%d",&f[i][j]);
89         set();
90         Guass();
91         print();
92         memset(f,0,sizeof(f));
93         memset(ga,0,sizeof(ga));
94         memset(ans,0,sizeof(ans));
95     }
96     return 0;
97 }

  

  

时间: 2024-11-05 17:31:17

【高斯消元】Poj 1222:EXTENDED LIGHTS OUT的相关文章

POJ 1222 EXTENDED LIGHTS OUT 高斯消元

点击打开链接 EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6492   Accepted: 4267 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 butt

POJ 1222 extended lights out 高斯消元 板子题

题目链接:http://poj.org/problem?id=1222 题目描述:其实就是开关问题, 按下按钮会影响当前和周围的四个按钮, 问关闭所有灯的方案 解题思路:以前用搜索做过, 那时候是刚刚接触ACM的时候, 当时劲头真足啊, 这个解释的很好:http://blog.csdn.net/u013508213/article/details/47263183 代码: #include <iostream> #include <cstdio> #include <cstr

POJ 1222 EXTENDED LIGHTS OUT(高斯消元解XOR方程组)

http://poj.org/problem?id=1222 题意:现在有5*6的开关,1表示亮,0表示灭,按下一个开关后,它上下左右的灯泡会改变亮灭状态,要怎么按使得灯泡全部处于灭状态,输出方案,1表示按,0表示不按. 思路:每个开关最多只按一次,因为按了2次之后,就会抵消了. 可以从结果出发,也就是全灭状态怎么按能变成初始状态. 用3*3来举个例子,$X\left ( i,j \right )$表示这些开关是按还是不按,那么对于第一个开关,对它有影响的就只有2.4这两个开关,所以它的异或方程

POJ 1222 EXTENDED LIGHTS OUT(高斯消元)

[题目链接] http://poj.org/problem?id=1222 [题目大意] 给出一个6*5的矩阵,由0和1构成,要求将其全部变成0,每个格子和周围的四个格子联动,就是说,如果一个格子变了数字,周围四格都会发生变化,变化即做一次与1的异或运算,输出每个格子的操作次数. [题解] 高斯消元练手题,对于每个格子的最终情况列一个方程,一共三十个方程三十个未知数,用高斯消元求解即可. [代码] #include <cstdio> #include <algorithm> #in

[POJ 1222] EXTENDED LIGHTS OUT

题目 http://acm.pku.edu.cn/JudgeOnline/problem?id=1222 描述 给你一个5行6列的矩阵分别表示30个灯,矩阵map[i][j]为1表示灯亮着, 0表示灯没亮. 要求你输出解决方案. press[i][j]为1表示按一下,0表示不按.使得最后状态为所有灯都熄灭. 分析 高斯消元法(Gaussian elimination method) => 对于每个格子 map[i][j], 能影响他的格子为 (i-1, j), (i, j-1), (i+1, j

poj 1222 EXTENDED LIGHTS OUT 高斯消元法

EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6872   Accepted: 4532 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 eac

【高斯消元】 POJ 1222 EXTENDED LIGHTS OUT

通道 题意:有一个5*6的矩阵,每个位置都表示按钮和灯,1表示亮,0表示灭.每当按下一个位置的按钮,它和它周围灯的状态全部翻转,问在这样的一个方阵中按下哪些按钮可以把整个方阵都变成灭的,这时1表示按了,0表示没按 代码: #include <cstdio> #include <cstring> #include <vector> #include <cmath> #include <algorithm> using namespace std;

数学 --- 高斯消元 POJ 1830

开关问题 Problem's Link: http://poj.org/problem?id=1830 Mean: 略 analyse: 略增广矩阵:con[i][j]:若操作j,i的状态改变则con[i][j]=1,否则con[i][j]=0. 最后的增广矩阵应该是N*(N+1),最后一列:对比开光的始末状态,若相同则为0,若不同则为1: 最后的解共有三种:1.无解,既出现了一行中前面N个数为0,第N+1的值非0:2.没有第1种情况出现,存在X行数值全为0,则解的个数为2^X;3,没有1,2

[ACM] POJ 2947 Widget Factory (高斯消元)

Widget Factory Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 4436   Accepted: 1502 Description The widget factory produces several different kinds of widgets. Each widget is carefully built by a skilled widgeteer. The time required to