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
Input
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
0
3
4
4
Note
The resulting matrices in the first three test cases:
( 1 1 0 1 )
( 0 0 0 0 0 0 )
( 1 0 1 1 1 1 1 0 1 1 0 1 1 0 1 1 1 1 1 0 1 )
思路:枚举有可能成为第i个点和第n+m-i+1个点的点。这些点最终应该是一样,要么都是0,要么都是1.因为最上面的点,有可能遍历到下面的每一个点。数据量不大,我们直接暴力枚举每一个组序列的数就可以了。如下图所示,相同颜色的为同一组序列(i,n+m-i+1)
代码如下:
ps:我用的vector但是我感觉deque应该更好。
#include
#define ll long long
using namespace std;
const int maxx=33;
struct node{
int x,y;
node(int a,int b)
{
x=a,y=b;
}
bool operator <(const node &a)const{
if(x!=a.x) return x<a.x;
else return y<a.y;
}
bool operator ==(const node &a)const{
if(a.x==x&&a.y==y) return 1;
else return 0;
}
};
int a[maxx][maxx];
int n,m;
inline int judge(vector<node> p1,vector<node> p2,int x)
{
int ans=0;
for(int i=0;i<p1.size();i++) ans+=(a[p1[i].x][p1[i].y]!=x);
for(int i=0;i<p2.size();i++) ans+=(a[p2[i].x][p2[i].y]!=x);
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&a[i][j]);
int ans=0;
vector<node> p1,p2;
p1.push_back(node(1,1));
p2.push_back(node(n,m));
int nn=min(n,m);
int mm=max(n,m);
int num=nn-1+(mm-nn+1)/2;
int cnt=0;
while(cnt<num)
{
sort(p1.begin(),p1.end());
sort(p2.begin(),p2.end());
p1.erase(unique(p1.begin(),p1.end()),p1.end());
p2.erase(unique(p2.begin(),p2.end()),p2.end());
int min1=judge(p1,p2,0);
int min2=judge(p1,p2,1);
ans+=min(min1,min2);
int zz=p1.size();
for(int i=0;i<zz;i++)
{
node s=p1[i];
if(s.x+1<=n) p1.push_back(node(s.x+1,s.y));
if(s.y+1<=m) p1.push_back(node(s.x,s.y+1));
}
for(int i=0;i<zz;i++)
{
node s=p2[i];
if(s.x-1>0) p2.push_back(node(s.x-1,s.y));
if(s.y-1>0) p2.push_back(node(s.x,s.y-1));
}
p1.erase(p1.begin(),p1.begin()+zz);
p2.erase(p2.begin(),p2.begin()+zz);
cnt++;
}
cout<<ans<<endl;
}
return 0;
}
努力加油a啊,(o)/~