留在矩阵中的概率

时间:2015-09-19 12:09:16

标签: algorithm matrix probability

您将获得一个有序矩阵(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);
    }
}

1 个答案:

答案 0 :(得分: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));

打印匹配的答案。