其他分享
首页 > 其他分享> > 普及- 过河卒

普及- 过河卒

作者:互联网

题目:P1002 [NOIP2002 普及组] 过河卒 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

 

参考题解:题解 P1002 【过河卒】 - Chiaro 的博客 - 洛谷博客 (luogu.org)

 

 

本人参考题解查看的代码

 

import java.util.Scanner;

public class Main {
    static int t = 0;

    /*
     * 1. a Horse of Map
     * 
     * 2. go and no Horse(DFS!!!!!!!)
     * 
     * in matrix not overstep:
     * 
     * 1. add a if
     * 
     * Or
     * 
     * 2. extend the Array(Matrix)
     * 
     * Look pro
     * 
     * (1, 1) -> (x, y) has x + y paths ?
     * 
     * f(x, y) = f(x - 1, y) + f(x, y - 1)
     * 
     * if x , y == 1, 2 the (x + y) == paths Only!
     * 
     * if x == 2, y == 2, x + y == 4 But the paths are 6!
     * 
     * so x == 2, y == 2 -> f(1, 2) + f(2, 1)
     * 
     * To do:
     * 
     * 1. For
     * 
     * 2. Recursion
     * 
     * f(x, y) = f(x - 1, y) + f(x, y - 1)
     * 
     * in: 8 6 0 4
     * 
     * out: 1617
     * 
     * Error:
     * 
     * 1. Horse's foot * 2
     * 
     * 2. the size of Map
     * 
     * 3.get the path
     * 
     * 4.the long number
     * 
     * 2022_05_31-22-20-12-Week2
     * 
     * Finally AC All
     * 
     * 1. Input
     * 
     * 2. Set 2 Arrays (a foot, a Path)
     * 
     * 3. set Horse's footpoint
     * 
     * 4. Goto and add the Paths (DP, DP, DP, it is a DP!)
     */

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt(), x = in.nextInt(), y = in.nextInt();
        int fx[] = { 0, -2, -1, 1, 2, 2, 1, -1, -2 };
        int fy[] = { 0, 1, 2, 2, 1, -1, -2, -2, -1 };
        long a[][] = new long[40][40]; // B
        long b[][] = new long[40][40]; // Path
        a[x + 2][y + 2] = 1; // Horse
        for (int i = 1; i <= 8; i++) { // Foot
            a[x + 2 + fx[i]][y + 2 + fy[i]] = 1;
        }
        b[2][1] = 1;
        for (int i = 2; i <= n + 2; i++) {
            for (int j = 2; j <= m + 2; j++) {
                if (a[i][j] == 1)
                    continue;
                b[i][j] = b[i - 1][j] + b[i][j - 1];
            }
        }
        System.out.println(b[n + 2][m + 2]);

        // Map
//        for (int i = 2; i <= n + 2; i++) {
//            for (int j = 2; j <= m + 2; j++) {
//                System.out.print(a[i][j] + ", ");
//            }
//            System.out.println();
//        }

        in.close();
    }

}

 

个人理解

1. 输入(Scanner类)

 

2. 创建数组-都是矩阵(大小在范围之外)

  一个是保存了马的足迹

  另一个是保存了到(x, y)路径的数量

 

3. 设置马的足迹,有八个,为了防止数组下标溢出,B坐标+2

 

4. 用状态转移方程来统计路径的数量

f(x, y) = f(x - 1, y ) + f(x, y - 1)

 

同时还要注意不要经过马的足迹

 

Debug:

 

最好要输出Map, 及时修改代码,

 

总结:DP思想:大化小, 问题的分解

 

注意数组的范围,不要越界

 

别自作聪明把设置马的足迹用几行for循环就写出来, 还他妈搞什么位运算,结果他妈就换了一个例子就废了,草

 

还是用数组存储足迹吧,再把数组遍历一下

 

最后注意以下结果的大小,应该用long

 

参考的题解里还没完

 

还有更优解, 什么切比雪夫距离, 曼哈顿距离

 

脑容量装不下了,淦

 

还是多做题目吧,总结经验了,会自己做了,就看看进阶的题解

 

也就是先一题一解, 再多题一解, 最后是一题多解吧

 

标签:普及,过河,Scanner,int,题解,Horse,long,DP
来源: https://www.cnblogs.com/SaberZHT/p/16332736.html