zoukankan      html  css  js  c++  java
  • 2965 -- The Pilots Brothers' refrigerator

    The Pilots Brothers' refrigerator
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 27893   Accepted: 10802   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
     
     
    题意:
      给你4*4的矩阵。每个点有两种状态,+代表关,-代表开。每个点有一个操作就是该点所在行列所有状态翻转。问最少多少次可以全部打开,并且输出最少个数情况下翻转的点。
     
    解题思路:
      这道题跟1753 -- Flip Game很像,可以用1753的解题思路。
      这道题用了更简单的方法。
      遍历矩阵中每一个关闭的点,按照规则将其进行翻转,将他的反转次数++。重复上述操作,直到所有的点都是打开状态。
     1 #include<iostream>
     2 #include<stdio.h>
     3 using namespace std;
     4 char m[5][5];
     5 int Count[5][5];
     6 //判断状态
     7 bool Check()
     8 {
     9     for(int i=1;i<=4;i++)
    10         for(int j=1;j<=4;j++)
    11         {
    12             if(m[i][j] == '+') return 0;//没有被全部打开
    13         }
    14     return 1;
    15 }
    16 int getAns()
    17 {//计算结果
    18     int ans = 0;
    19     for(int i=1;i<=4;i++)
    20     {
    21         for(int j=1;j<=4;j++)
    22         {
    23             if(Count[i][j]%2 == 0)
    24             {
    25                 Count[i][j] = 0;
    26             }else{
    27                 Count[i][j] = 1;
    28                 ans++;
    29             }
    30         }
    31     }
    32     return ans;
    33 }
    34 int changeState()
    35 {
    36     for(int i=1;i<=4;i++)
    37         for(int j=1;j<=4;j++)
    38         {
    39             if(m[i][j] == '+')//开关为关闭状态
    40             {
    41                 m[i][j] = '-';
    42                 Count[i][j] += 1;
    43                 for(int k = 1;k<=4;k++)
    44                 {
    45                     if(m[i][k] == '+') {m[i][k] = '-';}
    46                     else{m[i][k] = '+';}
    47                 }
    48                 for(int k=1;k<=4;k++)
    49                 {
    50                     if(m[k][j] == '+') {m[k][j] = '-';}
    51                     else{m[k][j] = '+';}
    52                 }
    53                 if(Check())
    54                 {//进行结果输出
    55                     return getAns();
    56                 }
    57             }
    58         }
    59         if(Check() == 0) return changeState();
    60 }
    61 
    62 int main()
    63 {
    64     char c;
    65     //初始化
    66     while(true)
    67     {
    68         for(int i = 1;i<=4;i++)
    69             for(int j=1;j<=4;j++)
    70                 Count[i][j] = 0;
    71         for(int i=1;i<=4;i++)
    72         {
    73             for(int j=1;j<=4;j++)
    74             {
    75                 if((c = getchar()) == EOF) return 0;
    76                 m[i][j] = c;
    77             }
    78             c = getchar();
    79         }
    80 
    81         cout<<changeState()<<endl;
    82         for(int i=1;i<=4;i++)
    83         {
    84             for(int j=1;j<=4;j++)
    85             {
    86                 if(Count[i][j] == 1) cout<<i<<" "<<j<<endl;
    87             }
    88         }
    89     }
    90 
    91     return 0;
    92 }

      对于一个关闭的点,想要把他打开,首先翻转一下他自己,为了不影响其他的点他所在行列都得翻转一次。这样最后是奇数次的即为翻转点。
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 using namespace std;
     5 char m[5][5];
     6 int Count[5][5];
     7 //判断状态
     8 bool Check()
     9 {
    10     for(int i=1;i<=4;i++)
    11         for(int j=1;j<=4;j++)
    12         {
    13             if(m[i][j] == '+') return 0;//没有被全部打开
    14         }
    15     return 1;
    16 }
    17 int getAns()
    18 {//计算结果
    19     int ans = 0;
    20     for(int i=1;i<=4;i++)
    21     {
    22         for(int j=1;j<=4;j++)
    23         {
    24             if(Count[i][j]%2 == 0)
    25             {
    26                 Count[i][j] = 0;
    27             }else{
    28                 Count[i][j] = 1;
    29                 ans++;
    30             }
    31         }
    32     }
    33     return ans;
    34 }
    35 int main()
    36 {
    37     char c;
    38     while(true)
    39     {
    40         memset(Count,0,sizeof(Count));//初始化
    41         for(int i=1;i<=4;i++)
    42         {
    43             for(int j=1;j<=4;j++)
    44             {
    45                 if((c = getchar()) == EOF) return 0;
    46                 m[i][j] = c;
    47                 if(c == '+')
    48                 {
    49                     for(int k=1;k<=4;k++)
    50                     {
    51                         Count[k][j]++;
    52                         Count[i][k]++;
    53                     }
    54                     Count[i][j]--;
    55                 }
    56             }
    57             c = getchar();
    58         }
    59         ///打印结果
    60         cout<<getAns()<<endl;
    61         for(int i=1;i<=4;i++)
    62         {
    63             for(int j=1;j<=4;j++)
    64             {
    65                 if(Count[i][j] == 1) cout<<i<<" "<<j<<endl;
    66             }
    67         }
    68     }
    69 
    70     return 0;
    71 }

     
     
     
  • 相关阅读:
    PHP实现畅言留言板和网易跟帖样式
    关于MySql中自增长id设置初始值
    建议
    P3P解决cookie存取的跨域问题
    学习模板实例
    Mac 安装Bower
    webstorm for mac 破解步骤
    Mac上搭建php开发环境
    ios 开发之 -- 极光推送,发送自定义消息,进入制定页面
    ios开发之 -- 强制横屏
  • 原文地址:https://www.cnblogs.com/yxh-amysear/p/8405565.html
Copyright © 2011-2022 走看看