本文共 1571 字,大约阅读时间需要 5 分钟。
There is an Infinite chessboard. Two bishops are there. (Bishop means the chess piece that moves diagonally).
Now you are given the position of the two bishops. You have to find the minimum chess moves to take one to another. With a chess move, a bishop can be moved to a long distance (along the diagonal lines) with just one move.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.Each case contains four integers r1 c1 r2 c2 denoting the positions of the bishops. Each of the integers will be positive and not greater than 109. You can also assume that the positions will be distinct.
Output
For each case, print the case number and the minimum moves required to take one bishop to the other. Print ‘impossible’ if it’s not possible.Sample Input
3 1 1 10 10 1 1 10 11 1 1 5 3 Output for Sample InputCase 1: 1
Case 2: impossible Case 3: 2国际象棋上走象,求一只到另一只最少要多少步。
棋盘上画一下就知道只有三种情况:1步,2步或者达不到。 棋盘可以分为两个区域,一个是坐标全为奇数或者偶数的地方,其他事另一个 达不到的条件是两个象分别在不同的区域内 1步的条件是两者在同一条对角线上 2步的条件是两者不在同一条对角线上#include#include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f#define MAXN 2005#define Mod 10001using namespace std;int main(){ int t,cnt=1; scanf("%d",&t); while(t--) { long long r1,c1,r2,c2; cin>>r1>>c1>>r2>>c2; printf("Case %d: ",cnt++); if(abs(r1-r2)==abs(c1-c2)) printf("1\n"); else { if(r1%2==c1%2) { if(r2%2==c2%2) printf("2\n"); else printf("impossible\n"); } else { if(r2%2!=c2%2) printf("2\n"); else printf("impossible\n"); } } } return 0;}
转载地址:http://bpcvb.baihongyu.com/