結果

問題 No.367 ナイトの転身
ユーザー nanophoto12nanophoto12
提出日時 2016-05-07 23:33:37
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,181 bytes
コンパイル時間 1,060 ms
コンパイル使用メモリ 95,832 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-15 13:41:55
合計ジャッジ時間 2,482 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <iomanip>
using namespace std;

#define ll long long int
#define ti4 tuple<int,int,int,int>
#define pii pair<int,int>

int B[2][500][500];
int red[500][500];

int main()
{
    int H, W;
    cin >> H >> W;
    fill(&red[0][0], &red[0][0] + 500 * 500, 0);
    fill(&B[0][0][0], &B[0][0][0] + 2 * 500 * 500, 1e6);
    pii start;
    pii end;
    for(int i = 0;i < H;i++)
    {
        string s;
        cin >> s;
        for(int j = 0;j < W;j++)
        {
            if(s[j] == 'S')
            {
                start = make_pair(j, i);
            }
            else if(s[j] == 'G')
            {
                end = make_pair(j, i);
            }
            else if(s[j] == 'R')
            {
                red[j][i] = 1;
            }
        }
    }
    priority_queue<ti4, vector<ti4>, greater<ti4>> q;
    q.push(make_tuple(0, start.first, start.second, 0));
    B[0][start.second][start.first] = 0;
    while(!q.empty())
    {
        int count, x, y, board;
        tie(count, x, y, board) = q.top();
        //cout << count << "," << x << "," << y << "," << board << endl;
        q.pop();
        if(x == end.first && y == end.second)
        {
            cout << count << endl;
            return 0;
        }
        int nc = count + 1;
        if(board == 0)
        {
            pii shift[] = {make_pair(-1,-2),make_pair(-2,-1),make_pair(1,-2),make_pair(2,-1),
                make_pair(-1,2),make_pair(-2,1),make_pair(1,2),make_pair(2,1)};
            for(int i = 0;i < 8;i++)
            {
                int nx = x + shift[i].first;
                int ny = y + shift[i].second;
                if(nx >= 0 && nx < W && ny >= 0 && ny < H)
                {
                    if(B[board][ny][nx] <= nc)
                    {
                        continue;
                    }
                    B[board][ny][nx] = nc;
                    if(red[ny][nx])
                    {
                        q.push(make_tuple(nc, nx, ny, 1));
                    }
                    else
                    {
                        q.push(make_tuple(nc, nx, ny, 0));
                    }
                }
            }
        }
        else
        {
            pii shift[] = {make_pair(-1,-1),make_pair(1,-1),make_pair(-1,1),make_pair(1,1)};
            for(int i = 0;i < 4;i++)
            {
                int nx = x + shift[i].first;
                int ny = y + shift[i].second;
                if(nx >= 0 && nx < W && ny >= 0 && ny < H)
                {
                    if(B[board][ny][nx] <= nc)
                    {
                        continue;
                    }
                    B[board][ny][nx] = nc;
                    if(red[ny][nx])
                    {
                        q.push(make_tuple(nc, nx, ny, 0));
                    }
                    else
                    {
                        q.push(make_tuple(nc, nx, ny, 1));
                    }
                }
            }
            
        }
    }
    cout << -1 << endl;
    return 0;
}
0