您的位置:首页 > 运维架构

Lightoj1202——Bishops(找规律)

2016-09-05 20:02 309 查看
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 Input

Case 1: 1

Case 2: impossible

Case 3: 2

国际象棋上走象,求一只到另一只最少要多少步。

棋盘上画一下就知道只有三种情况:1步,2步或者达不到。

棋盘可以分为两个区域,一个是坐标全为奇数或者偶数的地方,其他事另一个

达不到的条件是两个象分别在不同的区域内

1步的条件是两者在同一条对角线上

2步的条件是两者不在同一条对角线上

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
#define MAXN 2005
#define Mod 10001
using 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: