1222(部分枚举)
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翻译:?1.问题描述:有一个由按钮组成的矩阵,其中每行有6个按钮,共5行。每个按钮的位置上有一盏灯。当按下一个按钮后,该按钮以及周围位置(上边、下边、左边、右边)的灯都会改变一次。即,如果灯原来是点亮的,就会被熄灭;如果灯原来是熄灭的,则会被点亮。
#include<cstdio>using namespace std ;int a[6][8] ;//表示灯的初始状态int b[6][8] ;//表示要计算的结果/**对a的第一行的给定取值,根据熄灯规则计算出a的其他行的值,判断这个a能否使得矩阵第五行的所有灯也恰好熄灭。*/bool deal(){ int c,r ; for(r=1;r<5;r++) for(c=1;c<7;c++) b[r+1][c]=(a[r][c]+b[r][c]+b[r][c-1]+b[r][c+1]+b[r-1][c])%2 ; for(c=1;c<7;c++) if((b[5][c-1]+b[5][c]+b[5][c+1]+b[4][c])%2!=a[5][c]) return false ; return true ;}void temp(){ int c ; for(c=1;c<7;c++) b[1][c] = 0 ; while(deal()==false){ b[1][1]++; c=1 ; while(b[1][c]>1){ b[1][c] = 0 ; c++ ; b[1][c]++ ; } } return ;}int main(){ int cases,i,r,c ; scanf("%d",&cases) ; for(r=0;r<6;r++) b[r][0] = b[r][7] = 0;//把第0列和第7列全部置0 for(c=1;c<7;c++) b[0][c] = 0 ; for(i=0;i<cases;i++){ for(r=1;r<6;r++)//行 for(c=1;c<7;c++)//列 scanf("%d",&a[r][c]); temp(); printf("PUZZLE #%d\n", i+1); for(r=1;r<6;r++){ for(c=1;c<7;c++) printf("%d ",b[r][c]);printf("\n"); } } return 0 ;}?