首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RandomWalk解决问题

RandomWalk解决问题
EN

Stack Overflow用户
提问于 2015-09-18 02:06:55
回答 1查看 372关注 0票数 1

问题

我正在编写一个代码,在代码中我模拟一只狗在城市里行走--试图逃离这个城市。这只狗随机选择在每个十字路口走哪条路,在死胡同的probability.If卡在死胡同,狗会直接回到大城市的中心,重新开始。这只狗会一次又一次地这样做,直到它离开城市,或者在T次试验之后累了。但是当狗在每次尝试中再次从中间(N/2,N/2)开始时,它将忘记它在前一次尝试中访问过的所有交叉口。

IDEA

这样做的目的是模仿教科书中给出的代码,并提出解决方案。我们被输入N,T-其中N是城市的南北和东西街道的数目,T是狗在放弃之前试图离开城市的次数。我们必须用StdDraw把它画出来。我们被告知如何进行随机运动--生成一个介于0到4之间的数字--向上:0,右:1,向下:2左: 3

My Approach

代码语言:javascript
复制
import java.util.Random;
public class RandomWalk {
private static final Random RNG = new Random (Long.getLong ("seed", 
        System.nanoTime())); 
public static void main(String[] args) {
    int N = Integer.parseInt(args[0]);    // lattice size
    int T = Integer.parseInt(args[1]);    // number of trials
    int deadEnds = 0;                     // trials resulting in a dead end

    StdDraw.setCanvasSize();
    StdDraw.setXscale(0,N);
    StdDraw.setYscale(0,N);

    // simulate T self-avoiding walks
    for (int t = 0; t < T; t++) {

        StdDraw.clear();

        StdDraw.setPenRadius(0.002);
        StdDraw.setPenColor(StdDraw.LIGHT_GRAY);

        for(int i=0;i<N;i++){
            StdDraw.line(i, 0, i, N);
            StdDraw.line(0, i, N, i);
        }

        StdDraw.setPenColor(StdDraw.RED);
        StdDraw.setPenRadius(0.01);

        boolean[][] a = new boolean[N][N];   // intersections visited 
        int x = N/2, y = N/2;                // current position



        // repeatedly take a random step, unless you've already escaped
        while (x > 0 && x < N-1 && y > 0 && y < N-1)  {
            int t_x = x;
            int t_y=y;
            // dead-end, so break out of loop
            if (a[x-1][y] && a[x+1][y] && a[x][y-1] && a[x][y+1]) {
                deadEnds++;
                break;
            } 

            // mark (x, y) as visited
            a[x][y] = true; 

            // take a random step to unvisited neighbor
            int r = RNG.nextInt(4);
            if (r ==3) {
                //move left
                if (!a[x-1][y])
                    t_x--;

            }
            else if (r == 1 ) {
                //move right
                if (!a[x+1][y])
                    t_x++;
            }
            else if (r == 2) {
                //move down
                if (!a[x][y-1])
                    t_y--;
            }
            else if (r == 0) {
              //move up
                if (!a[x][y+1])
                    t_y++;
            }

            StdDraw.line(t_x, t_y, x, y);
            x = t_x;
            y = t_y;
        } 
        System.out.println("T: "+t);
    } 
    System.out.println(100*deadEnds/T + "% dead ends");

    }
}

问题

给定N-15,T-10,-Dseed=5463786,我们应该得到一个类似- http://postimg.org/image/s5iekbkpf/的输出

我要-见http://postimg.org/image/nxipit0pp/

我不知道我哪里出了问题。我知道这是非常具体的性质,但我真的很困惑,我做错了什么。我尝试了所有的24个排列,0,1,2,3,但没有一个给出所需的输出。因此,我的结论是,在我的代码中的问题。

EN

回答 1

Stack Overflow用户

发布于 2015-09-18 02:45:14

使用以下方法检查您的StdDraw.java:

http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java.html

你的代码应该没问题,我得到了预期的结果

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32642741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档