JAVA算法问题,在矩阵中求两点直接的所有路径

在一个矩阵中有两个点A(x1,y1),B(x2,y2),计算从A到B点所有可能的走法,假设x2>=x1,y2>=y1,x可以每次可以走一步或两步,y只能走一步。例如从(8,14)到(10,15)有5种走法。求大神能写段程序计算!!!100分送上

纯手写

import com.sun.deploy.util.ArrayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hcq on 2017/4/28.
 */
public class HappyStemp {
    public static void main(String[] args) {
        int[] a = {8, 14};
        int[] b = {10, 15};
        List<String> records = new ArrayList<String>();
        walk(a, b, records);
        System.out.println("方法数:" + count);

    }

    public static int count = 0;

    public static void walk(int[] a, int[] b, List<String> records) {
        if (a[0] == b[0] && a[1] == b[1]) {
            count++;
            System.out.print("第" + count + "种方法:  ");
            for (String record : records) {
                System.out.print(" "+record);
            }
            System.out.println();
        } else {
            xWalk(a, b, records);
            yWalk(a, b, records);

        }
    }
    //X轴走
    public static void xWalk(int[] a, int[] b, List<String> records) {

        int xHave = b[0] - a[0];
        if (xHave == 0) {
            return;
        } else {
            int[] temp = new int[2];
            temp[1] = a[1];
            temp[0] = a[0] + 1;
            walk(temp, b, makeRecords("X轴走了一步", records));
            if (xHave > 1) {
                temp[0] = a[0] + 2;
                walk(temp, b, makeRecords("X轴走了二步", records));
            }
        }
    }
    //y轴走
    public static void yWalk(int[] a, int[] b, List<String> records) {
        if (b[1] == a[1]) {
            return;
        } else {
            int[] temp = new int[2];
            temp[0] = a[0];
            temp[1] = a[1] + 1;
            walk(temp, b, makeRecords("y轴走了一步", records));
        }
    }

    //制作行走记录
    public static List<String> makeRecords(String str, List<String> old) {
        List<String> listNew = new ArrayList<String>();
        listNew.addAll(old);
        listNew.add(str);
        return listNew;
    }
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-04-28
你的5种走法是怎么走的,不明白呀。画了看看追问

上传图片了,就是上图示例中的那种