codeforce supplement

The title description
Insert picture description herehas an infinite two-dimensional grid. The robot stands in cell (0,0) and wants to reach cell (x,y). The following is a list of commands that the robot can execute.

Move north from cell (i,j) to (i,j+1).
Move east from cell (i,j) to (i+1,j).
Move south from cell (i,j) to (i,j-1).
Move westward from cell (i,j) to (i-1,j).
Keep it in cell (i, j).
The robot wants to reach cell (x, y) in as few instructions as possible. However, he cannot execute the same command two or more times in succession.

What is the minimum number of instructions from (0,0) to (x,y)?

enter

The first line contains an integer t (1≤t≤100)-the number of test cases.

Each subsequent line t contains two integers x and y (0≤x, y≤10^4)-the target coordinates of the robot.

Output

Print an integer for each test case-the minimum number of commands required for the robot to reach (x, y) from (0,0), and it is not allowed to execute two or more commands in a row.

Sample

Insert picture description here

Ideas

At the beginning, I wanted to search for it, but it turned out to be too difficult to process. In addition to recording the number of steps at each point, I also need to record which instruction the predecessor point performed. In fact, the problem is a simple math problem. If the coordinates of the target point (x, y) are assumed here, x>y, x>y+1, it will definitely not arrive without repeated execution of instructions, so wait in place. The number of instructions is x-(y+1) and the final answer is x+y+x-(y+1)=2x-1

AC code

#include<iostream>
#include<algorithm>
using namespace std;
int main() 
{
    
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
    
    
        int x,y;
        scanf("%d%d",&x,&y);
        if(x==y) cout<<x*2<<endl;
        else cout<<max(x,y)*2-1<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45327808/article/details/109845048