棋盘上的距离 - 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 ...
 
随机推荐
- ural 1090 In the Army Now
			
http://acm.timus.ru/problem.aspx?space=1&num=1090 #include <cstdio> #include <cstring&g ...
 - TCP粘包的拆包处理
			
因为TCP是流式处理的,所以包没有边界,必须设计一个包头,里面表示包的长度(一般用字节表示),根据这个来逐个拆包.如果对于发送/接收频率不高的话,一般也就不做拆包处理了,因为不大可能有粘包现象. 以下 ...
 - Java JDBC中,MySQL字段类型到JAVA类型的转换
			
1. 概述 在使用Java JDBC时,你是否有过这样的疑问:MySQL里的数据类型到底该选择哪种Java类型与之对应?本篇将为你揭开这个答案. 2. 类型映射 java.sql.Types定义了常 ...
 - leetcode-Rising Temperature
			
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
 - Hp vPv
 - python unicodeDecode error
			
混淆了 python2 里边的 str 和 unicode 数据类型. 0. 你需要的是让编码用实际编码而不是 ascii 1. 对需要 str->unicode 的代码,可以在前边写上 imp ...
 - 查询死锁和处理死锁(SqlServer)
			
-------------------查询死锁,极其引起的原因-------------------------------use master go create procedure sp_who_ ...
 - C#request 请求响应
			
/// <summary> /// 提交POST请求 /// </summary> /// <param name="url">提交地址< ...
 - (转)android之Fragment(官网资料翻译)
			
Fragment要点 Fragment作为Activity界面的一部分组成出现 可以在一个Activity中同时出现多个Fragment,并且,一个Fragment亦可在多个Activity中使用. ...
 - [Android] PorterDuff使用实例----实现新浪微博图片下载效果
			
先上效果图,如demo_sinaweibo.gif 由效果图,下半部分是简单的效果叠加,上半部分是新浪微博加载图片显示进度的效果,显示进度的半透明区域只与根据背景图的非透明区域叠加,背景图的透明区域仍 ...