棋盘上的距离 - Grids1657
棋盘上的距离
问题描述:
写一个程序,给定起始位置和目标位置,计算王、后、车、象从起始位置走到目标位置所需的最少步数。
- 王:横、直、斜都可以走,但每步限走一格。
- 后:横、直、斜都可以走,每步格数不受限制。
- 车:横、竖均可以走,不能斜走,格数不限。
- 象:只能斜走,格数不限。
以下是思路分析
王,王的情况最复杂
- X与Y的差相等,那么是 x0 与 x1 的差值;
- X的差与Y的差不等,分两大步完成:
- 第一大步,直走到最近一个与目标位置成对角线的位置。
- 第二大步,沿对角线走完。
后,最多只需两步就能完成,最少一步。
- 一步的情况:
- 起始位置与目标位置X,Y之一相等;
- 起始位置与目标位置X,Y差相同。
- 其他都是二步
- 一步的情况:
车,
- X,Y之一相等,一步
- X,Y均不同,二步
象,象存在不可到达的情况
- 象的起始位置两个坐标值与目标位置两个坐标值差相等的情况下,一步
- 否则,只有在差的和是偶数的情况下可到达,两步
- 其他情况不可到达。
以下是程序:
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;
}
}
棋盘上的距离 - Grids1657的更多相关文章
- OJ题解记录计划
容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001 A+B Problem First AC: 2 ...
- BZOJ2281:[SDOI2011]黑白棋(博弈论,组合数学,DP)
Description 小A和小B又想到了一个新的游戏. 这个游戏是在一个1*n的棋盘上进行的,棋盘上有k个棋子,一半是黑色,一半是白色. 最左边是白色棋子,最右边是黑色棋子,相邻的棋子颜色不同. 小 ...
- P2060 马步距离(洛谷)
我们无论遇到什么困难,都不要拖,微笑着面对他,战胜拖延的最好方法就是面对拖延. 今天又拖延了…… 早晨听完老师讲课,本想做一道题练练手的,结果因为懒,瘫了一上午.最后在固定的刷题时间去面对了这道题,然 ...
- iOS之计算上次日期距离现在多久, 如 xx 小时前、xx 分钟前等
/** * 计算上次日期距离现在多久 * * @param lastTime 上次日期(需要和格式对应) * @param format1 上次日期格式 * @para ...
- 挑子学习笔记:对数似然距离(Log-Likelihood Distance)
转载请标明出处:http://www.cnblogs.com/tiaozistudy/p/log-likelihood_distance.html 本文是“挑子”在学习对数似然距离过程中的笔记摘录,文 ...
- 字符串编辑距离(Levenshtein距离)算法
基本介绍 Levenshtein距离是一种计算两个字符串间的差异程度的字符串度量(string metric).我们可以认为Levenshtein距离就是从一个字符串修改到另一个字符串时,其中编辑单个 ...
- [LeetCode] Rearrange String k Distance Apart 按距离为k隔离重排字符串
Given a non-empty string str and an integer k, rearrange the string such that the same characters ar ...
- [LeetCode] Shortest Word Distance III 最短单词距离之三
This is a follow up of Shortest Word Distance. The only difference is now word1 could be the same as ...
- [LeetCode] Shortest Word Distance II 最短单词距离之二
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...
随机推荐
- 配置android开发环境eclipse获取ADT获取不到
在安装完Android SDK后eclipse要获取ADT, 可是由于GFW的存在, eclipse经常无法从http://dl-ssl.google.com/android/eclipse 获取到任 ...
- Thread 1 cannot allocate new log的问题分析
http://blog.csdn.net/zonelan/article/details/7613519 http://leoguan.blog.51cto.com/816378/584494 htt ...
- App项目升级Xcode7&iOS9(续) - This bundle is invalid. The bundle identifier contains disallowed characters
金田 iOS 9发布已经有2月有余,现在Xcode已经有升级到Xcode7.1,开发环境安装等一系列相关的流程,以及Xcode 7 & iOS 9升级相关的一些部分,在这里就不再多加赘述(详见 ...
- 【转】你应该知道的 10 个 VirtualBox 技巧与高级特性
原文网址:http://www.oschina.net/translate/10-virtualbox-tricks-and-advanced-features-you-should-know-abo ...
- 日积月累:EditText软键盘的显示和隐藏
在工作过程中,常常会遇见需要根据自己的需求,控制文本框的键盘显示和隐藏. 通过查阅Android文档,介绍可以通过在清单文件中<activity>元素中添加android:windowSo ...
- ubuntu下安装pdo扩展
ubuntu下安装好LAMP后默认情况没有安装mysql_pdo扩展,以下是安装 步聚,在终端输入以下命令 1.pecl search pdo 2.sudo pecl install pdo 当出现E ...
- Hello,world,l'm coming!
#include<studio.h> int main() { printf("Hello,Word!" l'm coming\n"); return0; }
- css——基础样式总结
颜色和单位的使用 颜色 用颜色的名字表示颜色,比如:red 用16进制表示演示 比如:#FF0000 用rgb数值表示颜色,rgb(红,绿,蓝),每个值都在0-255之间一般都用16进制表示颜色 单位 ...
- zookeeper[5] zookeeper集群配置及伪集群配置
参考:http://zookeeper.apache.org/doc/trunk/zookeeperStarted.html 集群配置: 1.配置文件conf/zoo.cfg,除了单机模式的配置之外, ...
- IOS XMPP
http://www.cnblogs.com/lmyhao/p/4120616.html