1366C
题意:找到所有从1,1点到n,m点的路径并且使得所有路径都是回文数字的最小操作次数;
思路:可以发现,从起点开始走k步,他走到的点是确定的,可以把这个当作是一个集合;一共会走n+m-2步,只需要是每一步的集合中的数组全部相等即可;
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
inline int read() {
int date = 0, w = 1; char c = 0;
while (c < '0' || c > '9') {if (c == '-')w = -1; c = getchar();}
while (c >= '0' && c <= '9') {date = date * 10 + c - '0'; c = getchar();}
return date * w;
}
inline void out(int x) {
if (x < 0) {putchar('-'); x = -x;}
if (x > 9) out(x / 10);
putchar(x % 10 + '0');
}
#define x first
#define y second
#define cn cout<<endl
const int N = 50;
int g[N][N];
int f[N * 2][2];
int main() {
int t = read();
while (t--) {
int n = read(), m = read();
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
g[i][j] = read();
int step = m + n - 2;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int s = i + j - 2;
f[s][g[i][j]]++;
}
}
int res = 0;
for (int i = step, j = 0; j < i; j++, i--) {
res += min(f[i][0] + f[j][0], f[i][1] + f[j][1]);
}
cout << res << endl;
memset(f, 0, sizeof f);
}
return 0;
}