結果

提出番号 516
提出者 tone_back
言語 C++
提出日時 2017-07-21 23:14:54
問題名 (31)ロシアの旗 (Russian Flag)
結果 AC
点数 100%

テストケース

テストケース 結果 得点 実行時間 メモリ使用量
1 AC 100% 1ms 7520KB
2 AC 100% 2ms 7696KB
3 AC 100% 2ms 7696KB
4 AC 100% 2ms 8320KB
5 AC 100% 2ms 8320KB

ソースコード

#define _USE_MATH_DEFINES

#include <iostream>
#include <math.h>
#include <climits>
#include <algorithm>
#include <functional>
#include <vector>
#include <queue>
#include <map> 
#include <set>
#include <string>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
const ll MD = 1000000007;

/* sample input

*/

int main()
{
	int test = 0;
	int n, m;
	char stage[50][50];//n行m列
	int l_w[50] = {}, l_b[50] = {}, l_r[50] = {};
	int answer = 0;

	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		int tempw = 0, tempr = 0, tempb = 0;
		for (int j = 0; j < m; j++) {
			cin >> stage[i][j];
			if (stage[i][j] == 'W') {
				tempw++;
			}
			if (stage[i][j] == 'R') {
				tempr++;
			}
			if (stage[i][j] == 'B') {
				tempb++;
			}
		}
		l_w[i] = tempw;
		l_r[i] = tempr;
		l_b[i] = tempb;
	}

	if (test) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cout << stage[i][j];
			}
			cout << endl;
		}
	}

	int cw = 0, cr = 0, cb = 0;

	//1行
	for (int j = 0; j < m; j++) {
		if (stage[0][j] == 'W') {
			cw++;
		}
		if (stage[0][j] == 'R') {
			cr++;
		}
		if (stage[0][j] == 'B') {
			cb++;
		}
	}
	answer += (m - cw);

	cw = 0;
	cr = 0;
	cb = 0;

	//n行
	for (int j = 0; j < m; j++) {
		if (stage[n-1][j] == 'W') {
			cw++;
		}
		if (stage[n - 1][j] == 'R') {
			cr++;
		}
		if (stage[n - 1][j] == 'B') {
			cb++;
		}
	}

	answer += (m - cr);


	int ans_middle = INT_MAX;
	//i行目からj行目まで青に
	for (int i = 1; i <= n - 2; i++) {
		for (int j = i; j <= n - 2; j++) {
			int temp = 0;
			for (int k = 1; k < n - 1; k++) {
				if (k < i) {//白
					temp += (m - l_w[k]);
				}
				else if (i <= k&&k <= j) {//青
					temp += (m - l_b[k]);
				}
				else {//赤
					temp += (m - l_r[k]);
				}
			}
			ans_middle = min(temp, ans_middle);
		}
	}

	answer += ans_middle;

	cout << answer;

	return 0;



}