結果

問題 No.367 ナイトの転身
ユーザー outlineoutline
提出日時 2021-02-14 01:14:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,984 bytes
コンパイル時間 1,467 ms
コンパイル使用メモリ 145,872 KB
実行使用メモリ 5,604 KB
最終ジャッジ日時 2023-09-28 10:40:59
合計ジャッジ時間 2,790 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 3 ms
5,344 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

constexpr int knight_dx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
constexpr int knight_dy[8] = {2, -2, 2, -2, 1, -1, 1, -1};
constexpr int bishop_dx[4] = {1, 1, -1, -1};
constexpr int bishop_dy[4] = {1, -1, 1, -1};
int dp[505][505][2];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int H, W;
    cin >> H >> W;
    vector<string> s(H);
    int sx = 0, sy = 0, gx = 0, gy = 0;
    for(int i = 0; i < H; ++i){
        cin >> s[i];
        for(int j = 0; j < W; ++j){
            if(s[i][j] == 'S')  sx = i, sy = j;
            if(s[i][j] == 'G')  gx = i, gy = j;
        }
    }

    for(int i = 0; i < H; ++i){
        for(int j = 0; j < W; ++j){
            for(int k = 0; k < 2; ++k){
                dp[i][j][k] = INF;
            }
        }
    }
    dp[sx][sy][0] = 0;
    using node = tuple<int, int, int, int>;
    priority_queue<node, vector<node>, greater<node>> que;
    que.emplace(0, sx, sy, 0);
    int counter = 0;
    while(!que.empty()){
        counter += 1;
        if(counter > 10)    break;
        auto [d, x, y, t] = que.top();
        que.pop();
        if(d > dp[x][y][t]) continue;
        if(t == 0){
            for(int i = 0; i < 8; ++i){
                int nx = x + knight_dx[i], ny = y + knight_dy[i];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
                int nt = s[nx][ny] == 'R' ? (t ^ 1) : t;
                if(chmin(dp[nx][ny][nt], dp[x][y][t] + 1)){
                    que.emplace(dp[nx][ny][nt], nx, ny, nt);
                }
            }
        }
        else{
            for(int i = 0; i < 4; ++i){
                int nx = x + bishop_dx[i], ny = y + bishop_dy[i];
                if(nx < 0 || nx >= H || ny < 0 || ny >= W)  continue;
                int nt = s[nx][ny] == 'R' ? (t ^ 1) : t;
                if(chmin(dp[nx][ny][nt], dp[x][y][t] + 1)){
                    que.emplace(dp[nx][ny][nt], nx, ny, nt);
                }
            }
        }
    }

    int ans = min(dp[gx][gy][0], dp[gx][gy][1]);
    if(ans == INF)  ans = -1;
    cout << ans << endl;
    
    return 0;
}
0