fork download
  1. #include <iostream>
  2. #include <tuple>
  3. #include <vector>
  4. #include <random>
  5. #include <cstdint>
  6. #include <cmath>
  7. #include <algorithm>
  8.  
  9. typedef std::tuple<std::uintmax_t, std::uintmax_t > Point;
  10. typedef std::vector<Point> PType;
  11. typedef std::vector<std::vector<char>> MType;
  12.  
  13. PType MakeRandomPoint(const std::uintmax_t& W, const std::uintmax_t& H, const std::uint64_t& Lim,const unsigned int& S = 0) {
  14.  
  15. //std::uint64_t Lim = 5;
  16. std::uint64_t L = std::sqrt(W * H);
  17. std::mt19937 mt(S);
  18. std::uniform_int_distribution<std::uintmax_t> XD(0, std::max<std::intmax_t>(0,(static_cast<std::intmax_t>(W)-1)));
  19. std::uniform_int_distribution<std::uintmax_t> YD(0, std::max<std::intmax_t>(0,(static_cast<std::intmax_t>(H)-1)));
  20.  
  21. PType R;
  22.  
  23. for (std::uintmax_t i = 0; i < L; i++) {
  24. std::uintmax_t X = XD(mt);
  25. std::uintmax_t Y = YD(mt);
  26.  
  27. double ML = Lim*Lim+1;
  28. for (auto o : R) {
  29. double H = ((std::get<0>(o) - X) * (std::get<0>(o) - X)) + ((std::get<1>(o) - Y) * (std::get<1>(o) - Y));
  30.  
  31. //double H = std::hypot<std::intmax_t,std::intmax_t>(static_cast<std::intmax_t>(std::get<0>(o)) - X, static_cast<std::intmax_t>(std::get<1>(o)) - Y);
  32. ML = std::min<double>(H, ML);
  33. }
  34. if ( ((std::intmax_t)ML >= Lim*Lim)) { R.push_back({ X,Y }); }
  35. }
  36.  
  37. return R;
  38. }
  39.  
  40. PType SetWall(const std::uintmax_t& W,const std::uintmax_t& H,const PType& P,const std::uintmax_t Lim = 5) {
  41.  
  42. PType R = P;
  43. bool F = true;
  44. for (std::uintmax_t Y = 0; Y < H; Y++) {
  45. for (std::uintmax_t X = 0; X < W; X++) {
  46. double ML = Lim*Lim+1;
  47. for (auto& o : P) {
  48. double H = ((std::get<0>(o) - X) * (std::get<0>(o) - X)) + ((std::get<1>(o) - Y) * (std::get<1>(o) - Y));
  49. //double H = std::hypot(std::get<0>(o) - X, std::get<1>(o) - Y);
  50. ML = std::min<double>(H, ML);
  51. }
  52. if ((ML <= (Lim*Lim)) && (ML >= 1)) {
  53. if (ML >= (Lim*Lim) * 0.6) {
  54. R.push_back({ X,Y });
  55. F = false;
  56. }
  57. }
  58. }
  59. }
  60. return (!F) ? SetWall(W, H, R, Lim) : R;
  61. }
  62.  
  63. MType MakeMap(const PType& P, std::uintmax_t W, std::uintmax_t H) {
  64. MType R(H);
  65. std::uintmax_t i = 0;
  66. for (auto& o : R) {
  67. o.resize(W,' ');
  68. }
  69.  
  70. for (auto& o : P) {
  71. R[std::get<1>(o)][std::get<0>(o)] = '#';
  72. }
  73.  
  74. return R;
  75. }
  76.  
  77. bool Show(const PType& P,std::uintmax_t W,std::uintmax_t H) {
  78. MType M = MakeMap(P, W, H);
  79. std::uintmax_t i = 0;
  80. for (auto oo : M) {
  81. std::cout <<i++<<'\t' << '*';
  82. for (auto o : oo) {
  83. std::cout << o;
  84. }
  85. std::cout << '*';
  86. std::cout << std::endl;
  87. }
  88.  
  89. return true;
  90. }
  91.  
  92.  
  93. int main() {
  94.  
  95. /**/
  96. {
  97. double T = 0;
  98. double T2 = 0;
  99. double T3 = 0;
  100. double T4 = 0;
  101. }
  102. /**/
  103. PType P;
  104. std::uintmax_t W = 15;
  105. std::uintmax_t H = 15;
  106. std::uintmax_t Lim = 5;
  107. P = MakeRandomPoint(W, H, Lim);
  108.  
  109. Show(P, W, H);
  110.  
  111. //
  112. std::cout << std::endl;
  113.  
  114. P=SetWall(W, H, P, Lim);
  115.  
  116. Show(P, W, H);
  117.  
  118. return 0;
  119.  
  120.  
  121.  
  122.  
  123. }
Success #stdin #stdout 0s 4352KB
stdin
Standard input is empty
stdout
0	*             # *
1	*               *
2	*               *
3	*               *
4	*               *
5	* #             *
6	*               *
7	*               *
8	*        #      *
9	*               *
10	*               *
11	*               *
12	*           #   *
13	*               *
14	*               *

0	* #      ##   # *
1	*#####    #     *
2	*    ##   #     *
3	*     #  ###    *
4	*     ##########*
5	* #   #     ### *
6	*            #  *
7	*            #  *
8	*    #   #   ###*
9	*#####         #*
10	* #  #          *
11	*    ##         *
12	*     ###   #   *
13	*       #       *
14	*##     #       *