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步的条件是两者不在同一条对角线上
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; }
相关文章推荐
- 基于python开发hadoop shell
- LFCS 系列第十四讲: Linux 进程资源用量监控和按用户设置进程限制
- hadoop完全分布式安装
- openssh设置免密码登录远程服务器
- Mesos架构简介
- Uva11992 Fast Matrix Operations(线段树区间修改+更新)
- [置顶] 大型网站架构技能图谱(Java版)
- linux小结
- Linux Makefile教程
- 远程启动服务
- 在Linux用户空间做内核空间做的事情
- 笔记:后台设置网站广告
- Tomcat 6 --- 使用Jasper引擎解析JSP
- 实战Centos系统部署Codis集群服务
- 记录一下坑爹的IIS错误,网站无法加载JS和CSS
- 对于linux的软连接的设置
- linux使用flock文件锁解决crontab冲突问题
- linux 删除!号
- Linux I2C(一)之常用的几种实例化(i2c_client )
- sh文件运行提示 “/bin/bash^M: 坏的解释器: 没有那个文件或目录”