棋盘上的距离

问题描述:

写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。

  • 王:横、直、斜都可以走,但每步限走一格。
  • 后:横、直、斜都可以走,每步格数不受限制。
  • 车:横、竖均可以走,不能斜走,格数不限。
  • 象:只能斜走,格数不限。

以下是思路分析

  1. 王,王的情况最复杂

    1. X与Y的差相等,那么是 x0 与 x1 的差值;
    2. X的差与Y的差不等,分两大步完成:
      1. 第一大步,直走到最近一个与目标位置成对角线的位置。
      2. 第二大步,沿对角线走完。
  2. 后,最多只需两步就能完成,最少一步。

    1. 一步的情况:

      1. 起始位置与目标位置X,Y之一相等;
      2. 起始位置与目标位置X,Y差相同。
    2. 其他都是二步
  3. 车,

    1. X,Y之一相等,一步
    2. X,Y均不同,二步
  4. 象,象存在不可到达的情况

    1. 象的起始位置两个坐标值与目标位置两个坐标值差相等的情况下,一步
    2. 否则,只有在差的和是偶数的情况下可到达,两步
    3. 其他情况不可到达。

以下是程序:

public class Grids1657 {

	public static void main(String[] args) {
// TODO Auto-generated method stub
Grids1657 grids = new Grids1657();
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("输入数据的组数:");
int n = sc.nextInt(); // 数据组数
sc.nextLine();
for (int i = 0; i < n; i++) {
System.out.println("第" + (i + 1) + "组数据:");
String inputData = sc.nextLine();
char[] charData = inputData.toCharArray();
// System.out.println(inputData.length());
int a, b, c, d = 0;
a = grids.TransInt(charData[0]);
b = grids.TransInt(charData[1]);
c = grids.TransInt(charData[3]);
d = grids.TransInt(charData[4]);
System.out.println("King:" + grids.King(a, b, c, d));
System.out.println("Queen:" + grids.Queen(a, b, c, d));
System.out.println("Vehicle:" + grids.Vehicle(a, b, c, d));
if (grids.Prime(a, b, c, d) == 3)
System.out.println("Prime:Inf");
else
System.out.println("Prime:" + grids.Prime(a, b, c, d));
}
System.out.println("程序结束!");
} private int Queen(int x0, int y0, int x1, int y1) {
int result = 0;
if ((x0 - x1 == y0 - y1) || x0 == x1 || y0 == y1) {
result = 1;
} else {
result = 2;
}
return result;
} private int King(int x0, int y0, int x1, int y1) {
int step1,step2 = 0;
int result = 0;
if (x0 - x1 == y0 - y1)
result = Math.abs(x0 - x1);
else if (Math.abs(x0 - x1) < Math.abs(y0 - y1)) {
step1 = Math.abs(Math.abs(y0 - Math.abs(y0 - (x1 - x0))));
step2 = Math.abs(x1 - x0);
result = step1 + step2;
} else {
step1 = Math.abs(Math.abs(x0 - Math.abs(x0 - (y1 - y0))));
step2 = Math.abs(y1 - y0);
result = step1 + step2;
}
return result;
} private int Vehicle(int x0, int y0, int x1, int y1) {
int result = 0;
if (x0 == x1 || y0 == y1) {
result = 1;
} else
result = 2;
return result; } private int Prime(int x0, int y0, int x1, int y1) {
int result = 0;
if (x0 - x1 == y0 - y1) {
result = 1;
} else if (0 == (x0 - x1 + y0 - y1) % 2) {
result = 2;
} else
result = 3; // 不可到达,要转成“Inf”输出
return result; } private int TransInt(char ch) {
int ch03 = 0;
switch (ch) {
case ('a'):
ch03 = 1;
break;
case ('b'):
ch03 = 2;
break;
case ('c'):
ch03 = 3;
break;
case ('d'):
ch03 = 4;
break;
case ('e'):
ch03 = 5;
break;
case ('f'):
ch03 = 6;
break;
case ('g'):
ch03 = 7;
break;
case ('h'):
ch03 = 8;
break;
case ('1'):
ch03 = 1;
break;
case ('2'):
ch03 = 2;
break;
case ('3'):
ch03 = 3;
break;
case ('4'):
ch03 = 4;
break;
case ('5'):
ch03 = 5;
break;
case ('6'):
ch03 = 6;
break;
case ('7'):
ch03 = 7;
break;
case ('8'):
ch03 = 8;
break;
default:
break;
}
return ch03;
}
}

最新文章

  1. [转] 从知名外企到创业公司做CTO是一种怎样的体验?
  2. AbstractFactoryPattern(抽象工厂)
  3. input框只允许输入数字 --------20160705
  4. ubuntu 下python版本切换
  5. codevs 1197 Vigen&#232;re密码
  6. Cocos2d-x开发实例介绍特效演示
  7. Effective C++ 学习总结
  8. 使用OllyDbg从零开始Cracking CHM
  9. getWritableDatabase()与getReadableDatabase()的区别:
  10. JaveScript用二分法与普通遍历(冒泡)
  11. bzoj 4813: [Cqoi2017]小Q的棋盘
  12. python Is 与== 的坑
  13. 2018 ,请领取您Power BI 年终报告
  14. Mysql服务器处理客户端请求流程
  15. org.apache.catalina.LifecycleException: Failed to stop component(生命周期异常)
  16. Codeforces 1005 E2 - Median on Segments (General Case Edition)
  17. APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)
  18. SharePoint 压缩打包文件代码分享
  19. loadrunner中log的使用初步总结
  20. Android-有序广播

热门文章

  1. IOS编程教程(八):在你的应用程序添加启动画面
  2. [LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)
  3. LeetCode _ Word Break
  4. 【HDOJ】4504 威威猫系列故事——篮球梦
  5. .OCX、.dll文件注册命令Regsvr32的使用
  6. ik分词
  7. leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
  8. Oracle11g新特性之动态变量窥视
  9. Java static块
  10. mybati之day02