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

46 篇文章 0 订阅
23 篇文章 0 订阅

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

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

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

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

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

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 <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 2

const 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值