codeforces-123B思维

> You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one.
A square (x; y) is considered bad, if at least one of the two conditions is fulfilled:
|x + y| ≡ 0 (mod 2a),
|x - y| ≡ 0 (mod 2b).
Your task is to find the minimum number of bad cells one will have to visit on the way from (x1; y1) to (x2; y2).
Input
The only line contains integers a, b, x1, y1, x2 and y2 — the parameters of the bad squares, the coordinates of the initial and the final squares correspondingly (2 ≤ a, b ≤ 109 and |x1|,|y1|,|x2|,|y2| ≤ 109). It is guaranteed that the initial and the final square aren't bad.
Output
Print a single number — the minimum number of bad cells that one will have to visit in order to travel from square (x1; y1) to square (x2; y2).

解题思路: 


给你两个平面内的点,坐标可能很大,但是不会超过int,在平面内可以上下左右走,问你从( x1 , y1 )走到( x2 , y2 )最少可能经过坏点的数量(坏点即为满足| x + y | % 2a ==0 或者| x - y | % 2b == 0) 
很明显的(可以在纸上稍微画个图),想要坏点最少,肯定就是在两个点之间走曲折的路线,而坏点所在的直线(我们称之为坏点线)其实是满足一定规律的,就是等间距(在跨象限的时候可能会有小小的不同)分布,这里盗个图用(点这里),该博客里面的图是不是很清晰呢(虽然走的方向有点问题,但是不得不说这图画的真是赞)? 
画到这里其实就已经很明显了,要使两点之间的路线坏点最少,那么是不是得走坏点线的交点!于是答案就很明显了。这里还要注意一个问题,就是跨象限的问题(x+y=0 和 x-y=0),不过有一个小技巧可以很神奇的解决


#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;

int main(){
	int a,b,x1,x2,y1,y2;
	cin>>a>>b>>x1>>y1>>x2>>y2;
	int x=x1;
	int y=y1;
	x1=x+y;
	y1=y-x;
	x=x2;
	y=y2;
	x2=x+y;
	y2=y-x;
	a*=2;
	b*=2;
	x1=x1/a+(x1>0);//巧妙的将(0,0)包含了进去 
	x2=x2/a+(x2>0);
	y1=y1/b+(y1>0);
	y2=y2/b+(y2>0);
	cout<<max(abs(y2-y1),abs(x2-x1))<<endl;
	return 0;
}
//            /\       |  /  |**、
//			 /  \      | /   |   \
//			/    \     |/    |   /  _____                      ____   |  /
//		   /------\    |\    |__/  /     \  \      /\      /  /    \  | /
//		  /        \   | \   |    /       \  \    /  \    /  /______\ |/
//		 /          \  |  \  |    \       /   \  /    \  /   \        |
//      /            \ |   \ |     \_____/     \/      \/     \_____  |
/**
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//
发布了186 篇原创文章 · 获赞 38 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43746332/article/details/104113811