結果

問題 No.367 ナイトの転身
ユーザー siguma323siguma323
提出日時 2016-04-30 12:36:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 2,720 bytes
コンパイル時間 931 ms
コンパイル使用メモリ 89,252 KB
実行使用メモリ 5,504 KB
最終ジャッジ日時 2024-04-19 07:43:11
合計ジャッジ時間 2,027 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 3 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 19 ms
5,376 KB
testcase_11 AC 26 ms
5,504 KB
testcase_12 AC 16 ms
5,376 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 14 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 4 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 4 ms
5,376 KB
testcase_21 AC 6 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:66:21: warning: 'start_x' may be used uninitialized [-Wmaybe-uninitialized]
   66 |     q.push(make_pair(start_x,start_y));
      |            ~~~~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:52:9: note: 'start_x' was declared here
   52 |     int start_x,start_y;
      |         ^~~~~~~
main.cpp:66:21: warning: 'start_y' may be used uninitialized [-Wmaybe-uninitialized]
   66 |     q.push(make_pair(start_x,start_y));
      |            ~~~~~~~~~^~~~~~~~~~~~~~~~~
main.cpp:52:17: note: 'start_y' was declared here
   52 |     int start_x,start_y;
      |                 ^~~~~~~

ソースコード

diff #

#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
#include <cmath>
#include <bitset>
#include <cstring>

using namespace std;

using ull = unsigned long long;

bool used[2][500][500];

int ans = 1000000;
int night_dx[8] = {2,1,1,2,-1,-1,-2,-2};
int night_dy[8] = {1,2,-2,-1,2,-2,-1,1};
int dx[4] = {-1,1,-1,1};
int dy[4] = {1,-1,-1,1};
int h,w;



bool InBounds(pair<int,int> test)
{
    if(test.first >=0 && test.second >=0 && test.first < w && test.second < h)
        return true;
    else return false;
}

int main()
{
    cin >> h >> w;
    vector<string> board(h);
    queue<pair<int,int>> q;
    vector<pair<int,int>> cash;
    queue<pair<int,int>> counter;
    int dp[500][500];

    for(auto&& x: dp)
        for(auto&& y : x)
            y = 100000;

    for(auto&& x : board)
    {
        cin >> x;
    }

    int start_x,start_y;
    for(int y = 0; y < h; ++y)
    {
        for(int x= 0; x < w; ++x)
        {
            if(board.at(y).at(x) == 'S')
            {
                start_x = x;
                start_y = y;
                break;
            }
        }
    }

    q.push(make_pair(start_x,start_y));
    counter.push(make_pair(0,true));

    while(!q.empty())
    {
        pair<int,int> test = q.front();
        q.pop();
        int num = counter.front().first;
        bool is_night = counter.front().second;
        counter.pop();

        if(board.at(test.second).at(test.first) == 'G')
        {
            ans = min(ans,num);
            continue;
        }

        if(board.at(test.second).at(test.first) == 'R')
        {
            is_night ^= true;
        }

        if(is_night)
        {
            for(int i =0; i < 8; ++i)
            {
                pair<int,int> tmp = make_pair(test.first + night_dx[i],test.second + night_dy[i]);
                if(InBounds(tmp) && !used[is_night][tmp.second][tmp.first])
                {
                    q.push(tmp);
                    counter.push(make_pair(num+1,is_night));
                    used[is_night][tmp.second][tmp.first] = true;
                }
            }
        }
        else
        {
            for(int i =0; i < 4; ++i)
            {
                pair<int,int> tmp = make_pair(test.first + dx[i],test.second + dy[i]);
                if(InBounds(tmp) && !used[is_night][tmp.second][tmp.first])
                {
                    q.push(tmp);
                    counter.push(make_pair(num+1,is_night));
                    used[is_night][tmp.second][tmp.first] = true;
                }
            }
        }
    }
    if(ans != 1000000)
        cout << ans << endl;
    else
        cout << -1 << endl;
}
0