Files
python/TangDou/LuoGuBook/P3392.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

68 lines
1.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
char a[N][N];
int MIN = 0x3f3f3f3f;
int n, m;
/**
* 功能:计算在白x行,蓝y行,红z行的情况下,修改的数量
* @param x 白色行数
* @param y 蓝色行数
* @param z 红色行数
* @return 修改的数量
*/
int check(int x, int y, int z) {
//检查现在前x行白的个数
int w = 0, r = 0, b = 0;
int s = 0;
//白的现有多少个?
for (int i = 1; i <= x; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'W') w++;
//修改量+ m*x-w个
s += m * x - w;
//蓝的现有多少个?
for (int i = x + 1; i <= x + y; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'B') b++;
//修改量+ m*y-b个
s += m * y - b;
//红的现有多少个?
for (int i = x + y + 1; i <= x + y + z; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'R') r++;
//修改量+ m*z-r个
s += m * z - r;
//返回当前情况下的修改量
return s;
}
int main() {
//n行m列的国旗
cin >> n >> m;
//读入现有的国旗
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
//白色,可能的范围是1行至n-2行(行数)
for (int i = 1; i <= n - 2; i++)
//蓝色,可能的范围是1行至n-2行(行数)
for (int j = 1; j <= n - 2; j++) {
//红色的行数
int k = n - i - j;
//如果红色不存在,那么就是没用的答案,j也没有必要再增大了,使用 continue无意义,直接break
if (k <= 0) break;
//在白i行,蓝j行,红k行的情况下,计算最小的修改量,取最后的最小值
MIN = min(MIN, check(i, j, k));
}
//输出最小值
cout << MIN << endl;
return 0;
}