Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <random>
- #include <ctime>
- using namespace std;
- void construire_passage (int i,int j,vector<vector<bool>>& libre,vector<vector<bool>>& passage);
- int main () {
- char c;
- cin.get(c);
- int n(0);
- cin>>n;
- double p;
- cin>>p;
- int nbt;
- cin>>nbt;
- if ((p>1) or (p<0)) { return 0;}
- cout<<" "<<c<<" : "<<n<<" : "<<p<<" : "<<nbt<<endl;
- //building cells
- vector<vector<bool>>libre (n,vector<bool>(n));
- vector<vector<bool>>passage (n,vector<bool>(n));
- bernoulli_distribution b(p);
- default_random_engine e;
- //for (int k(0); k<nbt;++k) { //generates random matrix
- //display the "libre" matrix
- for (int i(0); i<n ;++i) { //horizontal line
- for (int j(0); j<n;++j) { //vertical line
- libre[i][j]=b(e); //random cells
- cout<<libre[i][j];
- }
- cout<<endl;
- }
- cout<<endl;
- //}
- //buidling the "passage" matrix
- for(int j(0); j<n ;++j)
- {
- construire_passage(0,j,libre,passage);
- }
- //display the passage matrix
- for (int i(0); i<n ;++i)
- {
- for (int j(0); j<n ;++j)
- {cout<<passage[i][j];}
- cout<<endl;
- }
- int compteur(0);
- //Analyzing if there is "traversée"
- for (int l(0);l<nbt;++l) { //veryfing if there is "traversée for the nbt vectors
- for (int j(0);j<n;++j){
- if (passage[n-1][j]==0) {
- cout<<"il y a bel et bien traversée"<<endl;
- compteur+=1;
- return compteur;}
- else {}
- }
- }
- //final step : display the probability to have "traversée"
- double pf(0);
- pf = compteur/nbt;
- cout<<"la probabilité d'avoir un passage est donc de : "<<pf<<endl;
- }
- void construire_passage (int i,int j,vector<vector<bool>>& libre,vector<vector<bool>>& passage)
- {
- //step1 :: confirm that i,j are in the area
- int n;
- n=libre.size(); // warning :: libre is of dimension nXn
- if ((i<0) or (i>n-1) or (j<0) or (j>n-1)) {return;} // if outside stop execution and get back
- //test if cell is "libre" => value=0 => not true...
- if (libre[i][j]==1)
- {return;} //cell closed -> stop
- else
- {
- // test if it has been visited ??
- if (passage[i][j]==0)
- {return;} //cell already visited -> stop
- else
- {
- passage[i][j]=0; // cell is a free "passage"
- // test all adajcent cells
- construire_passage(i+1,j,libre,passage);
- construire_passage(i-1,j,libre,passage);
- construire_passage(i,j+1,libre,passage);
- construire_passage(i,j-1,libre,passage);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement