高斯消元 poj-1222

http://poj.org/problem?id=1222

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

高斯消元模型,30个灯的状态相当于30个方程,30个未知量,每个等的状态与它周围的4个灯状态相关联

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 using namespace std;
 7 int equ=30, var=30; //增广矩阵行数为equ,列数为var+1
 8 int a[100][100]; //增广矩阵
 9 int x[100]; //解集
10
11 void Gauss(){
12     int maxr; //记录主元行数
13     int r; //行
14     int c; //列
15     int i, j, k;
16     c = 0;
17     for(r=0; r<equ&&c<var; r++,c++){
18         maxr = r;
19         for(i=r+1; i<equ; i++){
20             if(abs(a[i][c])>abs(a[maxr][c]))
21                 maxr = i;
22         }
23         if(maxr!=r){ //交换行
24             for(i=0; i<var+1; i++)
25                 swap(a[maxr][i],a[r][i]);
26         }
27         for(j=r+1; j<equ; j++){
28             if(a[j][c]){
29                 for(i=c; i<var+1; i++){
30                     a[j][i] = a[j][i]^a[r][i];
31                 }
32             }
33         }
34         for(i=var-1; i>=0; i--){
35             x[i] = a[i][var]; //b
36             for(j=var-1; j>i; j--){
37                 x[i] = x[i]^(a[i][j]&&x[j]);
38             }
39         }
40     }
41 }
42
43 int main(){
44     int T, i, j, k, cas = 1;
45     cin>>T;
46     while(T--){
47         memset(a, 0, sizeof(a));
48         memset(x, 0, sizeof(x));
49         for(i=0; i<5; i++){
50             for(j=0; j<6; j++){
51                 int s = i*6+j;
52                 a[s][s] = 1;
53                 if(i>0)
54                     a[s-6][s] = 1;
55                 if(i<4)
56                     a[s+6][s] = 1;
57                 if(j>0)
58                     a[s-1][s] = 1;
59                 if(j<5)
60                     a[s+1][s] = 1;
61             }
62         }
63         for(i=0; i<30; i++){
64             cin>>a[i][30];
65         }
66        // cout<<"Gauss"<<endl;
67         Gauss();
68         printf("PUZZLE #%d\n",cas++);
69         for(i=0; i<30; i++){
70             if(i%6==5)
71                 printf("%d\n",x[i]);
72             else
73                 printf("%d ",x[i]);
74         }
75     }
76     return 0;
77 }
时间: 2024-10-06 16:50:52

高斯消元 poj-1222的相关文章

数学 --- 高斯消元 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

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【异或高斯消元|二进制状态枚举】

题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题解:首先可以用高斯消元来做,对于每个点,我们列出一个方程,左边是某个点和它相邻的点,他们的异或值等于右边的值(灯亮为1 ,灯灭为0),然后求一个异或高斯消元就可以了.可以用bitset优化,或者__int128优化(其实unsigned就可以了). 还可以枚举第一行的按开关的状态共有1<<6中状态

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 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9612   Accepted: 6246 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 EXTENDED LIGHTS OUT 1222【高斯消元】

Language: Default EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7672   Accepted: 4996 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 row

【高斯消元】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) neighbo

POJ - 1222: EXTENDED LIGHTS OUT (开关问题-高斯消元)

pro:给定5*6的灯的状态,如果我们按下一个灯的开关,它和周围4个都会改变状态.求一种合法状态,使得终状态全为关闭: sol:模2意义下的高斯消元. 终于自己手打了一个初级板子. #include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; int a[40][40],ans[40]; int x[5]={0,0,0,1,-1}; int y[5]={0,1,-1,0,0};