結果

問題 No.2411 Reverse Directions
ユーザー SSRSSSRS
提出日時 2023-08-11 21:46:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 3,048 bytes
コンパイル時間 1,955 ms
コンパイル使用メモリ 181,588 KB
実行使用メモリ 8,448 KB
最終ジャッジ日時 2024-04-29 12:38:11
合計ジャッジ時間 3,832 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 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 12 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 10 ms
6,016 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 27 ms
8,448 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 27 ms
5,760 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 10 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 11 ms
5,376 KB
testcase_18 AC 7 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 8 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
testcase_22 AC 19 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 23 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 19 ms
5,632 KB
testcase_29 AC 32 ms
6,528 KB
testcase_30 AC 21 ms
5,888 KB
testcase_31 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
vector<int> dx = {1, 0, -1, 0};
vector<int> dy = {0, 1, 0, -1};
int main(){
  int H, W, K, L, R;
  cin >> H >> W >> K >> L >> R;
  L--;
  vector<string> S(H);
  for (int i = 0; i < H; i++){
    cin >> S[i];
  }
  if ((H + W) % 2 != K % 2){
    cout << "No" << endl;
  } else if ((R - L) % 2 == 1){
    cout << "No" << endl;
  } else {
    auto check = [&](int x, int y){
      if (0 <= x && x < H && 0 <= y && y < W){
        if (S[x][y] == '.'){
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    };
    auto bfs = [&](int sx, int sy){
      vector<vector<int>> d(H, vector<int>(W, -1));
      d[sx][sy] = 0;
      queue<pair<int, int>> Q;
      Q.push(make_pair(sx, sy));
      while (!Q.empty()){
        int x = Q.front().first;
        int y = Q.front().second;
        Q.pop();
        for (int i = 0; i < 4; i++){
          int x2 = x + dx[i];
          int y2 = y + dy[i];
          if (check(x2, y2)){
            if (d[x2][y2] == -1){
              d[x2][y2] = d[x][y] + 1;
              Q.push(make_pair(x2, y2));
            }
          }
        }
      }
      return d;
    };
    vector<vector<int>> d1 = bfs(0, 0);
    vector<vector<int>> d2 = bfs(H - 1, W - 1);
    int px = -1, py = -1, t = -1;
    for (int i = 0; i < H; i++){
      for (int j = 0; j < W; j++){
        for (int k = 0; k < 2; k++){
          if (d1[i][j] != -1 && d2[i][j] != -1){
            if (d1[i][j] <= L && d2[i][j] <= K - R && (L - d1[i][j]) % 2 == 0){
              if (check(i + dx[k], j + dy[k]) && check(i - dx[k], j - dy[k])){
                px = i;
                py = j;
                t = k;
              }
            }
          }
        }
      }
    }
    if (px == -1 && py == -1 && t == -1){
      cout << "No" << endl;
    } else {
      auto get_path = [&](vector<vector<int>> &d, int sx, int sy, int tx, int ty){
        vector<int> ans;
        while (tx != sx || ty != sy){
          for (int i = 0; i < 4; i++){
            if (check(tx - dx[i], ty - dy[i])){
              if (d[tx - dx[i]][ty - dy[i]] == d[tx][ty] - 1){
                tx -= dx[i];
                ty -= dy[i];
                ans.push_back(i);
                break;
              }
            }
          }
        }
        reverse(ans.begin(), ans.end());
        return ans;
      };
      vector<int> ans(K, -1);
      vector<int> p1 = get_path(d1, 0, 0, px, py);
      vector<int> p2 = get_path(d2, H - 1, W - 1, px, py);
      for (int i = 0; i < d2[px][py]; i++){
        p2[i] ^= 2;
      }
      for (int i = 0; i < d1[px][py]; i++){
        ans[i] = p1[i];
      }
      for (int i = 0; i < d2[px][py]; i++){
        ans[K - 1 - i] = p2[i];
      }
      for (int i = d1[px][py]; i < K - d2[px][py]; i++){
        ans[i] = t;
        if (i % 2 == 1){
          ans[i] ^= 2;
        }
      }
      cout << "Yes" << endl;
      for (int i = 0; i < K; i++){
        cout << "DRUL"[ans[i]];
      }
      cout << endl;
    }
  }
}
0