C. Palindromic Paths

最近比赛结果很糟糕,开始刷cf的题。

time limit per test1.5 seconds memory limit per test256 megabytes
inputstandard input outputstandard output You are given a matrix with
n rows (numbered from 1 to n) and m columns (numbered from 1 to m). A
number ai,j is written in the cell belonging to the i-th row and the
j-th column, each number is either 0 or 1.

A chip is initially in the cell (1,1), and it will be moved to the
cell (n,m). During each move, it either moves to the next cell in the
current row, or in the current column (if the current cell is (x,y),
then after the move it can be either (x+1,y) or (x,y+1)). The chip
cannot leave the matrix.

Consider each path of the chip from (1,1) to (n,m). A path is called
palindromic if the number in the first cell is equal to the number in
the last cell, the number in the second cell is equal to the number in
the second-to-last cell, and so on.

Your goal is to change the values in the minimum number of cells so
that every path is palindromic.

Input The first line contains one integer t (1≤t≤200) — the number of
test cases.

The first line of each test case contains two integers n and m
(2≤n,m≤30) — the dimensions of the matrix.

Then n lines follow, the i-th line contains m integers ai,1, ai,2,
…, ai,m (0≤ai,j≤1).

Output For each test case, print one integer — the minimum number of
cells you have to change so that every path in the matrix is
palindromic.

Example inputCopy 4 2 2 1 1 0 1 2 3 1 1 0 1 0 0 3 7 1 0 1 1 1 1 1 0 0
0 0 0 0 0 1 1 1 1 1 0 1 3 5 1 0 1 0 0 1 1 1 1 0 0 0 1 0 0 outputCopy 0
3 4 4 Note The resulting matrices in the first three test cases:

具体做法分别从矩形的左上角右下角,斜向右下方斜向左上方读取格子,直到两指针碰撞为止,最小替换数为读取格子中的0的个数,1的个数的较小者的累加。也许我比较菜,具体写代码过程遇到许多问题。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
void initvectorll(vector<ll> &a, ll n)
{
    for (ll i = 0; i < n; i++)
    {
        ll t;
        cin >> t;
        a.push_back(t);
    }
}
void showll(vector<ll> &a, ll beg, ll enl)
{
    auto it = a.begin() + beg;
    auto its = a.begin() + enl;
    for (it; it <= its; it++)
        cout << *it << ' ';
    cout << endl;
}
void T_blocks(vector<ll>&a,ll m,ll n)
{
    vector<ll>d;
    for(int i=1;i<=a.size();i++)
        d.push_back(a[i-1]);
    a.clear();
    for(int  i=0;i<m;i++)
        for(int t=n,index=i;t;t--,index+=m)
    {
        a.push_back(d[index]);
    }
}
int main()
{
    int n;
    cin >> n;
    vector<ll> pali;
    while (n--)
    {
        ll m, n;
        cin >> n >> m;
        
        initvectorll(pali, n * m);if(n>m){T_blocks(pali,m,n);swap(m,n);}//转置矩阵
        ll maxx = 0;
        for (int i = 0, j = m * n - 1; i < (m + n - 1) / 2; i++, j--)
        {
            ll O = 0, I = 0;
            for (int k = min((long long)(i + 1),n), i2 = i, j2 = j; k; k--, i2 += (m - 1), j2 -= (m - 1))
            {
                if (pali[i2])
                    I++;
                else
                    O++;
                if (pali[j2])
                    I++;
                else
                    O++;
            }
            maxx += min(O, I);
        }
      
        cout << maxx << endl;
        pali.clear();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值