結果

問題 No.240 ナイト散歩
ユーザー elphe
提出日時 2024-08-01 17:15:01
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 949 bytes
コンパイル時間 941 ms
コンパイル使用メモリ 95,256 KB
最終ジャッジ日時 2025-02-23 19:40:54
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cmath>
#include <queue>

#define S  q.front().first
#define T  q.front().second

using namespace std;

int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int dist[13][13], i, j, X, Y;
    queue<pair<int, int>> q;
    const int di[] = { -2, -2, -1, -1, 1, 1, 2, 2 },
        dj[] = { -1, 1, -2, 2, -2, 2, -1, 1 };

    cin >> X >> Y;
    if (abs(X) > 6 || abs(Y) > 6)
    {
        cout << "NO\n";
        return 0;
    }

    for (i = 0; i != 13; ++i)
        for (j = 0; j != 13; ++j)
            dist[i][j] = -1;

    dist[6][6] = 0;
    q.emplace(6, 6);
    while (!q.empty() && dist[S][T] < 3)
    {
        for (i = 0; i != 8; ++i)
            if (dist[S + di[i]][T + dj[i]] == -1)
                dist[S + di[i]][T + dj[i]] = dist[S][T] + 1, q.emplace(S + di[i], T + dj[i]);
        q.pop();
    }

    if (dist[X + 6][Y + 6] != -1) cout << "YES\n";
    else cout << "NO\n";

    return 0;
}
0