poj1222(高斯消元,开关问题)
来源:互联网 发布:coc城墙升级数据 编辑:程序博客网 时间:2024/05/20 14:23
地址:http://poj.org/problem?id=1222
EXTENDED LIGHTS OUT
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6305 Accepted: 4140
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.
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
20 1 1 0 1 01 0 0 1 1 10 0 1 0 0 11 0 0 1 0 10 1 1 1 0 00 0 1 0 1 01 0 1 0 1 10 0 1 0 1 11 0 1 1 0 00 1 0 1 0 0
Sample Output
PUZZLE #11 0 1 0 0 11 1 0 1 0 10 0 1 0 1 11 0 0 1 0 00 1 0 0 0 0PUZZLE #21 0 0 1 1 11 1 0 0 0 00 0 0 1 0 01 1 0 1 0 11 0 1 1 0 1
题意:输入目标状态,问怎样操作才可以使全关的灯达到目标状态。
思路:开关问题的典型,直接高斯消元就可以,可以拿这题作为高斯消元的入门题。
代码:
#include<iostream>#include<queue>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;#define LL __int64#define col 30int num[40][40],ans[40];void getans(){ for(int i=0,m=0;i<col,m<col;m++){ int k=i; for(int j=i+1;j<col;j++){ //将最大的前移 if(abs(num[j][m])>abs(num[k][m])) k=j; } if(k!=i){ for(int j=m;j<=col;j++) swap(num[i][j],num[k][j]); } if(!num[i][m]) continue; //有可能存在中间一列全都为零 for(int j=i+1;j<col;j++){ if(!num[j][m]) continue; for(k=m;k<=col;k++) num[j][k]^=num[i][k]; //这里想明白了,因为我们操作恒置为1,所以操作状态为0的开关会使其变为1,反之操作状态为1的开关会使其变为0 } i++; } for(int i=col-1;i>=0;i--){ ans[i]=num[i][col]; for(int j=i+1;j<col;j++) ans[i]^=(num[i][j]&ans[j]); //需要注意,这种求答案只适用于开关问题一类的题目 }}int main(){ int t,cas=1; scanf("%d",&t); while(t--){ memset(num,0,sizeof(num)); for(int i=0;i<5;i++) for(int j=0;j<6;j++){ int s=i*6+j; num[s][s]=1; if(i>0) num[s-6][s]=1; if(i<4) num[s+6][s]=1; if(j>0) num[s-1][s]=1; if(j<5) num[s+1][s]=1; } for(int i=0;i<col;i++) scanf("%d",&num[i][col]); getans(); printf("PUZZLE #%d",cas++); for(int i=0;i<col;i++) if(i%6==0)printf("\n%d",ans[i]); else printf(" %d",ans[i]); puts(""); } return 0;}
0 0
- poj1222(高斯消元,开关问题)
- poj1222开关问题, 高斯消元
- 高斯消元(开关灯问题)poj1222
- poj1222 EXTENDED LIGHTS OUT 开关问题
- poj1222(高斯消元)
- 开关问题(高斯消元)
- POJ1222 EXTENDED LIGHTS OUT(熄灯问题)
- 枚举—案例(熄灯问题poj1222)
- POJ1222(高斯消元)
- POJ1222高斯消元
- poj1222 高斯消元
- poj1222 高斯消元
- poj 1830 开关问题(高斯消元(开关问题))
- poj1222 EXTENDED LIGHTS OUT (高斯消元)
- poj1222 EXTENDED LIGHTS OUT(高斯消元)
- [POJ1222]EXTENDED LIGHTS OUT(高斯消元)
- 开关问题(模板+高斯消元)
- [POJ1830]开关问题(高斯消元)
- 自己编写的 entity framwork 6 通用 dal 类 增删改查 多分类字段
- 解决:Error, some other host already uses address ${IPADDR}.
- 好忙的说、经常加班的有木有
- java 将pdf转换image
- tiny6410烧写环境
- poj1222(高斯消元,开关问题)
- Linux开启mysql远程连接的设置步骤
- 单机断网不能访问127.0.0.1
- nexus 简单操作
- 【Android开发】关于在res目录下新建文件夹raw
- 链接扫盲
- ffmpeg参数中文详细解释
- [cocos2dx 3.0 + iap]中文文档
- Linux集群管理工具PSSH批量管理服务器