POJ2965

Written by    18:48 June 17, 2014 

原题POJ2965

The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 21431 Accepted: 8274 Special Judge

Description

The game 「The Pilots Brothers: following the stripy elephant」 has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.

Input

The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol 「+」 means that the handle is in closed state, whereas the symbol 「−」 means 「open」. At least one of the handles is initially closed.

Output

The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

Sample Input

Sample Output

Source

Northeastern Europe 2004, Western Subregion

这一道题跟POJ1753很相似, 两者都是枚举相关的算法, 完整代码如下:

大部分功能函数都与POJ1753的一样, 最主要的有两个地方, 第一个这个题目要求输出路径, 所以必须用先用一个数组把临时路径存储下来, 然后最后再用一个数组输出最短路径, 这里当时自己没有理解透彻DFS算法, 居然还半天没想过来为什么要把临时路径存储下来而不直接complete过后就输出, 这里可以借用POJ1753里面所提到的那个逐步打印翻转的方法来理解, 模拟的结果:

这里可以很清晰地看到, 在7和8次翻转过后, 虽然已经complete, 但是程序还没有遍历完, 依旧会继续运行下去, 所以这个时候必须把路径给存储好, 不然接下来就会被覆盖。

第二个就是临时路径存储操作应该放在哪里(现在回过头来看自己很低级, 但是当时确实昏了头),也可以从这个模拟的过程里面看出来每一次临时路径的存储操作应该放在第一个flip操作之后。

PS: 这道题目耗费我时间最长的地方其实是flip里面忘记翻转那个点自己, 也就是

这一行代码, 然后就是各种抓狂为什么只会输出零。。。

Category : acmstudy

Tags :