結果

問題 No.643 Two Operations No.2
ユーザー e869120e869120
提出日時 2018-02-02 21:29:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,033 bytes
コンパイル時間 796 ms
コンパイル使用メモリ 86,252 KB
実行使用メモリ 7,544 KB
最終ジャッジ日時 2023-08-30 02:00:27
合計ジャッジ時間 1,601 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,320 KB
testcase_01 AC 5 ms
7,316 KB
testcase_02 AC 4 ms
7,468 KB
testcase_03 AC 4 ms
7,284 KB
testcase_04 AC 4 ms
7,260 KB
testcase_05 AC 5 ms
7,268 KB
testcase_06 AC 5 ms
7,312 KB
testcase_07 AC 5 ms
7,464 KB
testcase_08 AC 5 ms
7,308 KB
testcase_09 AC 4 ms
7,452 KB
testcase_10 AC 4 ms
7,308 KB
testcase_11 AC 4 ms
7,472 KB
testcase_12 AC 4 ms
7,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <functional>
#include <map>
using namespace std;

int dist[1009][1009]; queue<pair<int, int>>Q;

int main() {
	int a1, a2; cin >> a1 >> a2;
	for (int i = 0; i <= 1000; i++) {
		for (int j = 0; j <= 1000; j++) dist[i][j] = (1 << 30);
	}
	Q.push(make_pair(500 + a1, 500 + a2)); dist[500 + a1][500 + a2] = 0;
	while (!Q.empty()) {
		int p1 = Q.front().first, p2 = Q.front().second; Q.pop();
		if (dist[p2][p1] > dist[p1][p2] + 1) {
			dist[p2][p1] = dist[p1][p2] + 1;
			Q.push(make_pair(p2, p1));
		}
		int X = p1 - 500, Y = p2 - 500;
		int G1 = X + Y, G2 = X - Y;
		if (abs(G1) >= 500 || abs(G2) >= 500) continue;
		if (dist[G1 + 500][G2 + 500] > dist[p1][p2] + 1) {
			dist[G1 + 500][G2 + 500] = dist[p1][p2] + 1;
			Q.push(make_pair(G1 + 500, G2 + 500));
		}
	}
	int ret = (1 << 30);
	for (int i = 0; i <= 1000; i++) ret = min(ret, dist[i][i]);
	if (ret == (1 << 30)) ret = -1;
	cout << ret << endl;
	return 0;
}
0