結果

問題 No.608 God's Maze
ユーザー ei1333333ei1333333
提出日時 2017-12-08 10:04:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,571 bytes
コンパイル時間 2,287 ms
コンパイル使用メモリ 218,344 KB
実行使用メモリ 817,664 KB
最終ジャッジ日時 2024-05-07 05:20:51
合計ジャッジ時間 6,070 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 4 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 95 ms
12,672 KB
testcase_11 AC 31 ms
6,912 KB
testcase_12 AC 80 ms
11,904 KB
testcase_13 AC 67 ms
10,112 KB
testcase_14 AC 37 ms
7,552 KB
testcase_15 AC 64 ms
10,112 KB
testcase_16 AC 75 ms
11,904 KB
testcase_17 AC 42 ms
8,064 KB
testcase_18 AC 20 ms
5,632 KB
testcase_19 AC 70 ms
10,624 KB
testcase_20 AC 15 ms
5,376 KB
testcase_21 AC 21 ms
5,760 KB
testcase_22 AC 112 ms
12,928 KB
testcase_23 AC 80 ms
12,416 KB
testcase_24 AC 129 ms
14,848 KB
testcase_25 AC 19 ms
5,760 KB
testcase_26 AC 97 ms
12,544 KB
testcase_27 AC 30 ms
6,912 KB
testcase_28 AC 206 ms
19,072 KB
testcase_29 MLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

int solve(string S, int N)
{
  int right = -1, ret = 0;
  for(int i = N; i >= 0; i--) {
    if(S[i] == '#') right = i;
  }
  if(right == -1) return (INF);
  for(int i = N - 1; i >= right; i--) {
    S[i] = ".#"[S[i] == '.'];
    ++ret;
  }
  S += ".";
  int rest = count(begin(S), end(S), '#');
  while(rest > 0) {
    ++right;
    assert(right < S.size());
    ++ret;
    S[right] = ".#"[S[right] == '.'];
    if(S[right] == '.') rest--;
    else rest++;
    if(S[right - 1] == '#') {
      --right;
      ++ret;
      S[right] = ".#"[S[right] == '.'];
      if(S[right] == '.') rest--;
      else rest++;
    }
  }
  return (ret);
}

int main()
{
  int N;
  string S, T;
  cin >> N;
  cin >> S;

  if(N > 30) {
    T = S;
    reverse(begin(T), end(T));
    cout << min(solve(S, N - 1), solve(T, (int) S.size() - N)) << endl;
    return (0);
  }

  --N;
  queue< pair< int, string > > que;
  map< string, int > min_cost[S.size()];
  min_cost[N][S] = 0;
  que.emplace(N, S);
  while(!que.empty()) {
    int idx;
    string s;
    tie(idx, s) = que.front();
    que.pop();
    if(count(begin(s), end(s), '#') == 0) {
      cout << min_cost[idx][s] << endl;
      break;
    }
    for(int i = -1; i <= 1; i++) {
      if(i == 0) continue;
      if(idx + i < 0 || idx + i >= S.size()) continue;
      string p = s;
      p[idx + i] = ".#"[p[idx + i] == '.'];
      if(min_cost[idx + i].count(p)) continue;
      min_cost[idx + i][p] = min_cost[idx][s] + 1;
      que.emplace(idx + i, p);
    }
  }

}
0