C. Palindromic Paths(迴文路徑+思維)

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,1ai,1, ai,2ai,2, ..., ai,mai,m (0≤ai,j≤10≤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.

 有一張m*n的 01 圖,要求判斷更改幾次才能將從(1,1)點到(n,m)點的所有路徑都變成迴文路徑(迴文路徑是第1步與倒數第1步上的元素相同,第二步如此,以此類推)

我們將每一步都看成一個集合,只需要比較所有的第一步和所有的倒數第一步全爲1或全爲0的最小值即可

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
#define debug(a) cout<<"*"<<a<<"*"<<endl
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> Pair;
const int inf=0x3f3f3f3f;
const int N=50+5;

    int n,m,t;
    int i,j,k;
    int a[N][N];
    int vis[N+N][2];//vis[i][0]表示第 i 步,位置爲 0 的點有多少 

int32_t main()
{
    IOS; string s;
    rush(){
        cin>>n>>m;
        int ans=0; ms(vis,0);
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                cin>>a[i][j];
                int num=i+j-2;
                vis[num][a[i][j]]++;
            }
        }
        for(i=0,j=n+m-2;j>i;i++,j--){
            ans+=min(vis[i][0]+vis[j][0],vis[i][1]+vis[j][1]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章