Codeforces 1366C 贪心+思维

1366C
题意:找到所有从1,1点到n,m点的路径并且使得所有路径都是回文数字的最小操作次数;
思路:可以发现,从起点开始走k步,他走到的点是确定的,可以把这个当作是一个集合;一共会走n+m-2步,只需要是每一步的集合中的数组全部相等即可;

#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
inline int read() {
    int date = 0, w = 1; char c = 0;
    while (c < '0' || c > '9') {if (c == '-')w = -1; c = getchar();}
    while (c >= '0' && c <= '9') {date = date * 10 + c - '0'; c = getchar();}
    return date * w;
}
inline void out(int x) {
    if (x < 0) {putchar('-'); x = -x;}
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}
#define x first
#define y second
#define cn cout<<endl
const int N = 50;
int g[N][N];
int f[N * 2][2];
int main() {
    int t = read();
    while (t--) {
        int n = read(), m = read();
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= m; j++)
                g[i][j] = read();
        int step = m + n - 2;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                int s = i + j - 2;
                f[s][g[i][j]]++;
            }
        }
        int res = 0;
        for (int i = step, j = 0; j < i; j++, i--) {
            res += min(f[i][0] + f[j][0], f[i][1] + f[j][1]);
        }
        cout << res << endl;
        memset(f, 0, sizeof f);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值