POJ 2965贪心神解

貌似和POj1753一样是一般都是用为位运算+枚举做的。但是捏。这里用了贪心算法很容易。怎么样才能做到只把当前位置的+改为-而不改变其它所有位置的符号呢。嗯。就是把当前位置所在的行和列所在的元素都反转一次。最后统计操作数是记数的位置就是要操作的位置。详见代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;

char m;
int num[5][5];

int main()
{
    memset(num, 0, sizeof(num));
    for (int i=0; i<4; ++i)
    {
        for (int j=0; j<4; ++j)
        {
            cin >> m;
            if (m == ‘+‘)
            {
                for (int k=0; k<4; ++k)
                {
                   num[i][k]++;
                   num[k][j]++;
                }
                num[i][j]--;
            }
        }
    }
    int tot = 0;
    for (int i=0; i<4; ++i)
    {
        for (int j=0; j<4; ++j)
        {
            tot += num[i][j] % 2;
        }
    }
    cout << tot << endl;
    for (int i=0; i<4; ++i)
    {
        for (int j=0; j<4; ++j)
        {
            if (num[i][j] % 2)
            {
                cout << i+1 << ‘ ‘ << j+1 << endl;
            }
        }
    }
    return 0;
}

时间: 2024-11-13 09:26:34

POJ 2965贪心神解的相关文章

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

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

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

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

poj 2965 The Pilots Brothers&#39; refrigerator(dfs 枚举 +打印路径)

链接:poj 2965 题意:给定一个4*4矩阵状态,代表门的16个把手,'+'代表关,'-'代表开,当16个把手都为开(即'-')时,门才能打开,问至少要几步门才能打开 改变状态规则:选定16个把手中的任意一个,可以改变其本身以及同行同列的状态(即若为开,则变为关,若为关,则变为开),这一次操作为一步. 分析:这题与poj 1753思路差不多,每个把手最多改变一次状态, 所有整个矩阵最多改变16次状态 思路:直接dfs枚举所有状态,直到找到目标状态 但是要打印路径,所有应在dfs时记录路径 注

【POJ 2965】 The Pilots Brothers&#39; refrigerator

[POJ 2965] The Pilots Brothers' refrigerator 跟1753(我博客里另一篇有讲)棋盘问题一个做法 预处理后用二进制(BFS)暴力枚举 用到异或运算 很方便 大大缩短代码量 代码如下 #include <cstdio> #include <queue> #include <cstring> #include <stack> #define INF 0x3f3f3f3f using namespace std; type

poj 2965 The Pilots Brothers&amp;#39; refrigerator

The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18040   Accepted: 6841   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: 18641   Accepted: 7145   Special Judge Description The game "The Pilots Brothers: following the stripy elephant" has a quest where a player needs to o