fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Global Round 29-A (Div. 1 + Div. 2) Shortest Increasing Path
최초 업로드: 2025-09-21 05:50:51
최근 수정 시간: 2025-09-21 05:50:51
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 6
# A. Shortest Increasing Path [문제 링크](https://codeforces.com/contest/2147/problem/A) ## Problem Statement You are at $(0, 0)$ in a rectangular grid and want to go to $(x, y)$. In order to do so, you are allowed to perform a sequence of steps. Each step consists of moving a positive integer amount of length in the positive direction of either the $x$ or the $y$ axis. The first step must be along the $x$ axis, the second along the $y$ axis, the third along the $x$ axis, and so on. Formally, if we number steps from one in the order they are done, then odd-numbered steps must be along the $x$ axis and even-numbered steps must be along the $y$ axis. Additionally, each step must have a length **strictly greater** than the length of the previous one. Output the minimum number of steps needed to reach $(x, y)$, or $-1$ if it is impossible. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first and only line of each case contains two integers $x$ and $y$ ($1 \le x, y \le 10^9$). ## Output For each test case, output the minimum number of steps to reach $(x, y)$ or $-1$ if it is impossible. ## 풀이 x좌표를 먼저 이동하기에 y가 크면 x -> y 순서로 2번만에 이동이 가능하고, y좌표가 1 이상이고 x-1이 y보다 크다면 1 -> y -> x-1 순서로 이동하여 도착할 수 있다. 나머지 경우에는 이동이 불가능하다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int x, y; cin >> x >> y; if(x<y) cout << "2\n"; else if(y>1 && x-1>y) cout << "3\n"; else cout << "-1\n"; } } ```