zoukankan      html  css  js  c++  java
  • C. Palindromic Paths

    You are given a matrix with nn rows (numbered from 11 to nn) and mm columns (numbered from 11 to mm). A number ai,jai,j is written in the cell belonging to the ii-th row and the jj-th column, each number is either 00 or 11.

    A chip is initially in the cell (1,1)(1,1), and it will be moved to the cell (n,m)(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)(x,y), then after the move it can be either (x+1,y)(x+1,y) or (x,y+1)(x,y+1)). The chip cannot leave the matrix.

    Consider each path of the chip from (1,1)(1,1) to (n,m)(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 tt (1t2001≤t≤200) — the number of test cases.

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

    Then nn lines follow, the ii-th line contains mm integers ai,1ai,1, ai,2ai,2, ..., ai,mai,m (0ai,j10≤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
    input
    Copy
    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
    
    output
    Copy
    0
    3
    4
    4
    
     
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    #include <unordered_set>
    #include <unordered_map>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    using namespace std;
    int dir[4][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod =1e9+7;
    const int N = 200005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }       
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            int m, n;
            cin >> n >> m;
            vector<vector<int>> a(n, vector<int>(m));
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    cin >> a[i][j];
                }
            }
            vector<vector<int>> cnt(n + m - 1,vector<int>(2));
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    cnt[i+j][a[i][j]]++;
                }
            }
            int res = 0;
            for (int i = 0; i < (n + m - 1)/2; i++)
            {
                int j = m + n - 2 - i;
                res += min(cnt[i][0] + cnt[j][0], cnt[i][1] + cnt[j][1]);
            }
            cout << res << endl;
        }
        return 0;
    }
  • 相关阅读:
    20200813质因数分解 --已知正整数n是两个不同的质数的乘积,试求出较大的那个质数 (奥赛一本通 P71 8)
    20200807求梯形面积,要求输入浮点数,输出精度为2位
    c++语言printf()输出格式大全 scanf()输入格式大全
    20200803给出一 名学生的语文和数学成绩,判断他是否恰好有一门课不及格(<60分),如果是输出1;否则输出0(奥赛一本通 p32 10)
    20200803-判断一个数能否同时被3,5,7整除(奥赛一本通 p32 9)
    20200802--利用公式 e=1+1/1!+1/2!+...+1/n!,求e的值, 要求保留小数点后10位(奥赛一本通 p67 2)
    20200802 给定正整数n,求不大于n的正整数的阶乘的和(即求1!+2!+...+n!),输出阶乘的和 (奥赛一本通p67 1题)
    线程
    mysql逻辑架构
    《python网络数据采集》笔记2
  • 原文地址:https://www.cnblogs.com/dealer/p/13112219.html
Copyright © 2011-2022 走看看