Percolation/Site percolation: Difference between revisions

m
m (Altered code to be more C++ idiomatic.)
(3 intermediate revisions by 3 users not shown)
Line 48:
print(‘!) ’(‘ ’ * where)‘’:cell2char[cell[:nn - 1][where]])
 
F walk_maze(m, n, &cell, indx) X(PercolatedException) -> NVoid
cell[n][m] = indx
I n < :nn - 1 & cell[n + 1][m] == :NOT_VISITED
Line 63:
F check_from_top(&cell) -> (Int, Int)?
V (n, walk_index) = (0, 1)
XL(m) 0 .try< :M
L(m)I 0cell[n][m] .<== :MNOT_VISITED
I cell[n][m] == :NOT_VISITEDwalk_index++
walk_maze(m, n, &cell, walk_index++)
X.catchhandle PercolatedException ex
walk_maze(m, n, &cell, walk_index)
R ex.t
X.catch PercolatedException ex
R ex.t
R N
 
Line 399 ⟶ 398:
 
bool percolate() {
for ( int32_t x = 0; x < (int32_t) table[0].size(); x++x ) {
if ( path_exists(x, 0) ) {
return true;
Line 408 ⟶ 407:
 
void display() const {
for ( uint64_t col = 0; col < table.size(); col++col ) {
for ( uint64_t row = 0; row < table[0].size(); row++row ) {
std::cout << " " << table[col][row];
}
Line 430 ⟶ 429:
void create_table(const int32_t row_count, const int32_t col_count, const double probability) {
table.assign(row_count, std::vector<std::string>(col_count, EMPTY));
for ( int32_t col = 0; col < row_count; col++col ) {
for ( int32_t row = 0; row < col_count; row++row ) {
table[col][row] = ( distribution(generator) < probability ) ? FILLED: EMPTY;
}
Line 454 ⟶ 453:
std::cout << "Proportion of " << test_count << " tests that percolate through the grid:" << std::endl;
for ( double probable = 0.0; probable <= 1.0; probable += 0.1 ) {
uint32_tdouble percolation_count = 0.0;
for ( int32_t test = 0; test < test_count; test++test ) {
Grid test_grid(row_count, col_count, probable);
if ( test_grid.percolate() ) {
percolation_count += 1.0;
}
}
const double percolation_proportion = (double) percolation_count / test_count;
std::cout << " p = " << std::setprecision(1) <<std::fixed << probable << " : "
printf("%s%.1f%s%.4f%s", " p = ", probable, ": ", percolation_proportion, "\n");
<< std::setprecision(4) << percolation_proportion << std::endl;
}
}
Line 2,542:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
1,481

edits