結果

問題 No.643 Two Operations No.2
ユーザー uafr_csuafr_cs
提出日時 2018-02-02 21:52:29
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,465 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 74,128 KB
実行使用メモリ 56,216 KB
最終ジャッジ日時 2023-08-30 02:08:58
合計ジャッジ時間 4,696 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
56,040 KB
testcase_01 AC 133 ms
55,828 KB
testcase_02 AC 132 ms
56,048 KB
testcase_03 AC 131 ms
55,524 KB
testcase_04 WA -
testcase_05 AC 132 ms
56,080 KB
testcase_06 AC 131 ms
55,440 KB
testcase_07 AC 133 ms
56,216 KB
testcase_08 AC 132 ms
55,768 KB
testcase_09 WA -
testcase_10 AC 130 ms
55,672 KB
testcase_11 WA -
testcase_12 AC 131 ms
55,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {
	
	public static int solve(int X, int Y){
		if(X == Y){ 
			return 0;
		}else if(Y == 0){
			// (X, 0) -> (X, X)
			return 1;
		}else if(X == 0){
			// (0, Y) -> (Y, 0)
			return 1 + solve(Y, 0);
		}else if(Math.abs(X) != Math.abs(Y)){
			return -1;
		}else{
			return solve(X + Y, X - Y);
		}
	}
	
	public static final int INF = Integer.MAX_VALUE / 2 - 1;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int X = sc.nextInt();
		final int Y = sc.nextInt();
		
		int[][] DP = new int[100 + 1 + 100][100 + 1 + 100];
		for(int i = 0; i <= 200; i++){
			for(int j = 0; j <= 200; j++){
				DP[i][j] = (i == j) ? 0 : INF;
			}
		}
		
		while(true){
			boolean update = false;
			
			for(int i = 0; i <= 200; i++){
				for(int j = 0; j <= 200; j++){
					if(DP[j][i] > DP[i][j] + 1){
						DP[j][i] = DP[i][j] + 1; update = true;
					}
					
					final int y = i - 100;
					final int x = j - 100;
					final int nx = x + y;
					final int ny = x - y;
					
					final int ni = ny + 100;
					final int nj = nx + 100;
					if(ni >= 0 && ni <= 200 && nj >= 0 && nj <= 200 && DP[ni][nj] > DP[i][j] + 1){
						DP[ni][nj] = DP[i][j] + 1; update = true;
					}
				}
			}
			
			if(!update){ break; }
		}
		
		//System.out.println(solve(X, Y));
		System.out.println(DP[Y + 100][X + 100] == INF ? -1 : DP[Y + 100][X + 100]);
	}
}
0