POJ 1222 EXTENDED LIGHTS OUT(反转)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12616 | Accepted: 8002 |
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
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(反转)的更多相关文章
- POJ 1222 EXTENDED LIGHTS OUT(翻转+二维开关问题)
POJ 1222 EXTENDED LIGHTS OUT 今天真是完美的一天,这是我在poj上的100A,留个纪念,马上就要期中考试了,可能后面几周刷题就没这么快了,不管怎样,为下一个200A奋斗, ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)
EXTENDED LIGHTS OUT Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10835 Accepted: 6 ...
- Poj 1222 EXTENDED LIGHTS OUT
题目大意:给你一个5*6的格子,每个格子中有灯(亮着1,暗着0),每次你可以把一个暗的点亮(或者亮的熄灭)然后它上下左右的灯也会跟着变化.最后让你把所有的灯熄灭,问你应该改变哪些灯. 首先我们可以发现 ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)题解
题意:5*6的格子,你翻一个地方,那么这个地方和上下左右的格子都会翻面,要求把所有为1的格子翻成0,输出一个5*6的矩阵,把要翻的赋值1,不翻的0,每个格子只翻1次 思路:poj 1222 高斯消元详 ...
- poj 1222 EXTENDED LIGHTS OUT(位运算+枚举)
http://poj.org/problem?id=1222 题意:给一个确定的5*6放入矩阵.每一个格子都有一个开关和一盏灯,0表示灯没亮,1表示灯亮着.让你输出一个5*6的矩阵ans[i][j], ...
- 【高斯消元】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 ...
- OpenJudge 2811 熄灯问题 / Poj 1222 EXTENDED LIGHTS OUT
1.链接地址: http://bailian.openjudge.cn/practice/2811 http://poj.org/problem?id=1222 2.题目: 总时间限制: 1000ms ...
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)
[题目链接] http://poj.org/problem?id=1222 [题目大意] 给出一个6*5的矩阵,由0和1构成,要求将其全部变成0,每个格子和周围的四个格子联动,就是说,如果一个格子变了 ...
- POJ 1222 EXTENDED LIGHTS OUT (熄灯问题)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8417 Accepted: 5441 Description In an ...
随机推荐
- Mysql 建表时报错 invalid ON UPDATE clause for 'create_date' column
这个错误是由于mysql 版本问题导致的 `create_date` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '创建时间', ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- C++实现设计模式之-装饰模式
饰模式:动态地给一个对象添加一些额外的职责.就增加功能来说,装饰模式相比生成子类更为灵活.有时我们希望给某个对象而不是整个类添加一些功能.比如有一个手机,允许你为手机添加特性,比如增加挂件.屏幕贴膜等 ...
- 联想北研实习生面试-嵌入式Linux研发工程师
8月中旬暑假去联想北研参加了实习生面试,面试职位是嵌入式Linux研发工程师.投完简历第二天,主管回复我邮件,意思是说随时来面试,到北研时候给他打个电话就行.于是我回复条短信表示感谢,并约好时间第二天 ...
- 启动Windows防火墙提示“0x8007042c"
win8.1 启动防火墙是报错:启动Windows防火墙提示“0x8007042c" 一.检查服务 1,右击开始->运行->输入“services.msc” 打开服务 在框中找到 ...
- UE4 Sequencer的事件调用
本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/73554691 作者:car ...
- iOS开发-UITextView文字排版
UITextView文本排版 1.配置NSMutableParagraphStyle NSMutableParagraphStyle *MParaStyle = [[NSMutableParagrap ...
- asp.net 输入框在chrome中无法关闭自动提示
将asp:TextBox 的属性AutoCompleteType设为Disabled,希望在chrome中点击记住用户名密码后输入框不再自动提示,但不起作用. 解决方法: <asp:TextBo ...
- jQuery插件实现表格隔行换色且感应鼠标高亮行变色
实现表格隔行换色,且感应鼠标行变色的方法有很多,在本文将为大家介绍的是使用jQuery插件来实现,具体如下 看代码: : css代码:
- python3 使用matplotlib画图问题
保存图片的问题 在画子图并保存图片的时候,用如下代码保存的图片总是不能显示,但是在运行的过程中能够正常显示图片. # coding:utf-8 from pylab import * # 创建子图 f ...