UVA 439 Knight Moves

本文介绍了一个经典的骑士行走问题,即利用广度优先搜索(BFS)算法求解骑士从一个起点到达终点所需的最少移动次数。通过具体代码实现展示了如何在8×8的棋盘上找到最优路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UVA-439

题意:给出两个坐标,求骑士从从第一个条到第一个坐标跳(中国象棋中马的走法)到第二个坐标要走多少步。
解题思路:一般遇到求最少步骤的就会先想到bfs。简单的广搜题目。

/*************************************************************************
    > File Name: UVA-439.cpp
    > Author: Narsh
    > 
    > Created Time: 2016年07月20日 星期三 15时17分30秒
 ************************************************************************/

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
struct node{
    int x,y,l;
}q[600000];
const int MoVe[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-1};
int n,m,h,t,x,y;
bool map[30][30];
string s;
int main() {
//cout<<"s  b"<<endl;
    while (cin>>s) {
        h=t=0;
        memset(map,true,sizeof(map));
        q[++t].x=s[0]-'a'+1;
        q[t].y=s[1]-'0';
        q[t].l=0;
        map[q[t].x][q[t].y]=false;
        printf("To get from %c%c to ",s[0],s[1]);
        cin>>s;
        printf("%c%c takes ",s[0],s[1]);
        n=s[0]-'a'+1;
        m=s[1]-'0';
        while (h < t) {
            h++;
            if (q[h].x == n && q[h].y == m) {
                printf("%d knight moves.\n",q[h].l);
                break;
            }
            for (int i = 0; i < 8; i++) {
                x=q[h].x+MoVe[i][0];
                y=q[h].y+MoVe[i][1];
                if (1 <= x && x <= 8 && 1 <= y && y <= 8 && map[x][y]) {
                    map[x][y]=false;
                    t++;
                    q[t].x=x;
                    q[t].y=y;
                    q[t].l=q[h].l+1;
                }
            }
        }
    }
}
"Knight moves"是指国际象棋中的“马”走法,即一步可以跳过一个正方形格,然后向左、右、上或下两个方向再跳一格,形成“L”形移动。在Java中,你可以通过模拟这种规则来实现一个骑士的移动函数。以下是一个简单的示例: ```java public class KnightMove { public static boolean isLegalMove(int x1, int y1, int x2, int y2, int boardSize) { // 骑士的合法移动范围:(x, y) -> (x±2, y±1) 或 (x±1, y±2) if (Math.abs(x1 - x2) == 2 && Math.abs(y1 - y2) == 1 || Math.abs(x1 - x2) == 1 && Math.abs(y1 - y2) == 2) { return true; } else if (x1 >= 0 && x1 < boardSize && y1 >= 0 && y1 < boardSize && x2 >= 0 && x2 < boardSize && y2 >= 0 && y2 < boardSize) { // 确保不在边界外 return true; } return false; } public static void move(int[][] chessBoard, int startRow, int startCol, int endRow, int endCol) { if (isLegalMove(startRow, startCol, endRow, endCol, chessBoard.length)) { System.out.println("Valid knight move from (" + startRow + ", " + startCol + ") to (" + endRow + ", " + endCol + ")"); } else { System.out.println("Invalid knight move."); } } // 示例用法 public static void main(String[] args) { int[][] board = new int[8][8]; move(board, 1, 2, 6, 5); // 骑士从(1, 2)移到(6, 5),这是一个合法的步骤 move(board, 0, 0, 8, 8); // 越界无效移动 } } ``` 这个`KnightMove`类包含了判断骑士是否能从一个位置移动到另一个位置的`isLegalMove`方法,以及显示移动结果的`move`方法。在`main`函数中,你可以测试不同的坐标对。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值