【POJ1915】 Knight Moves骑士移动(宽度优先搜索Java版)

POJ1915题目详情见官网:http://poj.org/problem?id=1915
思维导图攻破算法难题
算法思路视频讲解请点击——> b站poj 1915算法思路讲解
手敲代码讲解视频请点击——> b站poj 1915代码讲解

更多精彩视频(Letecode刷题、互联网大厂笔试刷题)请在bilibili搜索鲁班代师,然后搜索题号就可以,关注不走丢,感谢。
欢迎批评指正、探讨!

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

public class Main
{
    public static int[] dx = {-2, -1, 1, 2, 2, 1, -1, -2};
    public static int[] dy = {1, 2, 2, 1, -1, -2, -2, -1};
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int ans = 0;
        while(--n >= 0)
        {
                int l = sc.nextInt();
                int beginx = sc.nextInt();
                int beginy = sc.nextInt();
                int endx = sc.nextInt();
                int endy = sc.nextInt();
               ans = BFS_move(beginx, beginy, endx, endy, l);
               System.out.println(ans);
        }
    }
    public static int BFS_move(int beginx, int beginy, int endx, int endy, int l)
    {
        int[][] mark = new int[l][l];
        for(int i = 0; i < l; i++)
        {
            for (int j = 0; j < l; j++)
            {
                mark[i][j] = 0;
            }
        }
        Queue<Location> Q = new LinkedList<Location>();
        Location lc = new Location(beginx, beginy, 0);
        Q.add(lc);
        mark[beginx][beginy] = 1;
        while (!Q.isEmpty())
        {
            lc = Q.poll();
            if(lc.x == endx && lc.y == endy)
            {
                return lc.distance;
            }
            for(int i = 0; i < 8; i++)
            {
                int newx = lc.x + dx[i];
                int newy = lc.y + dy[i];
                if( newx >= 0 && newx < l && newy >= 0 && newy < l && mark[newx][newy] == 0)
                {
                    Q.add(new Location(newx, newy, lc.distance + 1));
                    mark[newx][newy] = 1;
                }
            }
        }
        return -1;
    }
}
class Location
{
    int x;
    int y;
    int distance;
    public Location(int x, int y, int distance)
    {
        this.x = x;
        this.y = y;
        this.distance = distance;
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1915是一道经典的搜索题目,也被称为“Knight Moves”。下面我将为您提供解题思路和解题步骤: 1. 题目描述 在8x8的国际象棋棋盘上,棋子“马”从初始位置出发,允许走日字形的移动。给定目标位置,求出从初始位置到目标位置最少需要几步。 2. 解题思路 这道题目可以采用广度优先搜索(BFS)算法解决。将初始位置加入队列中,然后依次处理队列中的每个位置,根据“马”的走法,生成下一步可以到达的位置,如果这个位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数。 3. 解题步骤 具体的解题步骤如下: (1)将初始位置加入队列中,并记录步数为0。 (2)依次处理队列中的每个位置,生成下一步可以到达的位置。 (3)如果下一步可以到达的位置没有被访问过,则将其加入队列中,并记录到达这个位置的步数为当前位置的步数+1。 (4)重复步骤(2)和(3),直到队列为空。 (5)如果目标位置被访问过,则返回到达目标位置的步数,否则返回-1。 4. 注意事项 在实现BFS算法的过程中,需要注意以下几点: (1)需要记录每个位置是否被访问过,以避免重复访问。 (2)需要注意边界条件,防止数组越界。 (3)需要注意判断目标位置是否合法,即是否在棋盘范围内。 5. 总结 本题是一道经典的搜索题目,采用BFS算法可以求解出从初始位置到目标位置最少需要几步。在解题过程中,需要注意边界条件和目标位置是否合法等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值