The Pilots Brothers' refrigerator

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

解题思路:

呃。。。这题被出题人归为BFS和DFS类了,可如果仔细想想该题,可以找出其中的规律。如果一个冰箱门改变状态,那么它所在的行和列都得改变状态,只有改变奇数次状态的门才真正改变状态,而改变偶数次的门就回到了之前的状态。所以我们只需把改变奇数次的当成改变了一次,而改变偶数次的我们当做没有改变过。当其中一个点需要改变,那么就把该点所在的行列上的每个点都改变一次,那么对于需要改变的点一共改变了7次,而该行该列其他的点都只改变了4次,其余剩下的点只改变了2次,都是偶数次相当于没变。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 10;
int main()
{
    char map[maxn][maxn];
    int count[maxn][maxn], total = 0;   // count用来记录每个点改变的次数
    memset(count, 0,sizeof(count));
    for(int i = 0; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            scanf("%c",&map[i][j]);
            if(map[i][j] == '+')    // 如果需要改变就改变所在的行与列上所有的点
            {
                for(int k = 0; k < 4; k++)
                    count[i][k]++;
                for(int k = 0; k < 4; k++)
                    count[k][j]++;
                count[i][j]--;  // 点(i,j)被改变了两次,需要减去一次
            }
        }
        getchar();
    }
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)
            if(count[i][j] % 2 != 0)   // 改变奇数次就算作改变了一次
                total++;         // 总共改变了几次
    printf("%d\n", total);
    for(int i = 0; i < 4; i++)
        for(int j = 0; j < 4; j++)
            if(count[i][j] % 2 != 0)
                printf("%d %d\n", i + 1, j + 1);  // 输出需要改变的点的坐标
    return 0;
}

The Pilots Brothers' refrigerator

时间: 2024-10-01 21:44:00

The Pilots Brothers' refrigerator的相关文章

The Pilots Brothers&#39; refrigerator - poj 2965

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20325   Accepted: 7830   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 handle

poj 2965 The Pilots Brothers&#39; refrigerator[ 枚举 ]

传送门:http://poj.org/problem?id=2965 思路:二进制枚举,递归输出路径.G++ 900+Ms勉强过,C++超时. 代码: #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map>

POJ 2965 The Pilots Brothers&#39; refrigerator

原题链接:http://poj.org/problem?id=2965 简单的dfs,因为是要求最小值,枚举深度来得到递归终止条件,和 poj1753很像,毕竟是放在一起的. #include <iostream> #include <cstdio> #include <queue> #include <cstring> #include <cmath> #include <cstdlib> #include <vector&g

poj2965 The Pilots Brothers&#39; refrigerator

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 refri

POJ 2965-The Pilots Brothers&#39; refrigerator(贪心+枚举)

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19464   Accepted: 7462   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 2965:The Pilots Brothers&#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K 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 o

【递归】POJ2965-The Pilots Brothers&#39; refrigerator

和POJ1753的方法一致,唯一不同的是需要记录路径,只需添加一个全集变量的path路径即可. 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int INF=100000; 5 int map[4][4]; 6 int path[4][4]; 7 int maxpath[4][4]; 8 int ans=INF; 9 10 void open(int step,int turn) 11

POJ 1965 The Pilots Brothers&#39; refrigerator 搜索

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18222   Accepted: 6936   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

poj2965The Pilots Brothers&#39; refrigerator DFS+枚举

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20858   Accepted: 8048   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o

POJ 2965 The Pilots Brothers&#39; refrigerator (想法题)

POJ 2965 题意: 输入一个形如: -+-- ---- ---- -+-- 4*4图案,+表示close,-表示open,定义一种操作为:改变某个单元格符号(+变-,-变+),同时单元格所在行与所在列的所有单元格符号都会发生改变. 求最少操作次数能使所有单元格内都是'-'.并输出要操作的单元格. 思路: 正常的做法和POJ 1573类似,dfs枚举即可,见code1. 这里提供一种高效的做法: 通过思考我们可以验证,某一个单元格内符号为'+',同时对其所在行与所在列的所有单元格进行操作(其