[Blue Bridge Cup 2015 Preliminary] Moving distance

Insert picture description here
Insert picture description here
Because we cannot walk diagonally, we can use Manhattan distance to find the shortest distance
Insert picture description here

#include<stdio.h>
#include<math.h>

int main(){
    
    
	int w,m,n,xm,xn,ym,yn;
	while(scanf("%d %d %d",&w,&m,&n)!=EOF){
    
    
		xm=m/w;
		xn=n/w;
		if(m%w!=0){
    
    
			xm++;
		}
		if(n%w!=0){
    
    
			xn++;
		}
		if(xm%2==0){
    
    //m属于偶数列行 
			if(m%w==0)
				ym=1;
			else
				ym=w-m%w+1;
		}
		else{
    
    
			if(m%w==0)
				ym=w;
			else
				ym=m%w;
		}
		
		if(xn%2==0){
    
    
			if(n%w==0)
				yn=1;
			else
				yn=w-n%w+1;
		}
		else{
    
    	
			if(n%w==0)
				yn=w;
			else
				yn=n%w;
		}
		printf("%d\n",abs(xm-xn)+abs(ym-yn));
	}
}

Guess you like

Origin blog.csdn.net/banxiaCSDN/article/details/112921293
Recommended