zoukankan      html  css  js  c++  java
  • 棋盘上的距离

    棋盘上的距离

    问题描述:

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

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

    以下是思路分析

    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;
    	}
    }
  • 相关阅读:
    three.js raycaster射线碰撞的坑 (当canvas大小 不是屏幕大小是解决拾取物体的办法)
    如何去掉IE文本框后的那个X css代码
    解决input 有readonly属性 各个浏览器的光标兼容性问题
    centos的基本命令03(du 查看文件详情,echo清空文件内容)
    centos的 / ~
    centos的基本命令02
    centos的基本命令01
    关系性数据库和非关系型数据库
    绝对路径和相对路径的理解
    linux的目录和基本的操作命令
  • 原文地址:https://www.cnblogs.com/gcy77/p/4081710.html
Copyright © 2011-2022 走看看