poj2965-The Pilots Brothers' refrigerator

来源:互联网 发布:淘宝的二手手机可靠吗 编辑:程序博客网 时间:2024/06/06 08:50

通过寻找一定的规律来做的题,也是很简单的枚举 

题意是他们在玩一个游戏 会给你一个4*4的初始状态 + 为关 -为开, 当你转换其中一个开关的话这个开关所在的行和列都会发生转换 然后让你输出把所有的开关都转化为- 所需要的步数以及要转换的开关位置

下面是原题和题解:


The Pilots Brothers' refrigerator
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 25216 Accepted: 9738 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

61 11 31 44 14 34 4


解析:

这道题确实确实让我想起以前玩过的魔方,只转换某一位置 然后确保其他位置不动

假设4*4的一个面板 而我们移动(2,2)位置的话:

0100

0100

0100

1111

这是转换后的步数 移动一个位置会影响到其他的位置的状态 然而如果我们把(2,2)所在的行和列全都转换一次呢?

2422

2422

2422

4744

转换后的步数就会如上图所示, 然而同一个位置只有开和关两种状态,所以当一个位置的转换步数为偶数时即相当于没有发生转换,所以当我们把(2,2)所在行和列全部转换的话就相当于进行了下面的转换:

0000

0000

0000

0100

即只有(2,2)的位置发生了变化,所以因此只要把原本+位置的所在行列全部进行一次转换即可得到最终答案

玩过这个游戏的人都知道,同一个位置点击两个不管什么时候点击的都是相当于没有点,所以最终每一个位置的步数可以做模2处理


#include <iostream>using namespace std;int main(){    char sta[4][4];    int step[4][4]= {{0}};  //初始化步数为0    int ans=0;    for(int i=0; i<4; i++)        for(int j=0; j<4; j++)            cin>>sta[i][j];    for(int i=0; i<4; i++)    {        for(int j=0; j<4; j++)        {            if(sta[i][j] == '+')            {                step[i][j]++;                step[i][j] %= 2;    //经过分析 当一个位置转换两次与不转化相同 三次与一次相同 所以mod 2                for(int k=0; k<4; k++)                {                    step[i][k]++;                    step[k][j]++;                    step[i][k] %= 2;                    step[k][j] %= 2;                }//根据找到的规律 把同行同列的所有位置转换一次(步数+1)%2            }        }    }    for(int i=0; i<4; i++)        for(int j=0; j<4; j++)            if(step[i][j]) ans++;//计算一共有几个位置需要转换    cout<<ans<<endl;    for(int i=0; i<4; i++)    {        for(int j=0; j<4; j++)        {            if(step[i][j])            {                cout<<i+1<<" "<<j+1<<endl;//因为最初位置从0开始 而题意从1开始 所以输出修正+1            }        }    }}


0 0