結果

問題 No.367 ナイトの転身
ユーザー Konton7Konton7
提出日時 2020-03-22 18:28:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 4,812 bytes
コンパイル時間 2,866 ms
コンパイル使用メモリ 221,796 KB
実行使用メモリ 6,144 KB
最終ジャッジ日時 2024-06-07 12:31:47
合計ジャッジ時間 4,172 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 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
6,016 KB
testcase_11 AC 26 ms
6,144 KB
testcase_12 AC 21 ms
5,376 KB
testcase_13 AC 14 ms
5,376 KB
testcase_14 AC 18 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 18 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 6 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 8 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:116:19: warning: 'sx' may be used uninitialized [-Wmaybe-uninitialized]
  116 |     knfield[sy][sx] = c;
      |                   ^
main.cpp:96:26: note: 'sx' was declared here
   96 |     int h, w, c = 0, sy, sx, gy, gx, y, x, dy, dx, l, ans;
      |                          ^~
main.cpp:116:15: warning: 'sy' may be used uninitialized [-Wmaybe-uninitialized]
  116 |     knfield[sy][sx] = c;
      |               ^
main.cpp:96:22: note: 'sy' was declared here
   96 |     int h, w, c = 0, sy, sx, gy, gx, y, x, dy, dx, l, ans;
      |                      ^~
main.cpp:166:23: warning: 'gx' may be used uninitialized [-Wmaybe-uninitialized]
  166 |     if (knfield[gy][gx] == -1 && bsfield[gy][gx] == -1) ans = -1;
      |                       ^
main.cpp:96:34: note: 'gx' was declared here
   96 |     int h, w, c = 0, sy, sx, gy, gx, y, x, dy, dx, l, ans;
      |                                  ^~
main.cpp:166:19: warning: 'gy' may be used uninitialized [-Wmaybe-uninitialized]
  166 |     if (knfield[gy][gx] == -1 && bsfield[gy][gx] == -1) ans = -1;
      |                   ^
main.cpp:96:30: note: 'gy' was declared here
   96 |     int h, w, c = 0, sy, sx, gy, gx, y, x, dy, dx, l, ans;
      |                              ^~

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using PII = std::pair<int, int>;
using PLL = std::pair<ll, ll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n, d) for (int i = (s); i < (int)(n); i += (d))
#define allpt(v) (v).begin(), (v).end()
#define allpt_c(v) (v).cbegin(), (v).cend()
#define allpt_r(v) (v).rbegin(), (v).rend()

const int mod = 1e9 + 7;
const string wsp = " ";
const string tb = "\t";
const string rt = "\n";

template <typename T>
void show1dvec(const vector<T> &v)
{
    if (v.size() == 0)
        return;
    int n = v.size() - 1;
    rep(i, n) cout << v[i] << wsp;
    cout << v[n] << rt;
    return;
}

template <typename T>
void show2dvec(const vector<vector<T>> &v)
{
    int n = v.size();
    rep(i, n) show1dvec(v[i]);
}

template <typename T, typename S>
void show1dpair(const vector<pair<T, S>> &v)
{
    int n = v.size();
    rep(i, n) cout << v[i].first << wsp << v[i].second << rt;
    return;
}

template <typename T, typename S>
void pairzip(const vector<pair<T, S>> &v, vector<T> &t, vector<T> &s)
{
    int n = v.size();
    rep(i, n)
    {
        t.push_back(v[i].first);
        s.push_back(v[i].second);
    }
    return;
}

template <typename T>
void maxvec(vector<T> &v)
{
    T s = v[0];
    int n = v.size();
    rep(i, n - 1)
    {
        if (s > v[i + 1])
        {
            v[i + 1] = s;
        }
        s = v[i + 1];
    }
}

template <typename T, typename S>
bool myfind(T t, S s)
{
    return find(t.cbegin(), t.cend(), s) != t.cend();
}

bool check(int y, int x, int h, int w)
{
    return 0 <= y && y < h && 0 <= x && x < w;
}

int main()
{

#ifdef DEBUG
    cout << "DEBUG MODE" << endl;
    ifstream in("input.txt"); //for debug
    cin.rdbuf(in.rdbuf());    //for debug
#endif

    int h, w, c = 0, sy, sx, gy, gx, y, x, dy, dx, l, ans;
    cin >> h >> w;
    char ch;
    vector<tuple<int, int, int>> search, new_search;
    vector<string> field(h);
    vector<VI> knfield(h, VI(w, -1)), bsfield(h, VI(w, -1));
    vector<PII> knmove, bsmove;
    rep3(i, -1, 3, 2) rep3(j, -1, 3, 2) bsmove.push_back(make_pair(i, j));
    rep3(i, -2, 4, 4) rep3(j, -1, 3, 2) knmove.push_back(make_pair(i, j));
    rep3(i, -1, 3, 2) rep3(j, -2, 4, 4) knmove.push_back(make_pair(i, j));
    sort(allpt(knmove)), sort(allpt(bsmove));

    rep(i, h) cin >> field[i];
    rep(i, h) rep(j, w)
    {
        if (field[i][j] == 'S')
            sy = i, sx = j;
        if (field[i][j] == 'G')
            gy = i, gx = j;
    }
    knfield[sy][sx] = c;
    search.push_back(make_tuple(sy, sx, 1));

    while (!search.empty())
    {
        c++;
        for (auto z : search)
        {
            y = get<0>(z), x = get<1>(z), l = get<2>(z);
            if (l == 1)
            {
                for (auto v : knmove)
                {
                    dy = get<0>(v), dx = get<1>(v);
                    if (check(y + dy, x + dx, h, w) && field[y + dy][x + dx] == 'R' && bsfield[y + dy][x + dx] == -1)
                    {
                        bsfield[y + dy][x + dx] = c;
                        new_search.push_back(make_tuple(y + dy, x + dx, 2));
                    }
                    else if (check(y + dy, x + dx, h, w) && field[y + dy][x + dx] != 'R' && knfield[y + dy][x + dx] == -1)
                    {
                        knfield[y + dy][x + dx] = c;
                        new_search.push_back(make_tuple(y + dy, x + dx, 1));
                    }
                }
            }
            else if (l == 2)
            {
                for (auto v : bsmove)
                {
                    dy = get<0>(v), dx = get<1>(v);
                    if (check(y + dy, x + dx, h, w) && field[y + dy][x + dx] == 'R' && knfield[y + dy][x + dx] == -1)
                    {
                        knfield[y + dy][x + dx] = c;
                        new_search.push_back(make_tuple(y + dy, x + dx, 1));
                    }
                    else if (check(y + dy, x + dx, h, w) && field[y + dy][x + dx] != 'R' && bsfield[y + dy][x + dx] == -1)
                    {
                        bsfield[y + dy][x + dx] = c;
                        new_search.push_back(make_tuple(y + dy, x + dx, 2));
                    }
                }
            }
        }

        search.clear();
        search = new_search;
        new_search.clear();
    }

    if (knfield[gy][gx] == -1 && bsfield[gy][gx] == -1) ans = -1;
    else if (knfield[gy][gx] == -1) ans = bsfield[gy][gx];
    else if (bsfield[gy][gx] == -1) ans = knfield[gy][gx];
    else ans = min(bsfield[gy][gx], knfield[gy][gx]);

    cout << ans << rt;

    return 0;
}
0