Poj 2965: The pilots brothers 'refrigerator

Source: Internet
Author: User

The pilots brothers 'refrigerator
Time limit:1000 ms   Memory limit:65536 K
Total submissions:18080   Accepted:6855   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

It is also used to enumerate DFS .. Write to the question, you can first write http://blog.csdn.net/u013487051/article/details/37310817

The two are the same ..


# Include <cstdio> # include <cstring> # include <algorithm> # include <iostream> using namespace STD; bool door [6] [6] = {false}; int flag; int step; int X [105]; int y [105]; int all () {for (INT I = 1; I <= 4; I ++) for (Int J = 1; j <= 4; j ++) if (! Door [I] [J]) return 0; return 1;} void flip (int m, int N) // switch {for (INT I = 1; I <= 4; I ++) {Door [m] [I] =! Door [m] [I]; door [I] [N] =! Door [I] [N];} door [m] [N] =! Door [m] [N]; // return;} void DFS (int m, int N, int deep) {If (deep = step) {flag = All (); return;} If (flag | M = 5) return; flip (m, n ); // switch if (n <4) {x [Deep] = m; y [Deep] = N; DFS (m, n + 1, deep + 1 );} else {x [Deep] = m; y [Deep] = N; DFS (m + 1, 1, deep + 1);} flip (m, n ); // returns if (n <4) DFS (m, n + 1, deep); else DFS (m + 1, 1, deep); return ;} int main () {char STR; flag = 0; For (INT I = 1; I <= 4; I ++) for (Int J = 1; j <= 4; j ++) {CIN> STR; If (STR = '-') Door [I] [J] = true;} For (step = 0; step <= 16; Step ++) {DFS (1, 1, 0); If (FLAG) break;} If (FLAG) {cout <step <Endl; for (INT I = 0; I <step; I ++) cout <X [I] <"" <Y [I] <Endl ;} return 0 ;}

There is also a high-efficiency solution:


/* Refer to the efficient solution of experts:> proof: to change a '+' symbol to '-', the corresponding row and column operands must be odd. It can be proved that, if an operation is performed at each position of the row and column corresponding to the '+' position, only the symbol at the '+' position in the entire graph changes, and the rest will not change.> set a 4*4 integer array. The initial value is zero and is used to record the operands of each vertex. Then, add 1 to the positions of rows and columns on each '+, the result is calculated as mod 2 (because the result of an even operation on a vertex is the same as that of no operation, this is the inverse principle). Then, the> number in the integer array is the operand, position 1 is the position to be operated (other operations with the original operands of an even number are not performed because the operation has no effect) * ********************************** the Shanghai stock exchange can follow the above steps make the values of the array '-' ******************************* in in the above example, after adding 1 to the positions of all rows and columns, before modulo 2, perform operations on all positions in the given array status Number of stored operations. For example, if a [I] [J] = N, N operations are performed on (I, j). After all operations are completed, that is, the array is all. In fact, it is not a model 2 operation, and has done a lot of useless work. The above operation sequence does not affect the result. If there is a minimum step, this step must be included in the above operation. (To put it simply: the above operations already include all the operations that can change their positions.) The operation after mod 2 removes all useless operations, this operation also contains the minimum step. However, after mod 2 is removed from any or several steps, it is impossible to get all. (This also proves that, because the operation sequence is not affected, the minimum step is first performed and the result is '-'. If step m is left, then, after performing the M step operation in the array state of "-", an array state of "-" is also obtained. This operation can only be performed even times at the same position, it is in conflict with the previous model 2, so m = 0). Therefore, the operation after Model 2 is the operation of the minimum step. */

The Code is as follows:


#include <iostream>using namespace std;bool mark[4][4];char s[4][4];int main(){    int i,j,k;    int ci[16],cj[16];    int nas = 0;    memset(mark,0,sizeof(mark));for(i = 0;i < 4;i++)cin >> s[i];    for(i = 0;i < 4;i++)        for(j = 0;j < 4;j++)        {            char c = s[i][j];            if(c == '+')            {                mark[i][j] = !mark[i][j];                for(k = 0;k < 4;k++)                {                    mark[i][k] = !mark[i][k];                    mark[k][j] = !mark[k][j];                }            }        }    for(i = 0;i < 4;i++)        for(j = 0;j < 4;j++)            if(mark[i][j] == true)            {                ci[nas] = i + 1;                cj[nas] = j + 1;                nas ++;            }    printf("%d\n",nas);    for(i = 0;i < nas;i++)    {        printf("%d %d\n",ci[i],cj[i]);    }    return 0;}



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.