Poj-2965-the pilots brothers& #39; Refrigerator (High efficiency greedy!!) )

Source: Internet
Author: User

The pilots ' refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 19356 Accepted: 7412 Special Judge

Description

The game "The pilots brothers:following the stripy elephant" has a quest where a player needs to open a refrigerator.

There is handles on the refrigerator door. Every handle can is in one of the States:open or closed. The refrigerator is open if all handles is open. The handles is 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 are 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 is several solutions, you could give any one of the them.

Sample Input

-+-----------+--

Sample Output

61 11 31 44 14 34 4

Source

Northeastern Europe 2004, Western subregion



Look at what the ACM practice suggests, and what it says is an enumeration.

But I enumerated the half-day. I didn't get it.


Online search of other people's puzzle, are said to have efficient greedy algorithm. and figured it out for half a day.


Example:

-+--

----

----

----

To change this to a minus, you have to change the number of symbols in the row and column of the + sign all at once (the position of the + sign changes once)

So there are, for example, the following number of changes:

4744

2422

2422

2422

7 is the number of times the + position needs to be changed, and 4 is the number of changes required by the + number in the row and column (not including the + number)

The result is an efficient solution that calculates the number of times the row and column need to be changed when each input encounters ' + '.

When the input is finished, the array is traversed, all the odd positions are the position of the operation, and the sum of the odd positions is the last number of operations.

But such algorithms apply only if n is even.

PS: This problem will not have "impossible" situation.



AC Code:




Poj-2965-the pilots brothers& #39; refrigerator (efficient greedy!!) )

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.