結果

問題 No.367 ナイトの転身
ユーザー odan3240odan3240
提出日時 2016-04-29 23:44:57
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 836 ms / 2,000 ms
コード長 2,501 bytes
コンパイル時間 963 ms
コンパイル使用メモリ 100,348 KB
実行使用メモリ 35,072 KB
最終ジャッジ日時 2024-04-15 06:10:05
合計ジャッジ時間 4,460 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 442 ms
19,456 KB
testcase_11 AC 836 ms
35,072 KB
testcase_12 AC 308 ms
15,616 KB
testcase_13 AC 268 ms
15,744 KB
testcase_14 AC 272 ms
14,976 KB
testcase_15 AC 7 ms
6,944 KB
testcase_16 AC 237 ms
13,824 KB
testcase_17 AC 25 ms
6,944 KB
testcase_18 AC 36 ms
6,940 KB
testcase_19 AC 73 ms
6,944 KB
testcase_20 AC 29 ms
6,944 KB
testcase_21 AC 66 ms
6,948 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 4 ms
6,948 KB
testcase_24 AC 5 ms
6,940 KB
testcase_25 AC 4 ms
6,944 KB
testcase_26 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <tuple>
#include <complex>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
using P = pair<int, int>;
using State = tuple<P, int>;

P miniBishop[2] = {P(1, 1), P(1, -1)};
P kNight[4] = {P(2, 1), P(1, 2), P(-1, 2), P(-2, 1)};

int H, W;
bool valid(int x, int y) {
    if (x < 0 || y < 0) return false;
    if (x >= W || y >= H) return false;
    return true;
}
string maze[502];
map<State, int> dp;
queue<State> que;
void func(int y, int x, int dy, int dx, State &t) {
    // printf("debug: %d %d %d %d\n",y, x, dy,dx);
    int ny = y + dy;
    int nx = x + dx;
    if (valid(nx, ny)) {
        auto next = t;
        get<0>(next) = P(ny, nx);
        if (maze[ny][nx] == 'R') get<1>(next) ^= 1;
        if (!dp.count(next)) {
            dp[next] = dp[t] + 1;
            que.push(next);
        }
    }
}

int get(State s) {
    if (dp.count(s)) return dp[s];
    return INF;
}

int main() {
    cin >> H >> W;
    rep(i, H) cin >> maze[i];
    P s, g;
    rep(i, H) rep(j, W) if (maze[i][j] == 'S') s = P(i, j);
    rep(i, H) rep(j, W) if (maze[i][j] == 'G') g = P(i, j);

    que.push(make_tuple(s, 0));
    dp[make_tuple(s, 0)] = 0;

    while (que.size()) {
        auto t = que.front();
        que.pop();
        P p = get<0>(t);
        // printf("debug: %d %d %d\n", p.first, p.second, get<1>(t));
        if (get<1>(t) == 0) {
            rep(k, 4) {
                func(p.first, p.second, kNight[k].first, kNight[k].second, t);
                func(p.first, p.second, -kNight[k].first, -kNight[k].second, t);
            }
        } else {
            rep(k, 2) {
                func(p.first, p.second, miniBishop[k].first,
                     miniBishop[k].second, t);
                func(p.first, p.second, -miniBishop[k].first,
                     -miniBishop[k].second, t);
            }
        }
    }

    int ans = min(get(make_tuple(g, 0)), get(make_tuple(g, 1)));
    if (ans == INF)
        cout << "-1" << endl;
    else
        cout << ans << endl;

    return 0;
}
0