結果
問題 |
No.643 Two Operations No.2
|
ユーザー |
![]() |
提出日時 | 2018-02-02 21:46:58 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,315 bytes |
コンパイル時間 | 2,633 ms |
コンパイル使用メモリ | 77,576 KB |
実行使用メモリ | 54,396 KB |
最終ジャッジ日時 | 2024-12-31 07:23:48 |
合計ジャッジ時間 | 5,738 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 WA * 3 RE * 7 |
ソースコード
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++){ final int x = i - 100; final int y = j - 100; if(DP[i][j] > DP[i][j] + 1){ DP[i][j] = DP[i][j] + 1; update = true; } if(j - i >= 0 && j + i <= 200 && DP[j - i][j + i] > DP[i][j] + 1){ DP[j - i][j + i] = DP[i][j] + 1; update = true; } } } if(!update){ break; } } //System.out.println(solve(X, Y)); System.out.println(DP[Y][X] == INF ? -1 : DP[Y][X]); } }