EXTENDED LIGHTS OUT
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12616   Accepted: 8002

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

Source

 

题意分析

给出t组数据,每组数据为一个5×6的矩阵,矩阵中元素为0或1。在矩阵中选择每次点击一个点,可以将其相邻的(上下左右)的元素异或,即1变为0,变为1。求如何点击才能使得矩阵全部变为0。 
输出一个5×6的矩阵,其中1表示点击该位置,0表示不点击。

 #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int a[][][];
int d[][] = { {,},{,-},{-,},{,},{,} };
int b[][], c[][];
int map[][]; int get(int x, int y)//获得周围的转了几次+原本的数
{
int i;
int s = map[x][y];
for (i = ; i < ; i++)
{
int xx = x + d[i][];
int yy = y + d[i][];
if (xx < || y < || x >= || y >= ) continue;
s += b[xx][yy];
}
return s % ;
} int cal()//返回总共需要转几次
{
int i, j;
int res = ;
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
if (get(i - , j) == )//如果返回的是奇数,说明要通过i层来改变i-1层
{
res++;
b[i][j] = ;
}
}
}
for (j = ; j < ; j++)
{
if (get(, j) == ) return -;
}
return res;
} int solve(int t)
{
int i,j;
int ans = inf;
for (i = ; i < << ; i++)//二进制枚举这一层第一排的每一种情况
{
memset(b, , sizeof(b));
for (j = ; j < ; j++)
{
b[][j] = i >> j & ;
}
int res = cal();
if (res != - && ans > res)
{
ans =res;
memcpy(c, b, sizeof(b));
}
}
printf("PUZZLE #%d\n", t);
for (int i = ; i<; i++)
for (int j = ; j<; j++)
printf("%d%c", c[i][j], j == ? '\n' : ' ');
} int main()
{
int n;
scanf("%d", &n);
int i,j,k;
for (i = ; i <=n; i++)
for (j = ; j < ; j++)
for (k = ; k < ; k++)
scanf("%d", &a[i][j][k]);
for (i = ; i <= n; i++)//一层层来解决
{
for (j = ; j < ; j++)
for (k = ; k < ; k++)
map[j][k] = a[i][j][k];
solve(i);
}
}

高斯消元的

 #include <iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn=;
int a[maxn][maxn+],x[maxn];//a 是系数矩阵和增广矩阵,x 是最后存放的解
// a[][maxn]中存放的是方程右面的值(bi)
int equ,var;//equ 是系数阵的行数,var 是系数矩阵的列数(变量的个数)
int free_num,ans=; int abs1(int num) //取绝对值
{
if (num>=) return num;
else
return -*num;
}
void Debug(void)
{
int i, j;
for (i = ; i < equ; i++)
{
for (j = ; j < var + ; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//调试输出,看消元后的矩阵值,提交时,就不用了
inline int gcd(int a, int b) //最大公约数
{
int t;
while (b != )
{
t = b;
b = a % b;
a = t;
}
return a;
} inline int lcm(int a, int b) //最小公倍数
{
return a * b / gcd(a, b);
} int dfs(int p) //枚举自由解,只能取0-1,枚举完就回带,找到最小的
{
if (p<=free_num-) //深入到了主对角线元素非0 的行了
{
//下面就是回带的代码啊
for(int i = free_num-; i >= ; --i)
{
int tmp = a[i][var] % ;
for(int j = i+; j < var; ++j) //x[i]取决于x[i+1]--x[var]啊,所以后面的解对前面的解有影
//响。
if(a[i][j] != )
tmp = ( tmp - (a[i][j]*x[j])% + ) % ;
x[i] = (tmp/a[i][i]) % ; //上面的正常解
} //回带完成了
//计算解元素为1 的个数;
int sum=;
for(int i=; i<var; i++) sum+=x[i];
if (ans>sum) ans=sum;
return ;
}
x[p]=;
dfs(p-);
x[p]=;
dfs(p-);
}
void swap(int &a,int &b)
{
int temp=a; //交换 2 个数
a=b;
b=temp;
}
int Gauss()
{
int k,col = ;
//当前处理的列
for(k = ; k < equ && col < var; ++k,++col)
{
int max_r = k;
for(int i = k+; i < equ; ++i)
if(a[i][col] > a[max_r][col])
max_r = i;
if(max_r != k)
{
for(int i = k; i < var + ; ++i)
swap(a[k][i],a[max_r][i]);
}
if(a[k][col] == )
{
k--;
continue;
}
for(int i = k+; i < equ; ++i)
{
if(a[i][col] != )
{
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] < )
tb = -tb;
for(int j = col; j < var + ; ++j)
a[i][j] = ( (a[i][j]*ta)% - (a[k][j]*tb)% + ) % ;
// 0 和 1 两种状态
}
}
}
//a[i][j]只有 //上述代码是消元的过程,行消元完成
//解下来 2 行,判断是否无解
//注意 K 的值,k 代表系数矩阵值都为 0 的那些行的第 1 行
for(int i = k; i < equ; ++i)
if(a[i][col] != )
return -;
//Debug(); //唯一解或者无穷解,k<=var
//var-k==0 唯一解;var-k>0 无穷多解,自由解的个数=var-k
//能执行到这,说明肯定有解了,无非是 1 个和无穷解的问题。
//下面这几行很重要,保证秩内每行主元非 0,且按对角线顺序排列,就是检查列
for(int i = ; i <equ; ++i)//每一行主元素化为非零
if(!a[i][i])
{
int j;
for(j = i+; j<var; ++j)
if(a[i][j])
break;
if(j == var)
break;
for(int k = ; k < equ; ++k)
swap(a[k][i],a[k][j]);
}
// ----处理保证对角线主元非 0 且顺序,检查列完成 free_num=k;
if (var-k>)
{
dfs(var-);
return ans;
//无穷多解,先枚举解,然后用下面的回带代码进行回带;
//这里省略了下面的回带的代码;不管唯一解和无穷解都可以回带,只不过无穷解
//回带时,默认为最后几个自由变元=0 而已。
}
if(var-k<)
return -;
// 无解返回 -1
if (var-k==)//唯一解时
{
//下面是回带求解代码,当无穷多解时,最后几行为 0 的解默认为 0;
for(int i = k-; i >= ; --i) //从消完元矩阵的主对角线非 0 的最后 1 行,开始往
//回带
{
int tmp = a[i][var] % ; for(int j = i+; j < var; ++j) //x[i]取决于 x[i+1]--x[var]啊,所以后面的解对前面的解有影响。
if(a[i][j] != )
tmp = ( tmp - (a[i][j]*x[j])% + ) % ;
//if (a[i][i]==0) x[i]=tmp;//最后的空行时,即无穷解得
//else
x[i] = (tmp/a[i][i]) % ; //上面的正常解
}
int sum=;
for(int i=; i<var; i++)
sum+=x[i];
return sum; //回带结束了
}
} int main(void)
{
// freopen("Input.txt", "r", stdin);
int i, j,t,t1;
cin>>t;
t1=t;
equ=;
var=;
while (t--)
{
memset(a, , sizeof(a));
memset(x, , sizeof(x));
//memset(free_x, 1, sizeof(free_x)); // 一开始全是不确定的变元.
//下面要根据位置计算a[i][j];
for (i = ; i < ; i++)
{
for (j = ; j < ; j++)
{
/* for(int k=0;k<4;k++)
{
int ni=i+di[k];
int nj=j+dj[k];
if(inlim(ni,nj))
{
a[i*6+j][ni*6+nj]=1;
}
}
*/
if (i->=) a[i*+j][(i-)*+j]=; //计算上面的位置
if (i+<=) a[i*+j][(i+)*+j]=;//计算下面的位置
if (j->=) a[i*+j][i*+j-]=;//计算左面的位置
if (j+<=) a[i*+j][i*+j+]=; //计算右面的位置
a[i*+j][i*+j]=;//别忘了计算自己
cin>>a[i*+j][];
//scanf("%d", &a[i][j]);
}
}
//Debug();
//free_num = Gauss();
free_num=Gauss();
if (free_num == -) printf("无解!\n");
else if (free_num >= )
{
int na_num=;
printf("PUZZLE #%d\n",t1-t);
for (i = ; i < var; i++)
{
na_num++;
if (na_num%==)
{
printf("%d\n",x[i]);
}
else
printf("%d ",x[i]);
}
}
// printf("\n");
}
return ;
}

POJ 1222 EXTENDED LIGHTS OUT(反转)的更多相关文章

  1. POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)

    POJ 1222 EXTENDED LIGHTS OUT 今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗, ...

  2. POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10835   Accepted: 6 ...

  3. Poj 1222 EXTENDED LIGHTS OUT

    题目大意:给你一个5*6的格子,每个格子中有灯(亮着1,暗着0),每次你可以把一个暗的点亮(或者亮的熄灭)然后它上下左右的灯也会跟着变化.最后让你把所有的灯熄灭,问你应该改变哪些灯. 首先我们可以发现 ...

  4. POJ 1222 EXTENDED LIGHTS OUT(高斯消元)题解

    题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次 思路:poj 1222 高斯消元详 ...

  5. poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)

    http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...

  6. 【高斯消元】Poj 1222:EXTENDED LIGHTS OUT

    Description In an extended version of the game Lights Out, is a puzzle with 5 rows of 6 buttons each ...

  7. OpenJudge 2811 熄灯问题 / Poj 1222 EXTENDED LIGHTS OUT

    1.链接地址: http://bailian.openjudge.cn/practice/2811 http://poj.org/problem?id=1222 2.题目: 总时间限制: 1000ms ...

  8. POJ 1222 EXTENDED LIGHTS OUT(高斯消元)

    [题目链接] http://poj.org/problem?id=1222 [题目大意] 给出一个6*5的矩阵,由0和1构成,要求将其全部变成0,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...

  9. POJ 1222 EXTENDED LIGHTS OUT (熄灯问题)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8417   Accepted: 5441 Description In an ...

随机推荐

  1. 201621123006 《Java程序设计》第2周学习总结

    1. 本周学习总结 以几个关键词描述本周的学习内容.并将关键词之间的联系描述或绘制出来. 原则:少而精,自己写.即使不超过5行也可,但请一定不要简单的复制粘贴. java数据类型:java数据类型分为 ...

  2. 使用 create-react-app 构建 react应用程序

    原文 http://blog.csdn.net/github_squad/article/details/57452333#

  3. win10 安装MongoDB

    win10上面安装mongodb的时候,注意不要勾选上Install MongoDB Compass, 否则会退出报错!!!! mongodb的安装 我是在E盘建立的一个mongodb文件夹,用来安装 ...

  4. MP3格式音频文件结构解析

    MP3的全称是MPEG Audio Layer3,它是一种高效的计算机音频编码方案,它以较大的压缩比将音频文件转换成较小的扩展名为.MP3的文件,基本保持原文件的音质.MP3是ISO/MPEG标准的一 ...

  5. HslCommunication库的二次协议扩展,适配第三方通讯协议开发,基础框架支持长短连接模式

    本文将使用一个gitHub开源的项目来扩展实现二次协议的开发,该项目已经搭建好了基础层架构,并实现了三菱,西门子,欧姆龙,MODBUS-TCP的通讯示例,也可以参照这些示例开发其他的通讯协议,并Pul ...

  6. postfix邮件服务器搭建03-webmail安装篇

    本文接着上文的安装进行,介绍另一个WebMail功能更加人性化的roundcube.当然也可以对已有的postfix邮件系统进行功能完善 1.下载安装roundcube cd /server/tool ...

  7. LeetCode - Course Schedule 解题报告

    以前从来没有写过解题报告,只是看到大肥羊河delta写过不少.最近想把写博客的节奏给带起来,所以就挑一个比较容易的题目练练手. 原题链接 https://leetcode.com/problems/c ...

  8. BZOJ5301: [Cqoi2018]异或序列(莫队)

    5301: [Cqoi2018]异或序列 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 400  Solved: 291[Submit][Status ...

  9. 3DsMax动画插件

    * 简易骨骼动画: Mesh当前帧顶点 = Mesh绑定时顶点 * 绑定时骨骼的变换到本帧骨骼的变换的改变量. = Mesh绑定时顶点 * 绑定时骨骼的变换的逆矩阵 * 本帧的骨骼变换. = Mesh ...

  10. Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition

    在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...