📣 黑五价提前开抢|VIP一年通行证5折,蓝莓、Offer多多同步特惠
楼主: anonym
跳转到指定楼层
上一主题 下一主题
收起左侧

[Coursera] Algorithms, Part Ⅰ (Week 1)

 关闭
🔗
samandnike 2013-9-5 01:10:03 | 只看该作者
全局:
asterid 发表于 2013-9-2 03:26
我觉得必须使用两个个WQUF。
第一个WQUF连接顶部和底部的两个virtual cell,用于应答 percolates();
第 ...

用两个WQUF,memory test通不过...
回复

使用道具 举报

🔗
samandnike 2013-9-5 01:34:34 | 只看该作者
全局:
Test 2: Check whether exception is called if N, T are out of bounds
  *  N = -23, T = 42
     - IllegalArgumentException NOT thrown for PercolationStats()
  *  N =  23, T =  0
     - IllegalArgumentException NOT thrown for PercolationStats()
  *  N = -42, T =  0
     - IllegalArgumentException NOT thrown for PercolationStats()
==> FAILED

好无语啊...我肯定是加了的啊..
public PercolationStats(int N, int T) {    // perform T independent computational experiments on an N-by-N grid
          if (N<=0 || T<=0) throw new IndexOutOfBoundsException("Invalid N or T");
回复

使用道具 举报

🔗
samandnike 2013-9-5 01:44:07 | 只看该作者
全局:
本帖最后由 samandnike 于 2013-9-5 01:45 编辑

总算搞定了.. 用了两个WQUF对象,一个只有顶端节点用来check full,一个有顶端和底端节点用来check percolation,可解决back wash问题 但是这个时候需要把open的那个数组设置成boolean型才不会failed memory check....另外throw out exception的时候,因为只用了IndexOutOfBoundsException,导致有个check IllegalArgumentException的test failed掉了...

QQ图片20130904103746.jpg (67.56 KB, 下载次数: 0)

QQ图片20130904103746.jpg

评分

参与人数 1学分 +1 收起 理由
Shuang7 + 1

查看全部评分

回复

使用道具 举报

🔗
yukimi28128 2013-9-5 03:21:25 | 只看该作者
全局:
本帖最后由 yukimi28128 于 2013-9-5 03:24 编辑
asterid 发表于 2013-8-31 12:52
具体哪部分有问题?

D:\Percolation Score.jpg

第一次提交的时候backwash没有解决的时候还得了67.5  解决了backwash的问题后提交居然变成62.5  有没有大牛来给看看程序啊?

评分

参与人数 1学分 +1 收起 理由
Shuang7 + 1

查看全部评分

回复

使用道具 举报

🔗
samandnike 2013-9-5 06:55:05 | 只看该作者
全局:
yukimi28128 发表于 2013-9-5 03:21
第一次提交的时候backwash没有解决的时候还得了67.5  解决了backwash的问题后提交居然变成62.5  有没 ...

感觉是整个程序有错,backwash清理掉了一部分full的情况.
回复

使用道具 举报

🔗
asterid 2013-9-5 08:03:51 | 只看该作者
全局:
yukimi28128 发表于 2013-9-4 14:21
第一次提交的时候backwash没有解决的时候还得了67.5  解决了backwash的问题后提交居然变成62.5  有没 ...

可以把fail的那些测试信息贴出来。这个autograder的错误信息蛮详细的。
回复

使用道具 举报

🔗
samandnike 2013-9-5 08:30:13 | 只看该作者
全局:
怎么样才能+学分....
回复

使用道具 举报

🔗
yukimi28128 2013-9-6 07:57:15 | 只看该作者
全局:
asterid 发表于 2013-9-5 08:03
可以把fail的那些测试信息贴出来。这个autograder的错误信息蛮详细的。

不好意思了,各位,13个测试里边只对了两个,测试信息有点长:

Tests 2 through 8 create a Percolation object using your code, then repeatedly
open sites using open(i, j). After each call to open, we check that isFull(),
isOpen(), and percolates() return the corrrect results.

Test 2: Open predetermined list of sites using files
  *  filename = input6.txt
     isFull(2, 1) returns wrong value [after 12 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8-no.txt
  *  filename = input10-no.txt
  *  filename = greeting57.txt
  *  filename = heart25.txt
==> FAILED

Test 3: Open random sites until system percolates (then test is terminated)
  *  N = 3
     isFull(2, 1) returns wrong value [after 3 total calls to open()]
     - student   = true
     - reference = false
  *  N = 5
  *  N = 10
  *  N = 10
  *  N = 20
  *  N = 20
  *  N = 50
     isFull(2, 1) returns wrong value [after 123 total calls to open()]
     - student   = true
     - reference = false
  *  N = 50
==> FAILED

Test 4: Opens predetermined sites, but where N = 1 and N = 2 (corner case test)
  *  filename = input1.txt
     percolates() returns wrong value [after 0 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input1-no.txt
     percolates() returns wrong value [after 0 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input2.txt
  *  filename = input2-no.txt
==> FAILED

Test 5: Check for backwash with predetermined sites
  *  filename = input20.txt
  *  filename = input10.txt
  *  filename = input50.txt
     isFull(2, 1) returns wrong value [after 172 total calls to open()]
     - student   = true
     - reference = false
==> FAILED

Test 6: Check for backwash with predetermined sites that havemultiple percolating paths
  *  filename = input3.txt
     isFull(2, 1) returns wrong value [after 5 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input4.txt
     isFull(2, 1) returns wrong value [after 3 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input7.txt
==> FAILED

Test 7: Predetermined sites with very long percolating path
  *  filename = snake13.txt
     isFull(2, 1) returns wrong value [after 60 total calls to open()]
     - student   = true
     - reference = false
  *  filename = snake101.txt
     isFull(2, 1) returns wrong value [after 4694 total calls to open()]
     - student   = true
     - reference = false
==> FAILED

Test 8: Opens every site
  *  filename = input5.txt
==> passed

Test 9: Create multiple Percolation objects at the same time
        (to make sure you didn't store data in static variables)
     isFull(2, 1) returns wrong value [after 7 total calls to open()]
     - student   = true
     - reference = false
==> FAILED

Test 10: Open predetermined list of sites using file
         but change the order in which methods are called
  *  filename = input8.txt;  order =     isFull(),     isOpen(), percolates()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt;  order =     isFull(), percolates(),     isOpen()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt;  order =     isOpen(),     isFull(), percolates()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt;  order =     isOpen(), percolates(),     isFull()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt;  order = percolates(),     isOpen(),     isFull()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  filename = input8.txt;  order = percolates(),     isFull(),     isOpen()
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
==> FAILED

Test 11: Call all methods in random order until just before system percolates
  *  N = 3
     isFull(2, 1) returns wrong value [after 3 total calls to open()]
     - student   = true
     - reference = false
  *  N = 5
  *  N = 7
     isFull(2, 1) returns wrong value [after 4 total calls to open()]
     - student   = true
     - reference = false
  *  N = 10
  *  N = 20
     isFull(2, 1) returns wrong value [after 63 total calls to open()]
     - student   = true
     - reference = false
  *  N = 50
     isFull(2, 1) returns wrong value [after 59 total calls to open()]
     - student   = true
     - reference = false
==> FAILED

Test 12: Call all methods in random order with inputs not prone to backwash
  *  N = 3
  *  N = 5
     isFull(2, 1) returns wrong value [after 13 total calls to open()]
     - student   = true
     - reference = false
  *  N = 7
     isFull(2, 1) returns wrong value [after 19 total calls to open()]
     - student   = true
     - reference = false
  *  N = 10
  *  N = 20
  *  N = 50
==> FAILED

Test 13: Call all methods in random order until all sites are open
  *  N = 3
     isFull(2, 1) returns wrong value [after 5 total calls to open()]
     - student   = true
     - reference = false
  *  N = 5
  *  N = 7
  *  N = 10
     isFull(2, 1) returns wrong value [after 44 total calls to open()]
     - student   = true
     - reference = false
  *  N = 20
     isFull(2, 1) returns wrong value [after 146 total calls to open()]
     - student   = true
     - reference = false
  *  N = 50
     isFull(2, 1) returns wrong value [after 423 total calls to open()]
     - student   = true
     - reference = false
==> FAILED


Total: 2/13 tests passed!
===============================================================


不知道能不能贴code出来啊,不管了,希望有大牛帮我看看到底是啥问题,这两天搞的我茶饭不思的。万分感谢啊。

private boolean[][] site;                   // site status indexed
    private int grid;                     // grid size: grid*grid
    private WeightedQuickUnionUF quickunion1;    //weighted quick union tree
    private WeightedQuickUnionUF quickunion2;    // mirror to quickunion1
            
    public Percolation(int N)              // create N-by-N grid, with all sites blocked
    {
        grid = N;
        site = new boolean[N][N];
        quickunion1 = new WeightedQuickUnionUF(N*N+2);
        quickunion2 = new WeightedQuickUnionUF(N*N+1);        
        for (int i =0; i <N; i++)
        {
            for (int j =0; j<N; j++)
            {
                site[i][j] = false;
            }
        }
        for (int i=1; i<=N; i++) quickunion1.union(0,i);        // 0 is the virtual top site
        for (int i=0; i<N; i++) quickunion1.union(N*N+1,N*N-i); // N*N+1 is the virtual top site
        for (int i=1; i<=N; i++) quickunion2.union(0,i);   //connect mirror to virtual top site
    }
        
    public void open(int i, int j)         // open site (row i, column j) if it is not already
    {
         if (i < 1 || i > grid || j < 1 || j > grid)
         {
            throw new IndexOutOfBoundsException();
         }
        site[i-1][j-1] = true;
        if(i>1 && isOpen(i-1,j))
        {
            quickunion1.union((grid*(i-1)+(j-1)),(grid*(i-2)+(j-1)));
            quickunion2.union((grid*(i-1)+(j-1)),(grid*(i-2)+(j-1)));
        }
        if(i<grid && isOpen(i+1,j))
        {
            quickunion1.union((grid*(i-1)+(j-1)),(grid*i+(j-1)));
            quickunion2.union((grid*(i-1)+(j-1)),(grid*i+(j-1)));
        }
        if(j>1 && isOpen(i,j-1))
        {
            quickunion1.union((grid*(i-1)+(j-1)),(grid*(i-1)+(j-2)));
            quickunion2.union((grid*(i-1)+(j-1)),(grid*(i-1)+(j-2)));
        }
        if(j<grid && isOpen(i,j+1))
        {
            quickunion1.union((grid*(i-1)+(j-1)),(grid*(i-1)+j));
            quickunion2.union((grid*(i-1)+(j-1)),(grid*(i-1)+j));
        }
    }
   
    public boolean isOpen(int i, int j)    // is site (row i, column j) open?
    {
        if (i < 1 || i> grid || j < 1 || j > grid)
        {
            throw new IndexOutOfBoundsException();
        }
        return site[i-1][j-1] == true;
    }
   
    public boolean isFull(int i, int j)    // is site (row i, column j) full?
    {
        if (i < 1 || i> grid || j < 1 || j > grid)
        {
            throw new IndexOutOfBoundsException();
        }
        return (site[i-1][j-1]== true) && (quickunion2.connected(0,(i-1)*grid+j-1));
    }
   
    public boolean percolates()            // does the system percolate?
    {
       return quickunion1.connected(0,grid*grid+1);
    }   


回复

使用道具 举报

🔗
asterid 2013-9-6 11:23:26 | 只看该作者
全局:
本帖最后由 asterid 于 2013-9-5 22:26 编辑
yukimi28128 发表于 2013-9-5 18:57
不好意思了,各位,13个测试里边只对了两个,测试信息有点长:

Tests 2 through 8 create a Percolati ...

我用 InteractivePercolationVisualizer 测试了下,是miss-by-one问题。你可以试试看只打开第二排第一个cell,这个cell立刻就注满水了。

(i, j) 应该转换为 (i-1) * grid + j,而不是 (i-1) * grid + j - 1。原因在于cell 0是顶部virtual cell,所以第一排第一个是cell 1,就等于 j ,不需要减去 1 了。具体要调整的地方在percolated()和open()。







回复

使用道具 举报

🔗
 楼主| anonym 2013-9-7 09:03:27 | 只看该作者
全局:
没有写比较复杂程序的经验真是要被虐死了 从中午开始写还没搞完 而且肯定有很多错误
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表