Palindromic Paths CodeForces - 1366C(思维+贪心)

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<bits/stdc++.h>
#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)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值