前段时间在coursera上注册了两门课,Algorithms, Part I,Algorithms, Part II,这教授(Robert Sedgewick)讲得确实不错,不敢说甩国内老师一条街,起码比国内老师讲得要通俗易懂得多,配合ppt和作业,只要你认真做完,收获会非常大。对于学生,空闲时间比较多,同时上两门也OK;对于上班族,个人还是建议先上完Part I,再去上Part-II,一周同时上两门课,还要做两次作业,确实有点吃不消(本人作死,同时上了两门,下班回来做作业经常做到一两点,哭死)。
publicclassPercolation { publicPercolation(int n)// create n-by-n grid, with all sites blocked publicvoidopen(int row, int col)// open site (row, col) if it is not open already publicbooleanisOpen(int row, int col)// is site (row, col) open? publicbooleanisFull(int row, int col)// is site (row, col) full? publicintnumberOfOpenSites()// number of open sites publicbooleanpercolates()// does the system percolate? publicstaticvoidmain(String[] args)// test client (optional) }
import edu.princeton.cs.algs4.WeightedQuickUnionUF; publicclassPercolation { private WeightedQuickUnionUF uf1; private WeightedQuickUnionUF uf2; privateboolean[][] opened; privateint openedCount; privateint BOTTOM; privateint TOP; privateint N; // create n-by-n grid, with all sites blocked publicPercolation(int n) { validateParam(n <= 0); N = n; BOTTOM = N * N + 2; TOP = N * N + 1; uf1 = newWeightedQuickUnionUF((N+2) * (N+2) + 2); uf2 = newWeightedQuickUnionUF((N+2) * (N+2) + 1); opened = newboolean[N+1][N+1]; for (int i=1; i<=N; i++) for (int j=1; j<=N; j++) opened[i][j] = false; openedCount = 0; } // open site (row, col) if it is not open already publicvoidopen(int row, int col) { validateParam(row < 1 || row > N || col < 1 || col > N); if (!opened[row][col]) { opened[row][col] = true; intindex= xyTo1D(row, col); if (row == N) uf1.union(BOTTOM, index); if (row == 1) { uf1.union(TOP, index); uf2.union(TOP, index); } if (row-1 >= 1 && opened[row-1][col]) { uf1.union( index, xyTo1D(row-1, col) ); uf2.union( index, xyTo1D(row-1, col) ); } if (row+1 <= N && opened[row+1][col]) { uf1.union( index, xyTo1D(row+1, col) ); uf2.union( index, xyTo1D(row+1, col) ); } if (col-1 >= 1 && opened[row][col-1]) { uf1.union( index, xyTo1D(row, col-1) ); uf2.union( index, xyTo1D(row, col-1) ); } if (col+1 <= N && opened[row][col+1]) { uf1.union( index, xyTo1D(row, col+1) ); uf2.union( index, xyTo1D(row, col+1) ); } openedCount++; } } // is site (row, col) open? publicbooleanisOpen(int row, int col) { validateParam(row < 1 || row > N || col < 1 || col > N); return opened[row][col]; } // is site (row, col) full? publicbooleanisFull(int row, int col) { validateParam(row < 1 || row > N || col < 1 || col > N); return uf2.connected(TOP, xyTo1D(row, col) ); } // number of open sites publicintnumberOfOpenSites() { return openedCount; } // does the system percolate? publicbooleanpercolates() { return uf1.connected(TOP, BOTTOM); } privateintxyTo1D(int row, int col) { return (row-1) * N + col; } privatevoidvalidateParam(boolean invalid) { if (invalid) thrownewIllegalArgumentException("Data invalid! Please check your input!"); } // test client (optional) publicstaticvoidmain(String[] args) { } }
当然了,题目也要求增加统计API来计算渗透概率临界值p*
1 2 3 4 5 6 7 8 9
publicclassPercolationStats { // perform trials independent experiments on an n-by-n grid publicPercolationStats(int n, int trials) publicdoublemean()// sample mean of percolation threshold publicdoublestddev()// sample standard deviation of percolation threshold publicdoubleconfidenceLo()// low endpoint of 95% confidence interval publicdoubleconfidenceHi()// high endpoint of 95% confidence interval publicstaticvoidmain(String[] args)// test client (described below) }
Test 1: count calls to StdStats.mean() and StdStats.stddev() * n = 20, trials = 10 - calls StdStats.mean() the wrong number of times - number of student calls to StdStats.mean() = 3 - number of reference calls to StdStats.mean() = 1 ==> FAILED
统计API没有做缓存,后来又改了一版,终于得到了100分。然而比较可惜的是,并没有得到bonus。
1 2 3 4
Estimated student memory = 17.00 n^2 + 105.00 n + 392.00 (R^2 = 1.000) Test 2 (bonus): check that total memory <= 11 n^2 + 128 n + 1024 bytes - failed memory test for n = 64 ==> FAILED