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;
}

 

posted @ 2020-06-13 11:17  DeaL57  阅读(232)  评论(0编辑  收藏  举报