Poj 2965 the pilots brothers 'refrigerator

Source: Internet
Author: User

1. Link:

Http://poj.org/problem? Id = 2965

2. content:

The pilots brothers 'refrigerator
Time limit:1000 ms   Memory limit:65536 K
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 records 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 rowIAnd all handles in ColumnJ.

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

61 11 31 44 14 34 4

Source

Northeastern Europe 2004, western subregion

3. Method:

This question is only 4*4, so there are 16 ^ 2 cases. You can consider the enumeration method (add backtracing)

But this question has a more clever way from: http://blog.sina.com.cn/s/blog_49e7d7350100rsao.html

 


Let's look at a simple question first. How can we change '+' to '-' without changing the status of other locations? The answer is to update all handle in the position (I, j) and the row (I) and column (j). As a result, the location is updated seven times, the handle of the corresponding row (I) and column (j) is updated four times, and the remaining is updated two times. handle updated even times does not change the final state. therefore, an efficient solution is obtained. Each time the input encounters '+', the position and corresponding rows and columns are automatically added. When the input ends, the array is traversed, all the odd positions are the operation positions, while the sum of the odd positions is the final operation count.

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

Poj 2965 the pilots brothers 'refrigerator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.