POJ EXTENDED LIGHTS OUT(开关问题模版题)
来源:互联网 发布:rayfile软件官网 编辑:程序博客网 时间:2024/06/01 01:34
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
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 <cstdio>#include <cstring>#include <algorithm>using namespace std;int s[10][10], flip[10][10], opt[10][10];int dir[5][2]={{-1,0},{1,0},{0,0},{0,-1},{0,1}};int cal();int get(int x,int y);int main(){ int n, ncase=1; scanf("%d", &n); while(n--) { for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { scanf("%d",&s[i][j]); } } int res=-1; for(int i=0;i<1<<6;i++) { memset(flip,0,sizeof(flip)); for(int j=0;j<6;j++) { flip[0][6-j-1]=(i>>j)&1; } int num=cal(); if((res==-1||res>num)&&num>=0) { res=num; memcpy(opt,flip,sizeof(flip)); } } printf("PUZZLE #%d\n",ncase++); for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { printf("%d%c",opt[i][j],j==5?'\n':' '); } } } return 0;}int cal(){ for(int i=1;i<5;i++) { for(int j=0;j<6;j++) { if(get(i-1,j)%2!=0) { flip[i][j]=1; } } } for(int i=0;i<6;i++) { if(get(4,i)%2!=0) { return -1; } } int num=0; for(int i=0;i<5;i++) { for(int j=0;j<6;j++) { num+=flip[i][j]; } } return num;}int get(int x,int y){ int num=s[x][y]; for(int i=0;i<5;i++) { int a=dir[i][0]+x, b=dir[i][1]+y; if(a>=0&&a<5&&b>=0&&b<6) { num+=flip[a][b]; } } return num;}
- POJ 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 二进制高斯消元 (开关翻转问题)
- poj1222 EXTENDED LIGHTS OUT 开关问题
- poj EXTENDED LIGHTS OUT
- EXTENDED LIGHTS OUT POJ
- poj 1222 EXTENDED LIGHTS OUT(高斯消元(开关问题 对2取模的01方程组))
- POJ - 1222 EXTENDED LIGHTS OUT(反转问题)
- 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(高斯消元)
- POJ1222 EXTENDED LIGHTS OUT(熄灯问题)
- Java实现mysql增删改查--我的JDBC学习
- 第二周项目3-1 体验复杂度 两种算法的排序时间
- C++——算法基础之排序——冒泡排序(优化)
- STM32笔记:RCC结构与配置流程
- 资源分配图化简法
- POJ EXTENDED LIGHTS OUT(开关问题模版题)
- Android四大组件
- ContentProvider之手机号码查询
- 中文乱码问题
- 微信开发,获得自己的服务器
- 如何把依赖库工程打包为aar文件格式,并且运用到主项目中?
- [pySpark][note]Click-Through Rate Prediction
- Python环境下使用pip2pi搭建属于自己的pip源
- 大叔谈创业路上的几条真理(下)