HDU 1372Knight Moves

 A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.

Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.

Input
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying “To get from xx to yy takes n knight moves.”.
Sample Input

e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6

Sample Output

To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.

题目大意:下国际象棋,骑士从一个位置走到另外一个位置所需最少的步数
解体思路:bfs,裸题
还是java代码,虽然我不喜欢java

import java.util.*;
public class Main{
    public static class Node{
        public int x,y,ans;
        public Node(){

        }
        public Node(int a,int b,int c){
            x=a;
            y=b;
            ans=c;
        }
    }
    public static Node make_node(int a,int b,int c){
        Node w=new Node(a,b,c);
        return w;
    }
    public static boolean cheak(int x,int y){
        if(x<=0||y<=0||x>=9||y>=9)
            return false;
        else
            return true;
    }
    public static void main(String[] args){
        int la,ra,lb,rb;
        int []dx={1,2,2,1,-1,-2,-2,-1};
        int []dy={2,1,-1,-2,-2,-1,1,2};
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            String text=in.nextLine();
            la=text.charAt(0)-'a'+1;
            ra=text.charAt(1)-'0';
            lb=text.charAt(3)-'a'+1;
            rb=text.charAt(4)-'0';
            Queue<Node> qu=new LinkedList<Node>();
            boolean [][] vist=new boolean[9][9];
            qu.offer(make_node(la,ra,0));
            vist[la][ra]=true;
            while(!qu.isEmpty()){
                int nx=qu.peek().x;
                int ny=qu.peek().y;
                int ans=qu.peek().ans;
                if(nx==lb&&ny==rb){
                    System.out.println("To get from "+text.substring(0, 2)+" to "+text.substring(3,5)
                    +" takes "+ans+" knight moves.");
                    break;
                }
                qu.poll();
                ans++;
                for(int i=0;i<8;i++){
                    int mx=nx+dx[i];
                    int my=ny+dy[i];
                    if(cheak(mx,my)&&!vist[mx][my]){
                        vist[mx][my]=true;
                        qu.offer(make_node(mx,my,ans));
                    }
                }
            }
        }
    }
}
【源码免费下载链接】:https://renmaiwang.cn/s/9a9wv 西门子1FK7系列伺服电机作为工业自动化领域的核心设备,在精确控制机械运动与位置方面发挥着重要作用。该系列产品集成了多项先进技术,展现出卓越的性能与可靠性,并广泛应用于机器人技术、机床制造、包装机械等多个领域。其主要特点包括:快速响应能力(高转矩和加速度),精确位置与速度控制,节省空间的同时便于集成和安装,丰富的产品系列以满足不同需求,先进的散热系统确保稳定运行,以及操作维护极为便捷的服务支持。通过提供多种法兰尺寸和转轴设计选项,用户可根据具体需求选择合适的设备配置。此外,1FK7电机配备先进冷却系统和专业的技术支持服务:电话前缀区分服务中心或技术支持部门,并附有分机号或部门代码;用户可拨打北京服务中心电话010开头的号码进行技术咨询,青岛服务中心则为0532开头。紧急技术支持可通过上述联系方式快速获取帮助。西门子作为全球知名工业自动化解决方案提供商,在产品性能、售后服务等方面均处于行业领先地位,并通过在线支持和远程诊断等服务提升用户设备运行效率。产品说明书是操作与维护的重要参考依据,用户应仔细阅读以确保安全可靠的操作流程。同时,掌握正确的联系方式和操作规范对提高生产效率至关重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值