Last active
September 28, 2015 17:27
-
-
Save spceaza/3a2f99f1b9a7746a04e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <random> | |
#include <fstream> | |
#define N_ atoi(argv[1]) | |
#define DELTA strtod(argv[2], NULL) | |
enum{OPEN, CLOSED}; | |
using namespace std; | |
bool isConected(bool** grid, int x, int y, bool** grid_copy, int n); | |
double simulate(int size, double probability); | |
int main(int argc, char** argv) | |
{ | |
ofstream myfile, myscript; | |
myfile.open("data.csv"); | |
myscript.open("csp_script.R"); | |
double result; | |
for(double prob = 0.0; prob <= 1; prob += DELTA) | |
{ | |
result = 0.0; | |
for(int i = 0; i < 1000; i++) | |
result += simulate(N_, prob); | |
result /= 1000.0; | |
myfile << prob << "," << result << endl; | |
} | |
myscript << "pdf(\"mygraph.pdf\")" << endl; | |
myscript << "plot(read.csv(\"data.csv\"), pch=16, cex=.9)" << endl; | |
myscript << "dev.off()" << endl; | |
myfile.close(); | |
myscript.close(); | |
system("Rscript csp_script.R"); | |
return 0; | |
} | |
double simulate(int size, double probability) | |
{ | |
std::random_device rd; | |
std::mt19937 e2(rd()); | |
std::uniform_int_distribution<int> dist(0,100); | |
bool** grid, ** grid_copy; | |
grid = new bool*[size]; | |
grid_copy = new bool*[size]; | |
for(int i = 0; i < size; i++) | |
{ | |
grid[i] = new bool[size]; | |
grid_copy[i] = new bool[size]; | |
} | |
for(int i = 0; i < size; i++) | |
for(int j = 0; j < size; j++) | |
{ | |
grid[i][j] = dist(e2) >= probability*100.0 ? OPEN : CLOSED; | |
grid_copy[i][j] = false; | |
} | |
int* x = new int[size]{-1}; | |
int counter = 0; | |
bool conected = false; | |
for(int j = 0; j < size; j++) | |
if(!grid[0][j]) | |
{ | |
x[counter++] = j; | |
} | |
for(int i = 0; i < counter; i++) | |
if(x[i] >= 0) | |
conected += isConected(grid, x[i], 0, grid_copy, size); | |
for(int i = 0; i < size; i++) | |
{ | |
delete[] grid[i]; | |
delete[] grid_copy[i]; | |
} | |
delete[] x; | |
delete[] grid; | |
delete[] grid_copy; | |
return conected ? 1.0 : 0.0; | |
} | |
bool isConected(bool** grid, int x, int y, bool** grid_copy, int n) | |
{ | |
bool result = false; | |
grid_copy[y][x] = true; | |
if(y == n-1) | |
return true; | |
if(y < n-1 && grid_copy[y+1][x] == false && grid[y+1][x] == OPEN) | |
result += isConected(grid, x, y+1, grid_copy, n); | |
if(x > 0 && grid_copy[y][x-1] == false && grid[y][x-1] == OPEN) | |
result += isConected(grid, x-1, y, grid_copy, n); | |
if(x < n-1 && grid_copy[y][x+1] == false && grid[y][x+1] == OPEN) | |
result += isConected(grid, x+1, y, grid_copy, n); | |
if(y > 0 && grid_copy[y-1][x] == false && grid[y-1][x] == OPEN) | |
result += isConected(grid, x, y-1, grid_copy, n); | |
return result; | |
} |
g++ -std=c++11 -O3 -o parcial.bin parcial.cpp
Pregunta:
Usted no retorna un arreglo de números?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
./parcial.bin [1] [2]
[1]: Size of the grid
[2]: Delta for the probability