fork download
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <tuple>
  5. #include <algorithm>
  6. #include <cmath>
  7. //http://m...content-available-to-author-only...h.net/test/read.cgi/tech/1564310397/137
  8. namespace Dir {
  9. enum {
  10. //Now = 0,
  11. Top = 0,
  12. Right = 1,
  13. Bottom = 2,
  14. Left = 3,
  15.  
  16. };
  17. }
  18. typedef std::tuple<std::int64_t, std::int64_t, std::int8_t, bool> Data;//x,y,Dir,IsLeft.
  19. typedef std::tuple < std::int64_t, std::int64_t> Point;
  20. typedef std::vector<Point> Walls;
  21.  
  22. Data MakeBug(const Point& P) {
  23.  
  24. return { std::get<0>(P) ,std::get<1>(P) ,Dir::Right,true };
  25. }
  26. /**/
  27. Walls MakeWalls(Data& D, std::int8_t TD) {
  28. std::int64_t Y[] = { 1,0,-1,0 };
  29. std::int64_t X[] = { 0,1,0,-1 };
  30. Walls W;
  31. if (std::get<3>(D)) {
  32. for (std::int64_t i = std::get<2>(D); ((i+(4*7))%4) != TD; i--) {
  33. W.push_back({ std::get<0>(D) + X[((i + (4*7)) % 4)], std::get<1>(D) + Y[(i + (4*7)) % 4] });
  34. }
  35. }
  36. else {
  37. for (std::int64_t i = std::get<2>(D); ((i+(4*7))%4) != TD; i++) {
  38. W.push_back({ std::get<0>(D) + X[((i+(4*7)) % 4)], std::get<1>(D) + Y[(i +(4*7)) % 4] });
  39. }
  40.  
  41. }
  42.  
  43. if(W.size())std::get<3>(D) = !std::get<3>(D);
  44.  
  45. return W;
  46.  
  47. }
  48. std::int8_t SearchDir(const Point& D) {
  49. std::int64_t Y[] = { 1,0,-1,0 };
  50. std::int64_t X[] = { 0,1,0,-1 };
  51.  
  52. for (std::size_t i = 0; i < 4; i++) {
  53. if (std::get<0>(D) == X[i] && std::get<1>(D) == Y[i]) return i;
  54. }
  55.  
  56. return -1;
  57. }
  58.  
  59. Walls MakeHoge(const Point& Start, const Point& End) {
  60. Data B = MakeBug(Start);
  61. Walls W;
  62.  
  63.  
  64. std::int64_t PX = std::get<0>(End) - std::get<0>(B);
  65. std::int64_t PY= std::get<1>(End) - std::get<1>(B);
  66.  
  67. std::int8_t SX = std::signbit(static_cast<double>(PX))?-1:1;
  68. std::int8_t Dir = SearchDir({ SX,0 });
  69. Walls TW = MakeWalls(B, Dir);
  70.  
  71. W.insert(W.end(),TW.begin(), TW.end());
  72. TW.clear();
  73. std::get<0>(B) += PX;
  74. std::get<2>(B) = Dir;
  75.  
  76. std::int8_t SY = std::signbit(static_cast<double>(PY))?-1:1;
  77. Dir = SearchDir({ 0,SY });
  78. TW = MakeWalls(B, Dir);
  79.  
  80. W.insert(W.end(),TW.begin(), TW.end());
  81. std::get<1>(B) += PY;
  82. std::get<2>(B) = Dir;
  83.  
  84. return W;
  85. }
  86. typedef std::vector<std::vector<char>> Screen;
  87.  
  88. bool Show(const Point& S, const Point& E, const Walls& W) {
  89.  
  90. std::int64_t SCXMin = std::min(std::get<0>(E), std::get<0>(S));
  91. std::int64_t SCXMax = std::max(std::get<0>(E), std::get<0>(S));
  92. std::int64_t SCYMin = std::min(std::get<1>(E), std::get<1>(S));
  93. std::int64_t SCYMax = std::max(std::get<1>(E), std::get<1>(S));
  94.  
  95. std::int64_t Width = (SCXMax - SCXMin);
  96. std::int64_t Height = (SCYMax - SCYMin);
  97. Screen Sc(Height + 4);
  98.  
  99. for (auto& o : Sc) { o.resize(Width + 4, ' '); }
  100. Sc[-SCYMin+2][-SCXMin+2] = 'X';
  101. Sc[(std::get<1>(S) - SCYMin) + 2][(std::get<0>(S) - SCXMin) + 2] = 'S';
  102. Sc[(std::get<1>(E) - SCYMin) + 2][(std::get<0>(E) - SCXMin) + 2] = 'E';
  103. for (auto& o : W) {
  104. std::int64_t Y = (std::get<1>(o) - SCYMin) + 2;
  105. std::int64_t X = (std::get<0>(o) - SCXMin) + 2;
  106. Sc[Y][X] = '#';
  107. }
  108.  
  109. for (std::int64_t i = Sc.size() - 1; i > 0; i--) {
  110. for (auto& o : Sc[i]) {
  111. std::cout << o;
  112. }
  113. std::cout << std::endl;
  114. }
  115.  
  116. std::cout << '[' << std::get<0>(S) << ',' << std::get<1>(S) << ']' << std::endl;
  117.  
  118. for (auto& o : W) {
  119. std::cout << '[' << std::get<0>(o) << ',' << std::get<1>(o) << ']';
  120. }
  121. std::cout << std::endl;
  122.  
  123. std::cout << '[' << std::get<0>(E) << ',' << std::get<1>(E) << ']' << std::endl;
  124.  
  125. return true;
  126.  
  127. }
  128.  
  129.  
  130. int main() {
  131. //MakeHoge({ 1,1 }, { 2, 2 });
  132. Point S;
  133. Point E;
  134. Walls W;
  135. S = { 1,1 };
  136. E = { 2,2 };
  137. W = MakeHoge(S, E);
  138. Show(S, E, W);
  139. std::cout << "///////////" << std::endl;
  140. S = { 1,1 };
  141. E = { -2,2 };
  142. W = MakeHoge(S, E);
  143. Show(S, E, W);
  144. std::cout << "///////////" << std::endl;
  145. S = { 1,1 };
  146. E = { -2,-2 };
  147. W = MakeHoge(S, E);
  148. Show(S, E, W);
  149. std::cout << "///////////" << std::endl;
  150. S = { 1,1 };
  151. E = { 2,-2 };
  152. W = MakeHoge(S, E);
  153. Show(S, E, W);
  154. std::cout << "///////////" << std::endl;
  155. return 0;
  156. }
  157.  
Success #stdin #stdout 0s 4568KB
stdin
Standard input is empty
stdout
     
   E 
  S #
 X   
[1,1]
[3,1]
[2,2]
///////////
       
  E  # 
 #   S#
    X  
[1,1]
[2,1][1,2][-3,1]
[-2,2]
///////////
  #  # 
 # # S#
    X  
       
  E    
       
[1,1]
[2,1][1,2][-3,1][-2,2][-1,1]
[-2,-2]
///////////
   # 
  # #
 X   
     
   E 
     
[1,1]
[3,1][2,2][1,1]
[2,-2]
///////////