POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10835 | Accepted: 6929 |
Description

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
Output
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
题目链接:POJ 1222
对于异或方程组的高斯消元感觉用上三角的回代法做比较好,感觉化成行标准型比较麻烦,一共30的方程,首先可以假设第i个方程解第i个未知数,由于开关对自己和周围四个按钮均有影响,那么第$i$个方程的第$i$个变量代表自己,肯定系数为1,设与第$i$个位置相关的其余4个开关标号为$a_i,b_i,c_i,d_i$,那么第$i$个方程显然也与$a_i、b_i、c_i、d_i$有关,即第$i$个方程的第$a_i、b_i、c_i、d_i$位置的系数为1,其余为0(这个很重要,不相关在矩阵里用0代替,而不是不存在),然后化成上三角后用回代法从下至上得到答案。然后由于是模2意义下的加减乘除,可以发现加减法其实就是异或(对于0、1两个数的运算把加减号替换成异或符号不仅结果相同,而且还省去了取模2的麻烦),然后乘法就是做逻辑与即&&运算,除法可以用乘法的逆运算得到,由于当前处理行的要留下的系数肯定为1,然后要消元的行对应的列系数消元之前肯定也是1,因此$系数_1\oplus(*)1 \oplus(-)系数_2\oplus(*)1=系数_1\oplus系数_2$
代码:
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"ceq",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 32;
int Mat[N][N], ans[N];
int id[6][7]; void Gauasian(int neq, int nvar)
{
int ceq, cvar, i, j;
for (ceq = 1, cvar = 1; ceq <= neq && cvar <= nvar; ++ceq, ++cvar)
{
int teq = ceq;
for (i = ceq + 1; i <= neq; ++i)
if (abs(Mat[i][cvar]) > abs(Mat[teq][cvar]))
teq = i;
if (teq != ceq)
{
for (j = 1; j <= nvar + 1; ++j)
swap(Mat[ceq][j], Mat[teq][j]);
}
for (i = ceq + 1; i <= neq; ++i)
{
if (Mat[i][cvar] == 0)
continue;
for (j = cvar; j <= nvar + 1; ++j)
Mat[i][j] ^= Mat[ceq][j];
}
}
for (i = neq; i >= 1; --i)
{
ans[i] = Mat[i][nvar + 1];
for (j = i + 1; j <= nvar; ++j)
ans[i] ^= (Mat[i][j] && ans[j]);
}
}
int main(void)
{
int tcase, i, j;
scanf("%d", &tcase);
for (i = 1; i <= 5; ++i)
for (j = 1; j <= 6; ++j)
id[i][j] = (i - 1) * 6 + j;
for (int q = 1; q <= tcase; ++q)
{
CLR(Mat, 0);
CLR(ans, 0);
for (i = 1; i <= 5; ++i)
for (j = 1; j <= 6; ++j)
scanf("%d", &Mat[id[i][j]][31]);
for (i = 1; i <= 5; ++i)
{
for (j = 1; j <= 6; ++j)
{
int ID = id[i][j];
Mat[ID][ID] = 1;
if (i > 1)
Mat[ID][id[i - 1][j]] = 1;
if (i < 5)
Mat[ID][id[i + 1][j]] = 1;
if (j > 1)
Mat[ID][id[i][j - 1]] = 1;
if (j < 6)
Mat[ID][id[i][j + 1]] = 1;
}
}
Gauasian(30, 30);
printf("PUZZLE #%d\n", q);
for (i = 1; i <= 30; ++i)
printf("%d%c", ans[i], " \n"[i % 6 == 0]);
}
return 0;
}
POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)的更多相关文章
- POJ 1222 EXTENDED LIGHTS OUT (高斯消元)
题目链接 题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操作使灯全灭? 题解:这个问题是很经典的高斯消元问题.同一个按钮最多只能被按一次,因为 ...
- POJ 1222 EXTENDED LIGHTS OUT [高斯消元XOR]
题意: $5*6$网格里有一些灯告诉你一开始开关状态,按一盏灯会改变它及其上下左右的状态,问最后全熄灭需要按那些灯,保证有解 经典问题 一盏灯最多会被按一次,并且有很明显的异或性质 一个灯作为一个方程 ...
- bzoj千题计划187:bzoj1770: [Usaco2009 Nov]lights 燈 (高斯消元解异或方程组+枚举自由元)
http://www.lydsy.com/JudgeOnline/problem.php?id=1770 a[i][j] 表示i对j有影响 高斯消元解异或方程组 然后dfs枚举自由元确定最优解 #in ...
- 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组
[题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...
- 【poj1830-开关问题】高斯消元求解异或方程组
第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...
- hihocoder 第五十二周 高斯消元·二【高斯消元解异或方程 难点【模板】】
题目地址:http://hihocoder.com/contest/hiho57/problem/1 输入 第1..5行:1个长度为6的字符串,表示该行的格子状态,1表示该格子是亮着的,0表示该格子是 ...
- POJ 1222 熄灯问题【高斯消元】
<题目链接> 题目大意: 有一个5*6的矩阵,每一位是0或者1. 没翻转一位,它的上下左右的数字也为改变.(0变成1,1变成0).要把矩阵中所有的数都变成0.求最少翻转次数的方案,输出矩阵 ...
- 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> ...
随机推荐
- 【BZOJ1064】[NOI2008] 假面舞会(图上DFS)
点此看题面 大致题意:有\(k\)种面具(\(k\)是一个未知数且\(k≥3\),每种面具可能有多个),已知戴第\(i\)种面具的人能看到第\(i+1\)种面具上的编号,特殊的,戴第\(k\)种面具的 ...
- 【BZOJ2243】[SDOI2011] 染色(树链剖分)
点此看题面 大致题意: 有一棵\(n\)个节点的无根树和\(m\)个操作,且每个节点有一个颜色.操作有两种:一种是将两点树上路径之间所有点染成颜色\(c\),另一种是询问两点树上路径之间颜色段的数量. ...
- fread, fwrite - 二进制流的输入/输出
总览 (SYNOPSIS) #include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stre ...
- Charles拦截请求
一.通过Charles抓包,可拦截请求并篡改交互信息 1.可篡改客户端向服务器发起的请求信息(服务器收到的是假消息) 2.可篡改服务器返回给客户端的响应结果(客户端看到的是假消息) 二.篡改用户请求 ...
- 利用原生JS实现类似浏览器查找高亮功能(转载)
利用原生JS实现类似浏览器查找高亮功能 在完成 Navify 时,增加一个类似浏览器ctrl+f查找并该高亮的功能,在此进行一点总结: 需求 在.content中有许多.box,需要在.box中找出搜 ...
- 第十一篇、UITableView headerview下拉放大
核心代码: -(void)createTableViewHeaderView{ _tableViewHeaderView = [[UIView alloc] initWithFrame:(CGRect ...
- 64位系统InlineHook
APIHook64Class.h #ifndef APIHOOK64CLASS_H_ #define APIHOOK64CLASS_H_ #include <Windows.h> clas ...
- 为什么选择Redis
1)Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储. 2)Redis支持master-slave(主-从)模式应用 3)Redi ...
- Python入门必学:字符串和编码正确的使用方法
字符编码,我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...
- notification 使用的基本方法
当某个应用程序希望向用户发出一些提示信息,而应用程序又不在前台,可以借助Notification来实现.发出一条通知后,手机最上方额通知栏会显示一个图标,下来状态栏以后可以看到详细内容. 一.通知的基 ...