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

来源:互联网 发布:服务器ip端口是什么 编辑:程序博客网 时间:2024/06/05 02:44

题目大意:给你一个初始矩阵(里面每个元素代表一个开关),改变一个开关就可以使得在其上、下、左、右的开关的状态都改变。问若使得所有开关均关闭(开关开为1,关为0),则需要改变哪些开关,并给出要改变的开关的矩阵图,里面的元素为1的表示要改变的开关,为0的表示不要变的开关。

解题思路:对于每一个开关来说,和它相连的系数为1,否则系数为0。

然后以每个开关的状态为方程列出一个30*30的关系矩阵,用高斯消元解除系数就可以了啊。

因为开关的状态只有两个所以相当于对2取余。

EXTENDED LIGHTS OUT
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6528 Accepted: 4293

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

20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0

Sample Output

PUZZLE #11 0 1 0 0 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1
#include <algorithm>#include <iostream>#include <stdlib.h>#include <string.h>#include <iomanip>#include <stdio.h>#include <string>#include <queue>#include <cmath>#include <stack>#include <map>#include <set>#define eps 1e-10///#define M 1000100#define LL __int64///#define LL long long#define INF 0x7fffffff#define PI 3.1415926535898#define zero(x) ((fabs(x)<eps)?0:x)#define mod 2const int maxn = 340;using namespace std;int a[maxn][maxn];int x[maxn];int equ, var;char str[maxn];int LCM(int a, int b){    return (a/(__gcd(a, b)))*b;}int Gauss(){    int row, col, max_r;    row = col = 0;    while(row < equ && col < var)    {        max_r = row;        for(int i = row+1; i < equ; i++)            if(abs(a[i][col]) > abs(a[max_r][col])) max_r = i;        if(max_r != row)            for(int j = col; j <= var; j++) swap(a[row][j], a[max_r][j]);        if(a[row][col] == 0)        {            col++;            continue;        }        for(int i = row+1; i < equ; i++)        {            if(a[i][col] == 0) continue;            int l = LCM(abs(a[row][col]), abs(a[i][col]));            int ta = l/a[i][col];            int tb = l/a[row][col];            if(ta*tb < 0) tb *= -1;///判断是否异号            for(int j = col; j <= var; j++)                a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod + mod)%mod;        }        row++;        col++;    }    for(int i = row; i < equ; i++)///无解的情况;        if(a[i][col] != 0) return -1;    if(row < var)///多组解的情况        return var-row;    for(int i = var-1; i >= 0; i--)///唯一解的情况,根据上三角阵,迭代求出每一次的值    {        int tmp = a[i][var];        for(int j = i+1; j < var; j++)            if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod + mod)%mod;        while(tmp%a[i][i] != 0)            tmp += mod;        x[i] = tmp/a[i][i]%mod;    }    return 0;}void init(){    equ = var = 30;    memset(x, 0, sizeof(x));    memset(a, 0, sizeof(a));    for(int i = 0; i < 5; i++)    {        for(int j = 0; j < 6; j++)        {            int t = i*6+j;            a[t][t] = 1;            if(i-1 >= 0) a[(i-1)*6+j][t] = 1;            if(i+1 < 5)  a[(i+1)*6+j][t] = 1;            if(j-1 >= 0) a[i*6+j-1][t] = 1;            if(j+1 < 6)  a[i*6+j+1][t] = 1;        }    }}int main(){    int T;    cin >>T;    int Case = 1;    while(T--)    {        init();        for(int i = 0; i < 30; i++) scanf("%d",&a[i][30]);        Gauss();        cout<<"PUZZLE #"<<Case++<<endl;        for(int i = 0; i < 30; i++)        {            cout<<x[i];            if((i+1)%6 == 0)                cout<<endl;            else                cout<<" ";        }    }    return 0;}


0 0
原创粉丝点击