美文网首页
【洛谷】P1443 - 马的遍历

【洛谷】P1443 - 马的遍历

作者: 莫wen | 来源:发表于2020-11-16 00:06 被阅读0次
public class Main {
    static int N ;
    static int M ;
    static int MX ;
    static int MY;
    static int[][] area ;
    static int[][] map ;
    static int count ;

    static int[] px = {-2,-2,-1,1,2,2,1,-1};
    static int[] py = {-1,1,2,2,1,-1,-2,-2};
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        N = sc.nextInt();
        M = sc.nextInt();
        MX = sc.nextInt();
        MY = sc.nextInt();
        
        area = new int[N+1][M+1];
        map = new int[N+1][M+1];
        count = 0;
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                area[i][j] = Integer.MAX_VALUE;
                map[i][j] = Integer.MAX_VALUE;
            }
        }
        
        area[MX][MY] = 1 ;
        map[MX][MY] = 0;
        bfs(MX,MY);
        
        
        //呈矩阵输出
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                if (map[i][j] == Integer.MAX_VALUE) {
                    map[i][j] = -1;
                }
                System.out.printf("%-5d",map[i][j]);
            }
            System.out.println();
        }
        
        
        
    }
    
    private static void bfs(int x, int y) {
        LinkedList<Point> Q = new LinkedList<Point>();
        Point pt = new Point(x,y,0); //
        Q.add(pt);
        
        while(!Q.isEmpty()) {
            Point cur = Q.poll();
            int curx = cur.hz;
            int cury = cur.zz;
            int curn = cur.num;
            
            for (int i = 0; i < 8; i++) {
                int newx = px[i] + curx ;
                int newy = py[i] + cury ;
                count = curn + 1;
                
                if (newx >= 1 && newx <= N && newy >= 1 && newy <= M && area[newx][newy] != 1) {
                    area[newx][newy] = 1;
                    map[newx][newy] = count;
                    Q.add(new Point(newx,newy,count));
                }
            }
        }
        
    }
    
    private static class Point{
        int hz;
        int zz;
        int num;

        public Point(int hz, int zz, int num) {
            this.hz = hz;
            this.zz = zz;
            this.num = num;
        }
    }

}

相关文章

  • 【洛谷】P1443 - 马的遍历

  • 洛谷计划

    洛谷是IT生认可度较高的一个网站,有各种题目以及专业术语,是刷题的一个好地方,但是对基础要求还算挺高,因此需要在...

  • Cookie Session Token JWT & CSR

    前言: 夏洛:大爷,楼上322住的是马冬梅家吧?大爷:马都什么?夏洛:马冬梅。大爷:什么都没啊?夏洛:马冬梅啊。大...

  • 六州歌头·古都小令

    千年洛邑,九朝帝王宫。嵩岳屏,龙门控,郁山葱,首阳穷。伊洛开阙塞,平泉景,金谷情,邙山冢,铜驼岭,马寺声。雪...

  • 几个高精度模板

    模板来自洛谷及Acwing:Acwing洛谷 后续增加注释以及相关代码改进 高精度加法 高精度减法 高精度乘法 高...

  • js功能(函数)课堂笔记

    饥人谷_李栋 1.object2.array3.function 一、object 遍历: 注意,遍历一个对象的时...

  • P1000 超级玛丽游戏

    【题目背景】 本题是洛谷的试机题目,可以帮助了解洛谷的使用。 建议完成本题目后继续尝试P1001、P1008。 【...

  • 洛谷新手题

    今天只是做了一个简单的顺序与分支题,知识点也很常见,只截图题目和代码了~

  • 信息课总结(一)

    贪心与排序 一、合并果子(洛谷ojP1090) 原题是洛谷的P1090 合并果子思路:要使总共的和最小,则要使单次...

  • 洛谷P1219八皇后-dfs

    题目传送:洛谷P1219八皇后 dfs

网友评论

      本文标题:【洛谷】P1443 - 马的遍历

      本文链接:https://www.haomeiwen.com/subject/mftabktx.html