結果

問題 No.367 ナイトの転身
ユーザー ctyl_0ctyl_0
提出日時 2016-04-29 23:17:13
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 3,734 bytes
コンパイル時間 840 ms
コンパイル使用メモリ 98,460 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 05:54:00
合計ジャッジ時間 3,728 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template
int nx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
int ny[8] = {1, -1, 2, -2, 2, -2, 1, -1};
int bx[4] = {1, 1, -1, -1};
int by[4] = {1, -1, 1, -1};
int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    int H, W;
    cin >> H >> W;
    vector<string> S(H);
    rep(i, H) cin >> S[i];
    pair<int, int> start, goal;
    rep(i, H) rep(j, W) {
        if (S[i][j] == 'S') start = mp(i, j);
        if (S[i][j] == 'G') goal = mp(i, j);
    }
    
    
    vector<vector<int>> visN(H, vector<int>(W, -1)), visB(H, vector<int>(W, -1));
    queue<pair<pair<int, int>, int>> q; // 0: knight, 1: bishop
    q.push(mp(start, 0));
    visN[start.first][start.second] = 0;
    while (!q.empty()) {
        auto f = q.front();
        int y = f.first.first;
        int x = f.first.second;
        int s = f.second;
        q.pop();
        if (s == 0) { // knight
            int cnt = visN[y][x];
            rep(k, 8) {
                if (x + nx[k] >= 0 && x + nx[k] < W && y + ny[k] >= 0 && y + ny[k] < H) {
                    if(S[x + nx[k]][y + ny[k]] == 'R' && visB[y + ny[k]][x + nx[k]] == -1) {
                        q.push(mp(mp(y + ny[k], x + nx[k]), 1));
                        visB[y + ny[k]][x + nx[k]] = cnt + 1;
//                        cout << y + ny[k] << ", " << x + nx[k] << ": " << cnt + 1 << ": " << 1 << endl;
                    }
                    if(S[x + nx[k]][y + ny[k]] != 'R' && visN[y + ny[k]][x + nx[k]] == -1) {
                        q.push(mp(mp(y + ny[k], x + nx[k]), 0));
                        visN[y + ny[k]][x + nx[k]] = cnt + 1;
//                        cout << y + ny[k] << ", " << x + nx[k] << ": " << cnt + 1 << ": " << 0 << endl;
                    }
                }
            }
        }
        if (s == 1) {
            int cnt = visB[y][x];
            rep(k, 4) {
                if (x + bx[k] >= 0 && x + bx[k] < W && y + by[k] >= 0 && y + by[k] < H) {
                    if(S[x + bx[k]][y + by[k]] == 'R' && visN[y + by[k]][x + bx[k]] == -1) {
                        q.push(mp(mp(y + by[k], x + bx[k]), 0));
                        visN[y + by[k]][x + bx[k]] = cnt + 1;
//                        cout << y + by[k] << ", " << x + bx[k] << ": " << cnt + 1 << ": " << 0 << endl;
                    }
                    if(S[x + bx[k]][y + by[k]] != 'R' && visB[y + by[k]][x + bx[k]] == -1) {
                        q.push(mp(mp(y + by[k], x + bx[k]), 1));
                        visB[y + by[k]][x + bx[k]] = cnt + 1;
//                        cout << y + by[k] << ", " << x + bx[k] << ": " << cnt + 1 << ": " << 1 << endl;
                    }
                }
            }
        }
    }
    if(visN[goal.first][goal.second] == -1 && visB[goal.first][goal.second] == -1) {
        output(-1, 0);
        return 0;
    }
    if(visN[goal.first][goal.second] == -1) visN[goal.first][goal.second] = inf;
    if(visB[goal.first][goal.second] == -1) visB[goal.first][goal.second] = inf;
    
    output(min(visN[goal.first][goal.second], visB[goal.first][goal.second]), 0);
    
    
    
    return 0;
}
0