結果

問題 No.643 Two Operations No.2
ユーザー treeonetreeone
提出日時 2018-02-02 21:46:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 1,550 bytes
コンパイル時間 1,296 ms
コンパイル使用メモリ 151,236 KB
実行使用メモリ 11,404 KB
最終ジャッジ日時 2023-08-30 02:06:31
合計ジャッジ時間 2,127 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,288 KB
testcase_01 AC 5 ms
11,260 KB
testcase_02 AC 6 ms
11,300 KB
testcase_03 AC 6 ms
11,356 KB
testcase_04 AC 6 ms
11,352 KB
testcase_05 AC 6 ms
11,296 KB
testcase_06 AC 6 ms
11,296 KB
testcase_07 AC 6 ms
11,316 KB
testcase_08 AC 6 ms
11,388 KB
testcase_09 AC 6 ms
11,404 KB
testcase_10 AC 6 ms
11,356 KB
testcase_11 AC 5 ms
11,324 KB
testcase_12 AC 5 ms
11,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> PP;
const int mod = 1000000007;
const int INF = 1e12;

int d[1010][1010];

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x, y;
    cin >> x >> y;
    x += 500; y += 500;
    rep(i, 0, 1010) rep(j, 0, 1010) d[i][j] = INF;
    d[x][y] = 0;    
    priority_queue<PP, vector<PP>, greater<PP> > q;
    q.push(PP(0, P(x, y)));
    while(!q.empty()){
        PP p = q.top(); q.pop();
        int nowd = p.first;
        int nowx = p.second.first;
        int nowy = p.second.second;
        int nextx = nowy + nowx - 500;
        int nexty = nowx - nowy + 500;
        // cout << nowd << " " << nowx << " " << nowy << " " << nextx << " " << nexty << endl;
        if(d[nowy][nowx] > nowd + 1){
            d[nowy][nowx] = nowd + 1;
            q.push(PP(nowd + 1, P(nowy, nowx)));            
        }
        if(nexty < 0 || nexty > 1000 || nextx < 0 || nextx > 1000) continue;
        if(d[nextx][nexty] > nowd + 1){
            d[nextx][nexty] = nowd + 1;
            q.push(PP(nowd + 1, P(nextx, nexty)));
        }
       
        if(nexty == nextx) break;
    }
    int ans = INF;
    rep(i, 0, 1010) chmin(ans, d[i][i]);
    if(ans == INF) ans = -1;
    cout << ans << endl;
}
0