熄灯问题

描述
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 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.
样例输入
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
样例输出
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

解题思路:

        第2次按下同一个按钮时,将抵消第1次按下时所产生的结果。因此,每个按钮最多只需要按下一次。

        各个按钮被按下的顺序对最终的结果没有影响

        对第1行中每盏点亮的灯,按下第2行对应的按钮,就可以熄灭第1行的全部灯。如此重复下去,可以熄灭第1、2、3、4行的全部灯。

第一想法:枚举所有可能的按钮(开关)状态,对每个状态计算一下最后灯的情况,看是否都熄灭。每个按钮有两种状态(按下或不按下),一共有30个开关,那么状态数是230,太多,会超时。

如何减少枚举的状态数目呢?一个基本思路是,如果存在某个局部,一旦这个局部的状态被确定,那么剩余其他部分的状态只能是确定的一种,或者不多的n种,那么就只需枚举这个局部的状态就行了。

本题是否存在这样的“局部”呢?经过观察,发现第1行就是这样的一个“局部”。因为第1行的各开关状态确定的情况下,这些开关作用过后,将导致第1行某些灯是亮的,某些灯是灭的。此时要熄灭第1行某个亮着的灯(假设位于第i列),那么唯一的办法就是按下第2行第i列的开关(因为第一行的开关已经用过了,而第3行及其后的开关不会影响到第1行)。因此,为了使第1行的灯全部熄灭,第2行的合理开关状态就是唯一的。

第2行的开关起作用后,为了熄灭第二行的灯,第3行的合理开关状态就也是唯一的,以此类推,最后一行的开关状态也是唯一的。

总之,只要第1行的状态定下来,比如叫A,那么剩余行的情况就是确定唯一的了。推算出最后一行的开关状态,然后看看最后一行的开关起作用后,最后一行的所有灯是否都熄灭,如果是,那么A就是一个解的状态。如果不是,那么A不是解的状态,第1行换个状态重新试试。

因此,只需枚举第一行的状态,状态数是26 = 64

参考代码:

#include <iostream>
#include <memory.h>
#include <string.h>
using namespace std;
int T;  
int anPuzzle[6][6];
int anOriPuzzle[6][6];
int anSwitch[6][6]; //开关状态
int i,j;
void SwitchStatus( int n, int * pSwitch)
{
	for( i = 0;i < 6 ;i ++ )
		pSwitch[i] = (n >> i ) & 1;
}
void ApplySwitch( int * pLights, int * pNextLights, int * pSwitchs)  {
	for( int i = 0;i < 6; i ++ ) { //依次让每个开关起作用
		if( pSwitchs[i] ) {
			//开关左边的灯改变状态
			if( i > 0 )
				pLights[i-1] = 1 - pLights[i-1];
			//开关所在位置的灯改变状态
			pLights[i] = 1 - pLights[i];
			//开关右边的灯改变状态
			if( i < 5) 
				pLights[i+1] = 1 - pLights[i+1];
			//开关下边的灯改变状态
			pNextLights[i] = 1 - pNextLights[i];
		}
	}
}
void OutputResult(int t) //输出结果
{
	cout << "PUZZLE #" << t << endl;
	for( int i = 0;i < 5; i ++ ) {
		for( int j = 0; j < 6; j ++ ) {
			cout << anSwitch[i][j];
			if( j < 5 ) cout << " ";
		}
		cout << endl;
	}
}
int main()   {
	cin >> T;
	for( int t = 0; t < T; t ++ ) {
		for( i = 0;i < 5; i ++ )
			for(  j = 0; j < 6; j ++ ) 
				cin >> anOriPuzzle[i][j];
		for( int n = 0; n < 64; n ++ ) { //遍历首行开关的64种状态
			memcpy( anPuzzle,anOriPuzzle,sizeof(anPuzzle));
			//算出n所代表的开关状态,放到anSwitch[0]
			SwitchStatus( n, anSwitch[0]);
			//下面逐行让开关起作用,并算出下一行开关应该是什么状态,再让它们起作用……
			for( int k = 0; k < 5; k ++ ) {
				//算出第k行开关起作用后的结果
				ApplySwitch( anPuzzle[k],anPuzzle[k+1],anSwitch[k]);
				//第k+1行的开关状态应和第k行的灯状态一致
				memcpy( anSwitch[k+1], anPuzzle[k],sizeof(anPuzzle[k]));
			}
			bool bOk = true; //记录最后一行灯是不是全灭
			//看最后一行灯是不是全灭
			for(int  k = 0; k < 6; k ++ ) {
				if( anPuzzle[4][k] ) {
					bOk = false;
					break;
				}
			}
			if( bOk )  {
				OutputResult(t+1); //输出解
				break; //找到解,就不用再试下一种状态了
			}
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值