結果

問題 No.367 ナイトの転身
ユーザー simansiman
提出日時 2021-10-21 17:21:33
言語 C++17(clang)
(14.0.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 2,553 bytes
コンパイル時間 4,156 ms
コンパイル使用メモリ 133,252 KB
実行使用メモリ 6,004 KB
最終ジャッジ日時 2023-10-21 11:57:09
合計ジャッジ時間 5,035 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 36 ms
5,864 KB
testcase_11 AC 49 ms
6,004 KB
testcase_12 AC 26 ms
4,652 KB
testcase_13 AC 21 ms
4,476 KB
testcase_14 AC 23 ms
4,420 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 22 ms
4,348 KB
testcase_17 AC 4 ms
4,348 KB
testcase_18 AC 5 ms
4,348 KB
testcase_19 AC 7 ms
4,348 KB
testcase_20 AC 4 ms
4,348 KB
testcase_21 AC 8 ms
4,348 KB
testcase_22 AC 2 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int DY[12] = {-2, -2, -1, -1, 1, 1, 2, 2, -1, -1, 1, 1};
const int DX[12] = {-1, 1, -2, 2, -2, 2, -1, 1, -1, 1, -1, 1};

const int KNIGHT = 0;
const int BISHOP = 1;

struct Node {
  int y;
  int x;
  int dist;
  int type;

  Node(int y = -1, int x = -1, int dist = -1, int type = -1) {
    this->y = y;
    this->x = x;
    this->dist = dist;
    this->type = type;
  }
};

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S;
  int sy, sx, gy, gx;

  for (int y = 0; y < H; ++y) {
    string row;
    cin >> row;

    S.push_back(row);

    for (int x = 0; x < W; ++x) {
      if (S[y][x] == 'S') {
        sy = y;
        sx = x;
      }
      if (S[y][x] == 'G') {
        gy = y;
        gx = x;
      }
    }
  }

  queue<Node> que;
  que.push(Node(sy, sx, 0, KNIGHT));
  int visited[H][W][2];
  memset(visited, -1, sizeof(visited));

  while (not que.empty()) {
    Node node = que.front();
    que.pop();

    if (visited[node.y][node.x][node.type] != -1) continue;
    visited[node.y][node.x][node.type] = node.dist;

    // fprintf(stderr, "(%d, %d, %d, %d)\n", node.y, node.x, node.dist, node.type);

    if (node.type == KNIGHT) {
      for (int direct = 0; direct < 8; ++direct) {
        int ny = node.y + DY[direct];
        int nx = node.x + DX[direct];
        if (ny < 0 || H <= ny || nx < 0 || W <= nx) continue;

        if (S[ny][nx] == 'R') {
          que.push(Node(ny, nx, node.dist + 1, BISHOP));
        } else {
          que.push(Node(ny, nx, node.dist + 1, KNIGHT));
        }
      }
    } else {
      for (int direct = 8; direct < 12; ++direct) {
        int ny = node.y + DY[direct];
        int nx = node.x + DX[direct];
        if (ny < 0 || H <= ny || nx < 0 || W <= nx) continue;

        if (S[ny][nx] == 'R') {
          que.push(Node(ny, nx, node.dist + 1, KNIGHT));
        } else {
          que.push(Node(ny, nx, node.dist + 1, BISHOP));
        }
      }
    }
  }

  if (visited[gy][gx][KNIGHT] == -1 && visited[gy][gx][BISHOP] == -1) {
    cout << -1 << endl;
  } else if (visited[gy][gx][KNIGHT] == -1) {
    cout << visited[gy][gx][BISHOP] << endl;
  } else if (visited[gy][gx][BISHOP] == -1) {
    cout << visited[gy][gx][KNIGHT] << endl;
  } else {
    cout << min(visited[gy][gx][KNIGHT], visited[gy][gx][BISHOP]) << endl;
  }

  return 0;
}
0