【问题标题】:Probability of stay in matrix留在矩阵中的概率
【发布时间】:2015-09-19 12:09:15
【问题描述】:

给定一个阶矩阵 (M x N)。您可以向 4 个方向移动:左、上、右和下。您将获得初始位置 (x, y) 和您可以从给定位置移动的步数。如果您离开矩阵,则在移动时,您将被取消比赛资格。你不被取消资格的概率是多少?

我通过以下两种方式解决了这个问题:

方式 1. 找出您将在矩阵内的总方式 T1,并找出您将在矩阵外的总方式 T2。然后返回 T1 / (T1 + T2) 作为结果。

方式 2。利用到达您邻居的概率为:1/4 这一事实,因为您只能从给定位置向 4 个方向移动并计算结果。

但这两种方法在许多情况下会给出不同的结果。

请在下面找到我的代码,并让我知道我在哪里弄错了,或者方法是否有问题。

public class ProbabilityOfStay {

    private int[] x = {0, 1, 0, -1};
    private int[] y = {-1, 0, 1, 0};

    private int ROW;
    private int COL;
    private int xPos;
    private int yPos;
    private int steps ;
    int[][][] stayDP = null;
    int[][][] nonStayDP = null;
    float[][][] sp = null;

    public ProbabilityOfStay(int R, int C, int x, int y, int steps)
    {
        this.ROW = R;
        this.COL = C;

        this.xPos = x;
        this.yPos = y;
        this.steps = steps;

        stayDP = new int[ROW][COL][steps];
        nonStayDP = new int[ROW][COL][steps];
        sp = new float[ROW][COL][steps];

        this.initializeInt(stayDP, -1);
        this.initializeInt(nonStayDP, -1);
        this.initializeF(sp, -1);   
    }

    private void initializeInt(int[][][] M, int d)
    {
        for(int i = 0; i < ROW; i++)
        {
            for(int j = 0; j < COL; j++)
            {
                for(int k = 0; k < steps; k++)
                    M[i][j][k] = d;
            }
        }
    }

    private void initializeF(float[][][] M, int d)
    {
        for(int i = 0; i < ROW; i++)
        {
            for(int j = 0; j < COL; j++)
            {
                for(int k = 0; k < steps; k++)
                    M[i][j][k] = d;
            }
        }
    }

    private int getTotalStayPath()
    {
        int p = getStayPaths(xPos, yPos, steps);

        return p;
    }

    private int getStayPaths(int xp, int yp, int s)
    {
        if(xp < 0 || xp >= ROW || yp < 0 || yp >= COL)
            return 0;

        if(s == 0)
            return 1;

        if(stayDP[xp][yp][s-1] != -1)
            return stayDP[xp][yp][s-1];

        int ans = 0;

        for(int i = 0; i < x.length; i++)
        {
            ans += getStayPaths(xp + x[i], yp + y[i], s-1);
        }

        return (stayDP[xp][yp][s-1] = ans);
    }

    private int getTotalNonStayPath()
    {
        int p = getNonStayPaths(xPos, yPos, steps);

        return p;
    }

    private int getNonStayPaths(int xp, int yp, int s)
    {
        if(xp < 0 || xp >= ROW || yp < 0 || yp >= COL)
            return 1;

        if(s == 0)
            return 0;

        if(nonStayDP[xp][yp][s-1] != -1)
            return nonStayDP[xp][yp][s-1];

        int ans = 0;

        for(int i = 0; i < x.length; i++)
        {
            ans += getNonStayPaths(xp + x[i], yp + y[i], s - 1);
        }

        return (nonStayDP[xp][yp][s-1] = ans);
    }

    private float getStayProbabilityM()
    {
        float p = getProbability(xPos, yPos, steps);

        return p;
    }

    private float getProbability(int xp, int yp, int s)
    {
        if(xp < 0 || xp >= ROW || yp < 0 || yp >= COL)
            return 0;

        if(s == 0)
            return 1;

        if(sp[xp][yp][s-1] != -1)
            return sp[xp][yp][s-1];

        float ans = 0.0f;

        for(int i = 0; i < x.length; i++)
        {
            ans += (getProbability(xp + x[i], yp + y[i], s-1)) / 4.0;
        }

        return (sp[xp][yp][s-1] = ans);
    }

    public static void main(String[] args)
    {
        int ROW = 7, COL = 7, x = 3, y = 5, steps = 3; //(x, y) is your position in the matrix.
        ProbabilityOfStay pos = new ProbabilityOfStay(ROW, COL, x, y, steps);

        int totalStayPaths = pos.getTotalStayPath(); //number of ways in which you can stay in the matrix.
        int totalNonStayPaths = pos.getTotalNonStayPath(); ////number of ways in which you can't stay in the matrix.

        float stayingProbability = (totalStayPaths / (float)(totalStayPaths + totalNonStayPaths));

        float sP_memorization = pos.getStayProbabilityM();

        System.out.println("Total stay paths: " + totalStayPaths + ", total non-stay paths: " + totalNonStayPaths + ", Stay probability: " + stayingProbability);

        System.out.println("Total probability memoriation: " + sP_memorization);
    }
}

【问题讨论】:

    标签: algorithm matrix probability


    【解决方案1】:

    如果我运行它打印的程序:

    Total stay paths: 56, total non-stay paths: 5
    

    但是,这会导致路径总数为 56+5=61。

    3 个步骤中的每个步骤有 4 个选项,所以总数应该是 4*4*4 = 64。

    我认为问题在于,一旦路径离开棋盘,您就会停止计数。这意味着路径的概率不相等,因此您通过除以路径数的计算是无效的。

    如果您将计算更改为:

    float stayingProbability = (totalStayPaths / (float)Math.pow(4,steps));
    

    它打印匹配的答案。

    【讨论】:

    • 是的,你是对的。我用来计算概率的表达式是错误的。你的一个是正确的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2021-06-10
    • 2018-04-26
    • 2022-10-07
    相关资源
    最近更新 更多