Poj 2965 The Pilots Brothers' refrigerator

1.Link:

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

2.Content:

The Pilots Brothers' refrigerator
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18655   Accepted: 7152   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

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

Source

Northeastern Europe 2004, Western Subregion

3.Method:

此题由于只有4*4,所以共有16^2种情况,可考虑枚举法(加回溯)

但此题有更巧妙的方法,方法来自:http://blog.sina.com.cn/s/blog_49e7d7350100rsao.html


  先看一个简单的问题,如何把'+'变成'-'而不改变其他位置上的状态?答案是将该位置(i,j)及位置所在的行(i)和列(j)上所有的handle更新一次,结果该位置被更新了7,相应行(i)和列(j)handle被更新了4,剩下的被更新了2.被更新偶数次的handle不会造成最终状态的改变.因此得出高效解法,在每次输入碰到'+'的时候,自增该位置与相应的行和列,当输入结束后,遍历数组,所有为奇数的位置则是操作的位置,而奇数位置的个数之和则是最终的操作次数.

4.Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 
 5 #define ESIZE 4 
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     //freopen("D://input.txt","r",stdin);
12     
13     int i,j,k;
14     
15     int res[ESIZE][ESIZE];
16     memset(res,0,sizeof(int) * ESIZE * ESIZE);
17     
18     char ch;
19     for(i = 0; i < ESIZE; ++i)
20     {
21         for(j = 0; j < ESIZE; ++j)
22         {
23             cin >> ch;
24             if(ch == '+')
25             {
26                 for(k = 0; k < ESIZE; ++k)
27                 {
28                     res[i][k] = 1 - res[i][k];
29                     res[k][j] = 1 - res[k][j];
30                 }
31                 res[i][j] = 1 - res[i][j];
32             }         
33         }
34     }
35     
36     int count = 0;
37     for(i = 0; i < ESIZE; ++i)
38     {
39         for(j = 0; j < ESIZE; ++j)
40         {
41             count += res[i][j];
42         }
43     }
44     cout << count << endl;
45     
46     for(i = 0; i < ESIZE; ++i)
47     {
48         for(j = 0; j < ESIZE; ++j)
49         {
50             if(res[i][j]) cout << (i + 1) << " " << (j + 1) << endl;
51         }
52     } 
53     
54     
55     return 0;
56 } 

5.Reference:

http://blog.sina.com.cn/s/blog_49e7d7350100rsao.html

http://zhidao.baidu.com/link?url=RrjLHGqp4X_u6KM6if9nhx9u4MjsfbDL9BESxe_zgno-IVlKV9ftJANgC45rrsbsiPSeiW29LTrEq0BCVFPYPa