結果

問題 No.367 ナイトの転身
ユーザー HachimoriHachimori
提出日時 2016-04-29 23:37:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 2,053 bytes
コンパイル時間 736 ms
コンパイル使用メモリ 65,508 KB
実行使用メモリ 5,896 KB
最終ジャッジ日時 2023-07-27 19:15:02
合計ジャッジ時間 1,755 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,696 KB
testcase_01 AC 3 ms
5,648 KB
testcase_02 AC 3 ms
5,632 KB
testcase_03 AC 3 ms
5,760 KB
testcase_04 AC 3 ms
5,596 KB
testcase_05 AC 3 ms
5,596 KB
testcase_06 AC 3 ms
5,600 KB
testcase_07 AC 3 ms
5,636 KB
testcase_08 AC 4 ms
5,588 KB
testcase_09 AC 3 ms
5,648 KB
testcase_10 AC 26 ms
5,724 KB
testcase_11 AC 33 ms
5,712 KB
testcase_12 AC 9 ms
5,644 KB
testcase_13 AC 9 ms
5,600 KB
testcase_14 AC 15 ms
5,624 KB
testcase_15 AC 4 ms
5,644 KB
testcase_16 AC 14 ms
5,652 KB
testcase_17 AC 5 ms
5,596 KB
testcase_18 AC 5 ms
5,896 KB
testcase_19 AC 5 ms
5,632 KB
testcase_20 AC 4 ms
5,604 KB
testcase_21 AC 5 ms
5,632 KB
testcase_22 AC 4 ms
5,604 KB
testcase_23 AC 3 ms
5,640 KB
testcase_24 AC 4 ms
5,616 KB
testcase_25 AC 4 ms
5,624 KB
testcase_26 AC 3 ms
5,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int BUF = 505;


class Data {
public:
    bool type;
    int r, c;
    Data(){}
    Data(bool type, int r, int c):
        type(type), r(r), c(c){}
};


int row, col;
int bgnR, bgnC;
int endR, endC;
bool isRed[BUF][BUF];


void read() {
    memset(isRed, 0, sizeof(isRed));
    
    cin >> row >> col;
    for (int i = 0; i < row; ++i) {
        for (int j = 0; j < col; ++j) {
            char ch;
            cin >> ch;
            isRed[i][j] = ch == 'R';

            if (ch == 'S') {
                bgnR = i;
                bgnC = j;
            }
            if (ch == 'G') {
                endR = i;
                endC = j;
            }
        }
    }
}


void work() {
    queue<Data> Q;
    int cost[2][BUF][BUF];
    
    memset(cost, -1, sizeof(cost));
    Q.push(Data(0, bgnR, bgnC));
    cost[0][bgnR][bgnC] = 0;

    
    while (!Q.empty()) {
        Data curr = Q.front();
        Q.pop();
        
        if (curr.r == endR && curr.c == endC) {
            cout << cost[curr.type][curr.r][curr.c] << endl;
            return;
        }
        
        // knight の動き
        static int kdr[] = {-2, -1,  1,  2,  2,  1, -1, -2};
        static int kdc[] = { 1,  2,  2,  1, -1, -2, -2, -1};
        
        // mini bishop の動き
        static int bdr[] = {-1, 1,  1, -1};
        static int bdc[] = { 1, 1, -1, -1};

        for (int i = 0; i < (curr.type == 0 ? 8 : 4); ++i) {
            int nr = curr.r + (curr.type == 0 ? kdr[i] : bdr[i]);
            int nc = curr.c + (curr.type == 0 ? kdc[i] : bdc[i]);
            
            if (!(0 <= nr && nr < row && 0 <= nc && nc < col)) continue;

            bool nType = isRed[nr][nc] ? !curr.type : curr.type;

            if (cost[nType][nr][nc] == -1) {
                cost[nType][nr][nc] = cost[curr.type][curr.r][curr.c] + 1;
                Q.push(Data(nType, nr, nc));
            }
        }
    }

    cout << -1 << endl;
}


int main() {
    read();
    work();
    return 0;
}
0