Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <random>
  5. #include <ctime>
  6. using namespace std;
  7.  
  8. void construire_passage (int i,int j,vector<vector<bool>>& libre,vector<vector<bool>>& passage);
  9.  
  10. int main () {
  11.  
  12. char c;
  13. cin.get(c);
  14. int n(0);
  15. cin>>n;
  16. double p;
  17. cin>>p;
  18. int nbt;
  19. cin>>nbt;
  20. if ((p>1) or (p<0)) { return 0;}
  21. cout<<" "<<c<<" : "<<n<<" : "<<p<<" : "<<nbt<<endl;
  22.  
  23. //building cells
  24. vector<vector<bool>>libre (n,vector<bool>(n));
  25. vector<vector<bool>>passage (n,vector<bool>(n));
  26.  
  27. bernoulli_distribution b(p);
  28. default_random_engine e;
  29.  
  30. //for (int k(0); k<nbt;++k) { //generates random matrix
  31. //display the "libre" matrix
  32. for (int i(0); i<n ;++i) { //horizontal line
  33. for (int j(0); j<n;++j) { //vertical line
  34. libre[i][j]=b(e); //random cells
  35. cout<<libre[i][j];
  36. }
  37. cout<<endl;
  38. }
  39. cout<<endl;
  40. //}
  41.  
  42. //buidling the "passage" matrix
  43. for(int j(0); j<n ;++j)
  44. {
  45. construire_passage(0,j,libre,passage);
  46. }
  47.  
  48. //display the passage matrix
  49. for (int i(0); i<n ;++i)
  50. {
  51. for (int j(0); j<n ;++j)
  52. {cout<<passage[i][j];}
  53. cout<<endl;
  54.  
  55. }
  56.  
  57. int compteur(0);
  58.  
  59. //Analyzing if there is "traversée"
  60. for (int l(0);l<nbt;++l) { //veryfing if there is "traversée for the nbt vectors
  61. for (int j(0);j<n;++j){
  62. if (passage[n-1][j]==0) {
  63. cout<<"il y a bel et bien traversée"<<endl;
  64. compteur+=1;
  65. return compteur;}
  66. else {}
  67. }
  68. }
  69.  
  70. //final step : display the probability to have "traversée"
  71. double pf(0);
  72. pf = compteur/nbt;
  73. cout<<"la probabilité d'avoir un passage est donc de : "<<pf<<endl;
  74.  
  75.  
  76.  
  77. }
  78.  
  79. void construire_passage (int i,int j,vector<vector<bool>>& libre,vector<vector<bool>>& passage)
  80. {
  81. //step1 :: confirm that i,j are in the area
  82. int n;
  83. n=libre.size(); // warning :: libre is of dimension nXn
  84. if ((i<0) or (i>n-1) or (j<0) or (j>n-1)) {return;} // if outside stop execution and get back
  85. //test if cell is "libre" => value=0 => not true...
  86. if (libre[i][j]==1)
  87. {return;} //cell closed -> stop
  88. else
  89. {
  90. // test if it has been visited ??
  91. if (passage[i][j]==0)
  92. {return;} //cell already visited -> stop
  93. else
  94. {
  95. passage[i][j]=0; // cell is a free "passage"
  96. // test all adajcent cells
  97. construire_passage(i+1,j,libre,passage);
  98. construire_passage(i-1,j,libre,passage);
  99. construire_passage(i,j+1,libre,passage);
  100. construire_passage(i,j-1,libre,passage);
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement