【POJ】1222 EXTENDED LIGHTS OUT(高斯消元)
http://poj.org/problem?id=1222
竟然我理解了两天。。。。。
首先先来了解异或方程组(或者说mod2方程组,modk的话貌似可以这样拓展出来)
对于一些我们需要求出的变量a[1~n],我们现在知道n个方程组(有解的情况下),每个方程均是类似原版消元那样带了个系数的,只不过这个系数只有0和1,那么我们第i个方程用x[i, 1~n]表示a[1~n]的系数,然后x[n+1]为这个方程的右式
那么这些方程组是这样的
(x[1,1]*a[1])^(x[1,2]*a[2])^...^(x[1,n]*a[n])=x[1, n+1]
(x[2,1]*a[1])^(x[2,2]*a[2])^...^(x[2,n]*a[n])=x[2, n+1]
...
(x[n,1]*a[1])^(x[n,2]*a[2])^...^(x[n,n]*a[n])=x[n, n+1]
而我们知道,异或操作有交换律、结合律。那么对于有一个相同的项,我们要消掉这个项,得到一个相同的方程,我们直接方程异或消掉即可。也就是说,例如两个方程
(x[1,1]*a[1])^(x[1,2]*a[2])^...^(x[1,n]*a[n])=x[1, n+1]
(x[2,1]*a[1])^(x[2,2]*a[2])^...^(x[2,n]*a[n])=x[2, n+1]
当x[1, 1]=x[2, 1]=1时,我们要消掉x[2, 1],那么我们将这两个式子的所有项都异或就行了,原理就是a=c, b=d, a^b=c^d
然后就能得出个倒三角,最后回代就行了(因为前边的系数都是1了,所以不需要对A[i][i]进行操作)
在找每一列的矩阵时,我们注意只需要找到某一个方程的这一列的系数是1就行了,不需要最大(本来就没有最大),就能消掉所有这一列=1的方程。
然后注意回代的时候系数不是1的就不要异或了(因为本来就没用这个元素啊)
然后记住每次清空矩阵啊!!!我一直以为是我的思路错了,调试了好久,原来是数组没清。。。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=35;
typedef int mtx[N][N];
mtx a;
void gauss(mtx A, int n) {
for1(i, 1, n) {
int now=i;
while(!A[now][i] && now<=n) ++now;
for1(j, 1, n+1) swap(A[now][j], A[i][j]);
for1(j, i+1, n) if(A[j][i])
for1(k, i, n+1) A[j][k]^=A[i][k];
}
for3(i, n, 1)
for1(j, i+1, n) if(A[i][j]) A[i][n+1]^=A[j][n+1];
}
int main() {
int cs=getint();
for1(ttt, 1, cs) {
CC(a, 0);
for1(i, 1, 30) {
read(a[i][31]);
a[i][i]=1;
if(i%6!=1) a[i][i-1]=1;
if(i%6!=0) a[i][i+1]=1;
if(i>6) a[i][i-6]=1;
if(i<25) a[i][i+6]=1;
}
gauss(a, 30);
printf("PUZZLE #%d\n", ttt);
for1(i, 1, 30) {
printf("%d ", a[i][31]); if(i%6==0) puts("");
}
}
return 0;
}
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 Sample Output PUZZLE #1 Source |
【POJ】1222 EXTENDED LIGHTS OUT(高斯消元)的更多相关文章
- POJ 1222 EXTENDED LIGHTS OUT (高斯消元)
题目链接 题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操作使灯全灭? 题解:这个问题是很经典的高斯消元问题.同一个按钮最多只能被按一次,因为 ...
- POJ 1222 EXTENDED LIGHTS OUT [高斯消元XOR]
题意: $5*6$网格里有一些灯告诉你一开始开关状态,按一盏灯会改变它及其上下左右的状态,问最后全熄灭需要按那些灯,保证有解 经典问题 一盏灯最多会被按一次,并且有很明显的异或性质 一个灯作为一个方程 ...
- poj1222 EXTENDED LIGHTS OUT 高斯消元||枚举
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8481 Accepted: 5479 Description In an ...
- POJ1222 EXTENDED LIGHTS OUT 高斯消元 XOR方程组
http://poj.org/problem?id=1222 在学校oj用搜索写了一次,这次写高斯消元,haoi现场裸xor方程消元没写出来,真实zz. #include<iostream> ...
- POJ 1222【异或高斯消元|二进制状态枚举】
题目链接:[http://poj.org/problem?id=1222] 题意:Light Out,给出一个5 * 6的0,1矩阵,0表示灯熄灭,反之为灯亮.输出一种方案,使得所有的等都被熄灭. 题 ...
- POJ 1222 熄灯问题【高斯消元】
<题目链接> 题目大意: 有一个5*6的矩阵,每一位是0或者1. 没翻转一位,它的上下左右的数字也为改变.(0变成1,1变成0).要把矩阵中所有的数都变成0.求最少翻转次数的方案,输出矩阵 ...
- [poj1222]EXTENDED LIGHTS OUT(高斯消元)
题意:每个灯开启会使自身和周围的灯反转,要使全图的灯灭掉,判断灯开的位置. 解题关键:二进制高斯消元模板题. 复杂度:$O({n^3})$ #include<cstdio> #includ ...
- EXTENDED LIGHTS OUT (高斯消元)
In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each (the actual ...
- POJ 1681---Painter's Problem(高斯消元)
POJ 1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...
- POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
POJ 1222 EXTENDED LIGHTS OUT 今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗, ...
随机推荐
- DAY3敏捷冲刺
站立式会议 工作安排 (1)服务器配置 (2)数据库配置 燃尽图 燃尽图有误,已重新修改,先贴卡片的界面,后面补修改后燃尽图 代码提交记录
- 最长回文子串计算(fail)
题意: 给定一个字符串s,在s中找到最长的回文子字符串.您可以假设s的最大长度为1000. 例子: 输入: “babad” 输出: “bab” 注: “aba”也是一个有效的答案. 我的答案: 想法: ...
- ZooKeeper server &&client
写了一个关于zookeepeer应用的简单demo 服务端定时的向zookeeper集群注册,客户端监听zookeeper服务节点变化,一旦变化,立刻响应,更新服务端列表 服务端代码: #includ ...
- vim 删除文件全部内容
很多时候我们需要删除脚本文件全部内容, 重新再写入新的内容,进行其他的操作: 很多时候我们对应用程序的排错需要查看日志文件,然而日志中通常有许多我们以前的应用程序产生的日志,其他的日志过多的时候,有时 ...
- 如何设计好的RESTful API之安全性
保证RESTful API的安全性,主要包括三大方面: a) 对客户端做身份认证 b) 对敏感的数据做加密,并且防止篡改 c) 身份认证之后的授权 1.对客户端做身份认证,有几种常见的做法: 1)在请 ...
- query 获取本身的HTML
<div class="test"><p>hello,你好!</p></div> <script> $(".t ...
- OBJ文件
OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer"开发的一种标准3D模型文件格式,很适合用于3D软件模 ...
- 实用图像处理入门 - 1 - opencv VS2012 环境搭建
标签中的部分 font-family: 华文细黑; font-size: 26px; font-weight: bold; color: #611427; margin-top:40px; } h2 ...
- 【bzoj1717】[Usaco2006 Dec]Milk Patterns 产奶的模式 后缀数组+离散化
题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个“模式”. John的牛奶按质量可以被赋予一 ...
- 【刷题】洛谷 P2709 小B的询问
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...