高斯消元训练


EXTENDED LIGHTS OUT

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 6443Accepted: 4229

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



代码实现:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int maxn=100;

int a[maxn][maxn+1],x[maxn];//a 是系数矩阵和增广矩阵,x 是最后存放的解
// a[][maxn]中存放的是方程右面的值(bi)
int equ,var;//equ 是系数阵的行数,var 是系数矩阵的列数(变量的个数)
int free_num,ans=100000000;
int abs1(int num) //取绝对值
{
    if (num>=0) return num;
    else
        return -1*num;
}
void Debug() //调试输出,看消元后的矩阵值,提交时,就不用了
{
    int i, j;
    for (i = 0; i < equ; i++)
    {
        for (j = 0; j < var + 1; j++)
        {
            cout << a[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
inline int gcd(int a, int b) //最大公约数
{
    int t;
    while (b != 0)
    {
        t = b;
        b = a % b;
        a = t;
    }
    return a;
}
inline int lcm(int a, int b) //最小公倍数
{
    return a * b / gcd(a, b);
}
void swap(int &a,int &b)
{
    a=a^b;    //交换2 个数
    b=a^b;
    a=a^b;
}
int Gauss()
{
    int k,col = 0; //当前处理的列
    for(k = 0; k < equ && col < var; ++k,++col)
    {
        int max_r = k;
        for(int i = k+1; i < equ; ++i)
            if(a[i][col] > a[max_r][col])
                max_r = i;
        if(max_r != k)
        {
            for(int i = k; i < var + 1; ++i)
                swap(a[k][i],a[max_r][i]);
        }
        if(a[k][col] == 0)
        {
            k--;
            continue;
        }
        for(int i = k+1; i < equ; ++i)
        {
            if(a[i][col] != 0)
            {
                int LCM = lcm(a[i][col],a[k][col]);
                int ta = LCM/a[i][col], tb = LCM/a[k][col];
                if(a[i][col]*a[k][col] < 0)
                    tb = -tb;
                for(int j = col; j < var + 1; ++j)
                    a[i][j] = ( (a[i][j]*ta)%2 - (a[k][j]*tb)%2 + 2 ) % 2; //a[i][j]只有0 和1 两种状态
            }
        }
    }
//上述代码是消元的过程,行消元完成
//解下来2 行,判断是否无解
//注意K 的值,k 代表系数矩阵值都为0 的那些行的第1 行
    for(int i = k; i < equ; ++i)
        if(a[i][col] != 0) return -1; // 无解返回-1
///Debug();
//唯一解或者无穷解,k<=var
//var-k==0 唯一解;var-k>0 无穷多解,自由解的个数=var-k
//能执行到这,说明肯定有解了,无非是1 个和无穷解的问题。
//下面这几行很重要,保证秩内每行主元非0,且按对角线顺序排列,就是检查列
    for(int i = 0; i <equ; ++i)//每一行主元素化为非零
        if(!a[i][i])
        {
            int j;
            for(j = i+1; j<var; ++j)
                if(a[i][j])
                    break;
            if(j == var)
                break;
            for(int k = 0; k < equ; ++k)
                swap(a[k][i],a[k][j]);
        }
// ----处理保证对角线主元非0 且顺序,检查列完成
// free_num=k;
    if (var-k>0)
    {
//无穷多解,先枚举解,然后用下面的回带代码进行回带;
//这里省略了下面的回带的代码;不管唯一解和无穷解都可以回带,只不过无穷解
//回带时,默认为最后几个自由变元=0 而已。

    }
    if (var-k==0)//唯一解时
    {
//下面是回带求解代码,当无穷多解时,最后几行为0 的解默认为0;
        for(int i = k-1; i >= 0; --i) //从消完元矩阵的主对角线非0 的最后1 行,开始往
//回带
        {
            int tmp = a[i][var] % 2;
            for(int j = i+1; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊,所以后面的解对前面的解有影响。
                if(a[i][j] != 0)
                    tmp = ( tmp - (a[i][j]*x[j])%2 + 2 ) % 2;
//if (a[i][i]==0) x[i]=tmp; //最后的空行时,即无穷解得
//else
            x[i] = (tmp/a[i][i]) % 2; //上面的正常解
        }
        return 0;
//回带结束了
    }
}

int main()
{
    int casenum=1;
    int T;
    scanf("%d",&T);
    equ=30;
    var=30;
    while(T--)
    {
        memset(a,0,sizeof(a));
        memset(x,0,sizeof(x));
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<6;j++)
            {
                if(i-1>=0)
                    a[6*(i-1)+j][6*i+j]=1;
                if(i+1<=4)
                    a[6*(i+1)+j][6*i+j]=1;
                if(j-1>=0)
                    a[6*i+j-1][6*i+j]=1;
                if(j+1<=5)
                    a[6*i+j+1][6*i+j]=1;
                a[6*i+j][6*i+j]=1;
                scanf("%d",&a[6*i+j][30]);
            }
        }

        /*for(int i=0;i<30;i++)
        {
            for(int j=0;j<30;j++)
            {
                cout<<a[i][j]<<" ";
            }cout<<endl;
        }*/

        free_num=Gauss();

        printf("PUZZLE #%d\n",casenum++);
        for(int i=0;i<var;i++)
        {
            if((i+1)%6==0)
                printf("%d\n",x[i]);
            else
                printf("%d ",x[i]);
        }
    }
    return 0;
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值