結果

問題 No.38 赤青白ブロック
ユーザー d2verbd2verb
提出日時 2017-09-12 23:14:19
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 307 ms / 5,000 ms
コード長 1,306 bytes
コンパイル時間 1,839 ms
コンパイル使用メモリ 167,608 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-23 13:10:06
合計ジャッジ時間 7,715 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) REP(i,0,n)
#define INF (1<<30)
#define INFLL (1LL<<62LL)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int dx[8] = {0, 1, 0, -1, 1, -1, 1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, -1, 1};

int Kr, Kb;

bool is_valid(string & TS) {
  rep(i, TS.size()) {
    char c = TS[i];

    bool ok = true;
    switch (c) {
      case 'W':
        break;
      case 'R':
        if (i - Kr >= 0 && TS[i - Kr] == 'R')
          ok = false;
        if (i + Kr < TS.size() && TS[i + Kr] == 'R')
          ok = false;
        break;
      case 'B':
        if (i - Kb >= 0 && TS[i - Kb] == 'B')
          ok = false;
        if (i + Kb < TS.size() && TS[i + Kb] == 'B')
          ok = false;
        break;
    }
    if (!ok) return false;
  }

  return true;
}

int solve(string & S, string TS, int idx) {
  if (idx >= S.size())
    return is_valid(TS) ? TS.size() : 0;

  if (S[idx] == 'W') return solve(S, TS + S[idx], idx + 1);
  else return max(solve(S, TS + S[idx], idx + 1), solve(S, TS, idx + 1));
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  string S;
  cin >> Kr >> Kb >> S;
  cout << solve(S, "", 0) << endl;
  return 0;
}
0