-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_2_4_8_PercolationFlow.java
82 lines (82 loc) · 2.65 KB
/
ex_2_4_8_PercolationFlow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
///*
//2.4.8. Modify Percolation.java to animate the flow computation, showing the sites filling one by one. Check your answer to the previous exercise.
//*/
////package JavaBook;
//
//import stanfStd.*;
//
//public class ex_2_4_8_PercolationFlow {
//
// // given an N-by-N matrix of open sites, return an N-by-N matrix
// // of sites reachable from the top
// public static boolean[][] flow(boolean[][] open) {
// int N = open.length;
// boolean[][] full = new boolean[N][N];
// for (int j = 0; j < N; j++) {
// flow(open, full, 0, j);
// }
// return full; // testing comment
// }
//
// // determine set of full sites using depth first search
// public static void flow(boolean[][] open, boolean[][] full, int i, int j) {
// int N = open.length;
//
// // base cases
// if (i < 0 || i >= N) return; // invalid row
// if (j < 0 || j >= N) return; // invalid column
// if (!open[i][j]) return; // not an open site
// if (full[i][j]) return; // already marked as full
//
// // mark i-j as full
// full[i][j] = true;
//
// flow(open, full, i+1, j); // down
// flow(open, full, i, j+1); // right
// flow(open, full, i, j-1); // left
// flow(open, full, i-1, j); // up
// }
//
//
// // does the system percolate?
// public static boolean percolates(boolean[][] open) {
// int N = open.length;
// boolean[][] full = flow(open);
// for (int j = 0; j < N; j++) {
// if (full[N-1][j]) return true;
// }
// return false;
// }
//
// // draw the N-by-N boolean matrix to standard draw
// public static void show(boolean[][] a, boolean which) {
// int N = a.length;
// StdDraw.setXscale(-1, N);
// StdDraw.setYscale(-1, N);
// for (int i = 0; i < N; i++)
// for (int j = 0; j < N; j++)
// if (a[i][j] == which)
// StdDraw.filledSquare(j, N-i-1, .5);
// }
//
// // return a random N-by-N boolean matrix, where each entry is
// // true with probability p
// public static boolean[][] random(int N, double p) {
// boolean[][] a = new boolean[N][N];
// for (int i = 0; i < N; i++)
// for (int j = 0; j < N; j++)
// a[i][j] = StdRandom.bernoulli(p);
// return a;
// }
//
// // test client
// public static void main(String[] args) {
// boolean[][] open = StdArrayIO.readBoolean2D();
// StdArrayIO.print(open);
// StdOut.println();
//
// StdArrayIO.print(flow(open));
// StdOut.println(percolates(open));
// }
//
//}