代码一:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <cstdio>
//#include <bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
using namespace std;
int t;
int n;
char a[105][105];
int b[105][105];
int yi[105][105], ling[105][105];
int d[105][105];
int ans;
void init()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
b[i][j] = 0;
yi[i][j] = ling[i][j] = d[i][j] = 0;
}
ans = 0;
}
int main()
{
cin >> t;
while (t--) {
cin >> n;
// cout << "n=" << n << endl;
init();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == '1') {
int cishu = 4, ii = i, jj = j;
while (cishu--)
{
b[ii][jj] = 1;
int tt = ii;
ii = jj;
jj = n - tt + 1;
}
}
}
}
/* cout << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << b[i][j];
}
cout << endl;
}*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
// cout << a[i][j] << " " << b[i][j] << endl;
if (b[i][j] == 1) {
if (a[i][j] == '0') {
ling[i][j]++;
int cishu = 4, ii = i, jj = j;
while (cishu--)
{
ling[ii][jj] = ling[i][j];
int tt = ii;
ii = jj;
jj = n - tt + 1;
}
}
else if (a[i][j] == '1') {
yi[i][j]++;
int cishu = 4, ii = i, jj = j;
while (cishu--)
{
yi[ii][jj] = yi[i][j];
int tt = ii;
ii = jj;
jj = n - tt + 1;
}
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
// cout << i << " " << j << endl;
// cout << yi[i][j] << " " << ling[i][j] << endl;
if (!d[i][j]) {
ans = ans + min(yi[i][j], ling[i][j]);
int cishu = 4, ii = i, jj = j;
while (cishu--)
{
d[ii][jj] = 1;
int tt = ii;
ii = jj;
jj = n - tt + 1;
}
}
}
}
cout << ans << endl;
}
return 0;
}
代码二:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <cstdio>
//#include <bits/stdc++.h>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <map>
typedef long long ll;
using namespace std;
int t;
int n;
char a[105][105];
int b[105][105];
int yi[105][105], ling[105][105];
int d[105][105];
int ans;
void init()
{
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
b[i][j] = 0;
yi[i][j] = ling[i][j] = d[i][j] = 0;
}
ans = 0;
}
int main()
{
cin >> t;
while (t--) {
cin >> n;
// cout << "n=" << n << endl;
init();
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> a[i][j];
if (a[i][j] == '1') {
b[i][j] = b[j][n - i + 1] = b[n - i + 1][n - j + 1] = b[n - j + 1][i] = 1;
}
}
}
/* cout << endl;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cout << b[i][j];
}
cout << endl;
}*/
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
// cout << a[i][j] << " " << b[i][j] << endl;
if (b[i][j] == 1) {
if (a[i][j] == '0') {
ling[i][j]++;
ling[j][n - i + 1] = ling[n - i + 1][n - j + 1] = ling[n - j + 1][i] = ling[i][j];
}
else if (a[i][j] == '1') {
yi[i][j]++;
yi[j][n - i + 1] = yi[n - i + 1][n - j + 1] = yi[n - j + 1][i] = yi[i][j];
}
}
}
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
// cout << i << " " << j << endl;
// cout << yi[i][j] << " " << ling[i][j] << endl;
if (!d[i][j]) {
ans = ans + min(yi[i][j], ling[i][j]);
d[j][n - i + 1] = d[n - i + 1][n - j + 1] = d[n - j + 1][i] = d[i][j] = 1;
}
}
}
cout << ans << endl;
}
return 0;
}
原题链接:https://codeforces.com/contest/1703/submission/164259119