首页 > 其他分享 >骑士的移动(Knight Moves)

骑士的移动(Knight Moves)

时间:2022-11-28 23:35:35浏览次数:68  
标签:moves takes get int Knight kp knight 骑士 Moves



​Knight Moves​


Time Limit:3000MS

 

Memory Limit:Unknown

 

64bit IO Format:%lld & %llu


​Submit  ​​​​Status​

Description


骑士的移动(Knight Moves)_java

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 Specification​

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 Specification​

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.


【分析】

       利用广度优先搜索寻找最短路径。用way[r][c]记录起点到达[r, c]位置所走的步数。用pos[r][c]来标记[r, c]位置是否被访问过。

用java语言编写程序,代码如下:


import java.io.BufferedInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(new BufferedInputStream(System.in));
//位置移动
final int[] dr = { -1, -2, -2, -1, 1, 2, 2, 1};
final int[] dc = { -2, -1, 1, 2, 2, 1, -1, -2};
while(input.hasNext()) {
String from = input.next();
String to = input.next();

if(from.equals(to)) {
System.out.println("To get from " + from + " to " + to +" takes 0 knight moves.");
continue;
}
int[][] pos = new int[9][9];
int[][] way = new int[9][9];
//将输入的位置转化为数组的下标
int fr = 9 - (from.charAt(1) - '0'); int fc = from.charAt(0) - 'a' + 1;
int tr = 9 - (to.charAt(1) - '0'); int tc = to.charAt(0) - 'a' + 1;

boolean arrive = false;
Queue<KnightPos> q = new LinkedList<KnightPos>();
q.add(new KnightPos(fr, fc));
while(!q.isEmpty()) {
KnightPos kp = q.poll();
pos[kp.r][kp.c] = 1;//标记已被访问

if(kp.r == tr && kp.c == tc)
break;

for(int i = 0; i < 8; i++) {
int r = kp.r + dr[i];
int c = kp.c + dc[i];

if(r >= 1 && r <= 8 && c >= 1 && c <= 8 && pos[r][c] == 0) {
pos[r][c] = 1;//标记已被访问
way[r][c] = way[kp.r][kp.c] + 1;//从[kp.r, kp.c]向[r, c]走一步
q.add(new KnightPos(r, c));
if(r == tr && c == tc) { //走到终点,标记arrive,跳出循环
arrive = true;
break;
}

}
}

if(arrive)
break;
}
System.out.println("To get from " + from + " to " + to +
" takes " + way[tr][tc] + " knight moves.");
}
}

static class KnightPos {
int r, c;
public KnightPos(int r, int c) {
this.r = r;
this.c = c;
}
}
}




标签:moves,takes,get,int,Knight,kp,knight,骑士,Moves
From: https://blog.51cto.com/u_15894233/5893792

相关文章

  • Knights of the Round Table
    #Description给定若干骑士和他们之间的仇恨关系,规定召开圆桌会议时,两个有仇恨的骑士不能坐在相邻位置,且召开一次圆桌会议的骑士人数必须为奇数,求有多少骑士永远不可能参加......
  • 题解 LGP2607【[ZJOI2008] 骑士】
    problem基环树森林带权最大独立集。\(n\leq10^6\)。solution0这里先解释一下基环树森林,它就是一个\(n\)个点\(n\)条边的森林,同时是一个荒漠。我们拿出其中一棵连......
  • 骑士游历问题(马踏棋盘)解析(c++)
    骑士游历问题:在国际棋盘上使一个骑士遍历所有的格子一遍且仅一遍,对于任意给定的顶点,输出一条符合上述要求的路径解题思路:这是一道经典的遍历问题(DFS),由于题目要求遍历全部,那......
  • [ZJOI2008]骑士
    P2607基环树板子题(虽然打了好久好久)题目大意是给你一n个人的能力值,在每个人都有一个死敌不能同时被选中的情况下,从中挑出一些人,问能力值最大是多少。首先,为什么会往基......
  • UVA1364 Knights of the Round Table | 点双连通分量
    主要就是一个性质:如果一个点双连通分量中有奇环,那么这个点双连通分量中的每个点都在至少一个奇环中。#include<bits/stdc++.h>usingnamespacestd;constintN=100......
  • 【SCOI2005】骑士精神(IDA_,A_)
    我们先考虑最纯粹的暴力,也就是暴力枚举每次空格调到哪里,并继续递归求解。然后发现\(O(8^{15}\times5\times5)\)的复杂度限制了我们的想象。同学写了一发好像10分然后既......
  • P2607 [ZJOI2008] 骑士
    #include<bits/stdc++.h>//#include<windows.h>usingnamespacestd;#definelllonglongconstintN=1e6+1;intn;inth[N],nt[N*2],to[N*2];intcnt;voidadd(i......
  • [SCOI2005] 骑士精神 题解
    题目描述解法采用IDA*算法。不移动骑士而移动空格。每次限制深度,然后对每个遍历到的点进行一次估价,估价函数的值即为当前状态和终点的差异数。如果估计的加上已经确......
  • ABC 243 D - Moves on Binary Tree(树+字符串)
    https://atcoder.jp/contests/abc243/tasks/abc243_d题目大意:给定一颗完全二叉树,他总共可以有(2^10^100)-1个节点,节点下标为1,2,...,(2^10^100)-1。给我们一个长度为n......
  • 卸载阿里云云盾(安骑士)(Agent)
    前言一直在使用阿里云ECS服务器,但是最近一年,多个服务器都遇到过突然连不上的情况,服务器跑的跑的服务都是已知的。但是在某个时间,实例云盘却出现了每秒一百多M的读写(BPS)......