洛谷 马的遍历

题目链接:P1443 马的遍历

题目描述

有一个 n × m n \times m n×m 的棋盘,在某个点 ( x , y ) (x, y) (x,y) 上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步。

输入格式

输入只有一行四个整数,分别为 n , m , x , y n, m, x, y n,m,x,y

输出格式

一个 n × m n \times m n×m 的矩阵,代表马到达某个点最少要走几步(不能到达则输出 − 1 -1 1)。

样例 #1

样例输入 #1

3 3 1 1

样例输出 #1

0    3    2    
3    -1   1    
2    1    4

提示

数据规模与约定

对于全部的测试点,保证 1 ≤ x ≤ n ≤ 400 1 \leq x \leq n \leq 400 1xn400 1 ≤ y ≤ m ≤ 400 1 \leq y \leq m \leq 400 1ym400

import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

    public static int[][] map = new int[500][500];
    public static int[][] v = new int[500][500];
    public static int[][] ans = new int[500][500];
    public static int[] dx = new int[]{-2, -1, 1, 2, 2, 1, -1, -2};
    public static int[] dy = new int[]{1, 2, 2, 1, -1, -2, -2, -1};
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static Queue<Node> queue = new LinkedList<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        for (int i = 2; i <= n + 1; i++) {
            for (int j = 2; j <= m + 1; j++) {
                map[i][j] = 1;
            }
        }

        int x = scanner.nextInt();
        int y = scanner.nextInt();

        Node node = new Node();
        node.px = x + 1;
        node.py = y + 1;
        node.step = 0;
        ans[x + 1][y + 1] = 0;
        v[x + 1][y + 1] = 1;
        queue.add(node);

        while (!queue.isEmpty()) {
            Node front = queue.peek();
            for (int k = 0; k < 8; k++) {
                int tx = front.px + dx[k];
                int ty = front.py + dy[k];
                if (map[tx][ty] == 1 && v[tx][ty] == 0) {
                    Node tmp = new Node();
                    tmp.px = tx;
                    tmp.py = ty;
                    tmp.step = front.step + 1;
                    ans[tx][ty] = front.step + 1;
                    queue.add(tmp);
                    v[tx][ty] = 1;
                }
            }
            queue.remove();
        }

        for (int i = 2; i <= n + 1; i++) {
            for (int j = 2; j <= m + 1; j++) {
                if (ans[i][j] == 0) {
                    if (i == x + 1 && j == y + 1) {
                        out.printf(0 + "\t");
                    } else {
                        out.printf(-1 + "\t");
                    }
                } else {
                    out.printf(ans[i][j] + "\t");
                }
            }
            out.println();
        }
        out.close();
    }
}


class Node {
    int px;
    int py;
    int step;
}
  • 30
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值