Atcoder Beginner Contest 184C Super Ryuma Problem Solution

Topic link

First of all, it is worth noting that we can walk an infinite number of squares along the diagonal in one step.
Secondly, those who have played chess (or have done similar questions) should know that after dyeing the map alternately in black and white, you can only walk to the grid of the same color according to the method of walking diagonally, and it only takes two steps at most to get there. Any grid.
Then, in the worst case (the color of the start and end grids are different), we only need to go 3 3The goal can be reached in 3 steps: the
distance to a Manhattan is no more than3 33 different color grids, and then go diagonally twice.

Then, we start to classify the discussion.
Here we refer to the way of movement as walking diagonally and walking Manhattan respectively.

  • If ans=0 ans=0a n s=0 , the start and end points coincide
  • If ans = 1 ans=1a n s=1 , then the start and end points are on the same diagonal or Manhattan distance≤ 3 \le 33
  • If ans = 2 ans = 2a n s=2 , the grids at the start and end points are the same color, or the start point and the end point can be on the same diagonal line after walking Manhattan once
  • If the above conditions are not met, then ans = 3 ans=3a n s=3
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int Maxn=200000+10;
int x,y,u,v;
inline int read()
{
    
    
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
    
    if(ch=='-')w=-1;ch=getchar();}
	while(ch>='0' && ch<='9')s=(s<<3)+(s<<1)+(ch^48),ch=getchar();
	return s*w;
}
int main()
{
    
    
	x=read(),y=read(),u=read(),v=read();
	if(x==u && y==v){
    
    puts("0");return 0;}
	if(x-y==u-v || x+y==u+v){
    
    puts("1");return 0;}
	for(int i=x-3;i<=x+3;++i)
	for(int j=y-3;j<=y+3;++j)
	{
    
    
		if(abs(x-i)+abs(y-j)>3)continue;
		if(i==u && j==v){
    
    puts("1");return 0;}
	}
	if((abs(x-u)+abs(y-v))%2==0)
	{
    
    puts("2");return 0;}
	for(int i=x-3;i<=x+3;++i)
	for(int j=y-3;j<=y+3;++j)
	{
    
    
		if(abs(x-i)+abs(y-j)>3)continue;
		if(i-j==u-v || i+j==u+v){
    
    puts("2");return 0;}
	}
	puts("3");
	return 0;
}

Guess you like

Origin blog.csdn.net/Brian_Pan_/article/details/109966650