fork download
  1. /* Authors: Vu Hoang Bach from Phuoc Hoa Secondary School */
  2. /* Enjoy TheFatRat's music while reading my code. Link: https://w...content-available-to-author-only...e.com/watch?v=GCx9FU59FJc&t=669s */
  3.  
  4. //#pragma GCC optimize("O3", "unroll-loops")
  5. //#pragma GCC target("avx2", "bmi", "bmi2", "lzcnt", "popcnt")
  6.  
  7. #include <bits/stdc++.h>
  8. #define ldb long double
  9. //#define double ldb
  10. #define db double
  11. #define name "RECTANGL"
  12. #define unomap unordered_map
  13. #define unoset unordered_set
  14. #define endl '\n'
  15. #define str string
  16. #define strstr stringstream
  17. #define sz(a) a.size()
  18. #define ll long long
  19. //#define int ll
  20. #define pii pair <int, int>
  21. #define pll pair <ll, ll>
  22. #define Unique(a) a.resize(unique(all(a)) - a.begin())
  23. #define ull unsigned ll
  24. #define fir first
  25. #define sec second
  26. #define idc cin.ignore()
  27. #define lb lower_bound
  28. #define ub upper_bound
  29. #define all(s) s.begin(), s.end()
  30. #define rev reverse
  31. #define sigma ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  32. #define skibidi int main
  33. #define rizz signed main
  34. #define gcd __gcd
  35. #define found(a, mp) mp.find(a) != mp.end()
  36. #define game_over exit(0);
  37. #define stfu return 0;
  38. #define stop break;
  39. #define odd(n) n & 1
  40. #define pushb push_back
  41. #define popb pop_back
  42. #define pushf push_front
  43. #define popf pop_front
  44. #define even(n) !odd(n)
  45. #define mul2x(a, x) a << x
  46. #define div2x(a, x) a >> x
  47. #define no_check continue;
  48. #define lcm(a, b) a / __gcd(a, b) * b
  49. #define log_base(x, base) log(x) / log(base)
  50. #define debug clog << "No errors!"; game_over;
  51. #define forw(i, a, b) for (int i = a; i <= b; ++i)
  52. #define forw2(i, a, b) for (ll i = a; i <= b; ++i)
  53. #define fors(i, a, b) for (int i = a; i >= b; --i)
  54. #define fors2(i, a, b) for (ll i = a; i >= b; --i)
  55. #define meme memset
  56. #define pqueue priority_queue
  57. #define want_digit(x) cout << fixed << setprecision(x);
  58. #define excuting_time 1000.0 * clock() / CLOCKS_PER_SEC
  59. using namespace std;
  60. const int MOD = 1e9 + 7; // 998244353
  61. const int inf = 1e9;
  62. const ll INF = 1e18;
  63. const int N = 1e4;
  64.  
  65. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  66. ll random(const ll &L, const ll &R)
  67. {
  68. return uniform_int_distribution<ll> (L, R) (rng);
  69. }
  70.  
  71. int n, a[N + 5][N + 5], x, y;
  72. bool visited[N + 5][N + 5];
  73.  
  74. bool inside(int ind)
  75. {
  76. return (1 <= ind && ind <= n);
  77. }
  78.  
  79. bool check(int i, int j)
  80. {
  81. if (!inside(i)) return false;
  82. if (!inside(j)) return false;
  83. return !visited[i][j];
  84. }
  85.  
  86. void build()
  87. {
  88. int curr = 2;
  89. int direction = 0;
  90. int row = 1, col = 1;
  91. visited[1][1] = true;
  92. a[1][1] = 1;
  93. while (curr <= n * n)
  94. {
  95. if (!direction)
  96. {
  97. while (check(row, col + 1))
  98. {
  99. ++col;
  100. a[row][col] = curr;
  101. visited[row][col] = true;;
  102. ++curr;
  103. }
  104. }
  105. else if (direction == 1)
  106. {
  107. while (check(row + 1, col))
  108. {
  109. ++row;
  110. a[row][col] = curr;
  111. visited[row][col] = true;
  112. ++curr;
  113. }
  114. }
  115. else if (direction == 2)
  116. {
  117. while (check(row, col - 1))
  118. {
  119. --col;
  120. a[row][col] = curr;
  121. visited[row][col] = true;
  122. ++curr;
  123. }
  124. }
  125. else
  126. {
  127. while (check(row - 1, col))
  128. {
  129. --row;
  130. a[row][col] = curr;
  131. visited[row][col] = true;
  132. ++curr;
  133. }
  134. }
  135. (direction += 1) %= 4;
  136. }
  137. }
  138.  
  139. void cook()
  140. {
  141. cin >> n >> x >> y;
  142. build();
  143. cout << a[x][y] << endl;
  144. }
  145.  
  146. skibidi()
  147. //rizz()
  148. {
  149. srand(time(NULL));
  150. sigma;
  151. if (fopen(name".INP", "r"))
  152. {
  153. freopen(name".INP", "r", stdin);
  154. freopen(name".OUT", "w", stdout);
  155. }
  156. int numTest = 1;
  157. // cin >> numTest;
  158. while (numTest--)
  159. {
  160. cook();
  161. }
  162. stfu;
  163. }
  164.  
Success #stdin #stdout 0.01s 5780KB
stdin
12
2 4
stdout
47