您的位置:首页 > 其它

POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

2016-03-03 21:43 134 查看
The Pilots Brothers' refrigerator

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 22286Accepted: 8603Special 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

6
1 1
1 3
1 4
4 1
4 3
4 4

Source

Northeastern Europe 2004, Western Subregion
题目大意

  给出$4\times 4$共$16$个门把手,改变一个门把手(打开或关闭)需要同时改变同行同列的门把手,当所有门把手都打开时才能打开门。+代表关,-代表开。

基本思路

  此题与POJ 1753差不太多,只是本题没有Impossible的情况,且需要输出改变的门把手的位置。POJ 1753的详细题解请看这里,里面讲的都可以用于此题。因此本文简要略过,只给出爆搜的代码,以及分治的详解。

  一、爆搜出奇迹:

  1、将16个门把手的状态压缩成一个整数。

  2、可以选择改变0个门把手(已经全开),1~16个门把手(重复选相当于没有改变),要求给出最小步数,因此从0搜到16即可。

  3、要求输出要改变的门把手的位置,在搜索中回溯即可。本题为特判,因此输出顺序没有关系。

  4、搜不到要把门把手的状态改回来再搜。

  代码如下:

#include <stdio.h>

int sswitch[5][5];

int main() {
for(int i=0; i<4; i++) {
for(int j=0; j<4; j++)
if(getchar()=='+') {
for(int k=0; k<4; k++) {
sswitch[i][k]^=1;
sswitch[k][j]^=1;
}
sswitch[i][j]^=1;
}
getchar();
}

int res=0;
for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(sswitch[i][j])
++res;
printf("%d\n",res);

for(int i=0; i<4; i++)
for(int j=0; j<4; j++)
if(sswitch[i][j])
printf("%d %d\n",i+1,j+1);
return 0;
}


POJ 2903 分治

——本文原创by BlackStorm,转载请注明出处。

本文链接:/article/7078212.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: