枚举:熄灯问题

POJ1222 EXTENDED LIGHTS OUT

http://poj.org/problem?id=1222

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11915 Accepted: 7637

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:
- It does not matter what order the buttons are pressed.
- 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.
- 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.

  • 有一个由按钮组成的矩阵 , 其中每行有 6个按钮 , 共5行
  • 每个按钮的位置上有一盏灯
  • 当按下一个钮后 , 该按钮以及周围位置 (上边 , 下边 , 左 边, 右边 )的灯都会改变状态
  • 如果灯原来是点亮的 , 就会被熄灭
  • 如果灯原来是熄灭的 , 则会被点亮
    • 在矩阵 角上 的按钮改变 3盏灯 的状态
    • 在矩阵 边上 的按钮改变 4盏灯 的状态
    • 其他的按钮 改变 5盏灯 的状态
  • 与一盏灯毗邻的多个按钮被下时 ,一个操 作会抵消另次作的结果
  • 给定矩阵中每盏灯的初始状态,求一种按钮方案使得所有的灯都熄灭

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.

  • 第一行是个正整数 N, 表示需要解决的案例数
  • 每个案例由 5行组成 , 每一行包括 6个数字
  • 这些数字以空格隔开 , 可以是 0或1
  • 0 表示灯的初始状态是 熄灭 的
  • 1 表示灯的初始状态是 点亮 的

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.

  • 对每个案例 , 首先输出一行 ,
    输出字符串 “PUZZLE #m”, “PUZZLE #m”, “PUZZLE #m”, “PUZZLE #m”, 其中 m是该案例的序 号
  • 接着按照该案例的输入格式出 5行
    • 1 表示需要把对应的按钮 按下
    • 0表示 不需要按 对应的按钮
    • 每个数字以一空格隔开

Sample Input

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

Sample Output

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

Source

Greater New York 2002

解题分析

  • 第2次按下同一个按钮时,将抵消第 1次按下时所产生的结果
  • 每个按钮最多只需要按下一次
  • 各个按钮被按下的顺序对最终的结果没有影响
  • 对第一行中每一盏点亮的灯,按下第二行对应列的按钮,就可以熄灭第一行所有灯
  • 如此重复下去,可以熄灭第1,2,3,4行的全部灯
  • 第一想法:枚举所有可能的按钮(开关)状态,对每个状态计算一下最后灯的情况,看是否都熄灭
    • 每个按钮有两种状态(按下或者不按下)
    • 一共有30个开关,那么状态数是 230 ,太多,会超时
  • 如何减少枚举的状态数?
    基本思路 : 如果存在某个局部 , 一旦这个局部的状态被确定 , 那么剩余其他部分的状态只能是确定一种 , 或者不多的 n种, 那么就只需枚举这个局部状态即可
  • 本题是否存在这样的“局部”呢?
    经过观察,发现第1行就是这样的一个“局部”
    因为第1行的各开关状态确定的情况下,这些开关作用过后,将导致第一行某些灯是亮的,某些灯是灭的
    要熄灭第1行某个亮着的灯(假设位于第i列),那么唯一的办法就是按下第2行第i列的开关(因为第1行的开关已经用过了,而第3行及其以后的开关不会影响到第1行)
    为了使第1行的灯全部熄灭,第2行的合理开关状态就是唯一的
    第2行的开关起作用后,为了熄灭第3行的合理开关状态就也是唯一的
    以此类推,最后一行的开关状态也是唯一的
    只要第1行的状态确定下来,记作A,那么剩余行的情况就是唯一确定的了
    推算出最后一行的开关状态,然后看看最后一行的开关起作用后,最后一行的所有灯是否都熄灭:
    如果是,那么A就是一个解
    如果不是,那么A是一个错误的解,换一个第1行的状态重新尝试
    只需枚举第1行的状态,状态数是 26=64
    枚举第一列,状态数是 25=32
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值