链接:https://codeforces.com/contest/1366/problem/C
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&nbs***bsp;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)&nbs***bsp;(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 (1≤t≤2001≤t≤200) — the number of test cases.
The first line of each test case contains two integers nn and mm (2≤n,m≤302≤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 (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.
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
Note
The resulting matrices in the first three test cases:
(1011)(1101)
(000000)(000000)
⎛⎝⎜101011111101111110101⎞⎠⎟(101111101101101111101)
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200000+10,M=1e4+5,mod=1e9+7;
ll n,t,m,x,b,ans,l,r,s,u,v,y;
ll a[100][100],f[100][100];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>m;
memset(f,0,sizeof(f));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
if(a[i][j]==1)
f[i+j][1]++;
else
f[i+j][0]++;
}
}
ans=0;
x=2;
y=n+m;
while(1)
{
if(x==y)
break;
ans=ans+min(f[x][0]+f[y][0],f[x][1]+f[y][1]);
x++;
y--;
if(x>y)
break;
}
cout<<ans<<endl;
}
}