LoginSignup
1
1

More than 5 years have passed since last update.

壊れたロボット2

Posted at

 ああああああああ!!2週目だというのに無限ループですよ!!なんの条件みすったのか・・・グラフから脱出出来なくなってる・・・

public class Main {
    boolean[][] grid = new boolean[51][51];
    int[] xMove = new int[]{1, -1, 0, 0};//xの進む距離
    int[] yMove = new int[]{0, 0, -1, 1};//yの進む距離
    double[] dir;

    double dfs(int n, int x, int y, double prob) {
        System.out.println(n + "ステップ目");
        double ret = 0;
        if(grid[x][y]) {
            //System.out.println("We had already visited.");
            return(0);//既に訪れたならばその連続確率は0となる
        }
        if(n == 0) {
            return(prob);//成功した事になるので、連続確率はprobで良い
        }
        System.out.println(x + ", " + y);
        for(int r = 0; r < 4; r++) {
            grid[x][y] = true;
            ret += dfs(n - 1, x + xMove[r], y + yMove[r], prob * dir[r]);
            grid[x][y] = false;
        }
        return(ret);
    }


    public double getProbability(int n, int east, int west, int south, int north) {
        dir = new double[]{((double)east)/100.0, ((double)west)/100.0, ((double)south)/100.0, ((double)north)/100.0};
        return(dfs(n, 25, 25, 1));
    }

    void doIt() {
        int n = 14;
        //以下2ケースにおいて無限ループ
        int x = 2, y = 2, z = 0, r = 0;
        //int x =1, y =1, z =1, r = 1;
        int east = 25*x, west = 25*y, south = 25*z, north = 25*r;
        System.out.println(getProbability(n, east, west, south, north));
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new Main().doIt();
    }

}

 
 頑張らないとマズいなぁ・・・。7/11日ICPCあるのに・・・

1
1
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1