2017省选拔(三)poj 1222 EXTENDED LIGHTS OUT (状态压缩+枚举反转)
来源:互联网 发布:淘宝裸机是什么意思 编辑:程序博客网 时间:2024/06/05 10:23
点击打开链接
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.
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.
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.
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
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题目大意:给出一个01矩阵,然后将其都反转为0,每一个点最多翻一次,输出每个点的反转次数。
题解:状态压缩进行枚举所有的情况就行了(听说正解是高斯,但我不会),这个题和http://blog.csdn.net/txgang/article/details/70054329几乎是一样的。
#include <iostream>#include <cstring>#include <cstdio>#include <cmath>using namespace std;int a[10][10];int b[10][10];int temp[10][10];int dx[]={0,0,0,1,-1};int dy[]={1,-1,0,0,0};const int n=5;const int m=6;int up(int x,int y){ int num=a[x][y]; for(int i=0;i<5;i++) { int nx=x+dx[i]; int ny=y+dy[i]; if(nx>=0&&ny>=0&&nx<n&&ny<m) { if(temp[nx][ny])num++; } } return num%2;}int work(){ for(int i=1;i<n;i++) { for(int j=0;j<m;j++) { if(up(i-1,j)) { temp[i][j]=1; } } } for(int i=0;i<m;i++) { if(up(n-1,i))return -1; } int num=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(temp[i][j])num++; } } return num;}int main(){ int t,w=0; scanf("%d",&t); while(t--) { for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { scanf("%d",&a[i][j]); } } int mx=-1; for(int i=0;i<(1<<m);i++) { memset(temp,0,sizeof(temp)); for(int j=0;j<m;j++) { temp[0][m-j-1]=(i>>j)&1; } int num=work(); if(num>mx) { mx=num; memcpy(b,temp,sizeof(temp)); } } printf("PUZZLE #%d\n",++w); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(j==m-1)printf("%d\n",b[i][j]); else printf("%d ",b[i][j]); } } } return 0;}
0 0
- 2017省选拔(三)poj 1222 EXTENDED LIGHTS OUT (状态压缩+枚举反转)
- POJ - 1222 EXTENDED LIGHTS OUT(反转问题)
- POJ 1222 EXTENDED LIGHTS OUT 枚举 || 高斯消元
- POJ 1222 Java: Extended lights out 枚举
- POJ-1222 EXTENDED LIGHTS OUT【暴力枚举】
- Poj-1222-EXTENDED LIGHTS OUT-枚举
- POJ 1222 EXTENDED LIGHTS OUT (noi OpenJudge 1813)枚举
- POJ 1222 EXTENDED LIGHTS OUT(矩阵反转问题——枚举or高斯消元)
- poj 1222 EXTENDED LIGHTS OUT(高斯消元)
- poj EXTENDED LIGHTS OUT 1222 (高斯消元)
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)
- poj 1222 EXTENDED LIGHTS OUT (高斯消元)
- poj 1222 EXTENDED LIGHTS OUT (高斯消元法)
- POJ 1222 EXTENDED LIGHTS OUT(高斯消元)
- poj 1222 EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- POJ 1222 : EXTENDED LIGHTS OUT
- POJ 1222 EXTENDED LIGHTS OUT
- 蓝桥杯--入门练习 Fibonacci数列
- Dom事件流
- day81_babasport第七天
- 深度学习——通俗理解卷积神经网络
- 十年软件测试经验总结
- 2017省选拔(三)poj 1222 EXTENDED LIGHTS OUT (状态压缩+枚举反转)
- 解决/usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.14' not found问题
- ios 开发 根据数组内字典指定key排序
- 程序员简历如何脱颖而出
- SQL模糊查询语句和Escape转义字符
- 【python】引用模块,以及初探__init__.py
- Docker三剑客之Compose-二
- [BZOJ 3450]Tyvj1952 Easy:期望DP
- nova创建虚机流程源码分析 openstack