fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. struct State {
  7. int x, y;
  8. int dx, dy;
  9. };
  10.  
  11. signed main() {
  12. ios::sync_with_stdio(false);
  13. cin.tie(nullptr);
  14.  
  15. int T;
  16. cin >> T;
  17.  
  18. while (T--) {
  19. int n, m, x, y;
  20. cin >> n >> m >> x >> y;
  21.  
  22. vector<bool> row(n + 1, false);
  23. vector<bool> col(m + 1, false);
  24.  
  25. int cntRow = 0;
  26. int cntCol = 0;
  27.  
  28. State cur = {x, y, 1, 1};
  29.  
  30. int time = 0;
  31.  
  32. while (true) {
  33.  
  34. // lau hàng và cột hiện tại
  35. if (!row[cur.x]) {
  36. row[cur.x] = true;
  37. cntRow++;
  38. }
  39.  
  40. if (!col[cur.y]) {
  41. col[cur.y] = true;
  42. cntCol++;
  43. }
  44.  
  45. // đã lau hết
  46. if (cntRow == n || cntCol == m) {
  47. cout << time << '\n';
  48. break;
  49. }
  50.  
  51. // phản xạ theo hàng
  52. if (cur.x + cur.dx < 1 || cur.x + cur.dx > n)
  53. cur.dx *= -1;
  54.  
  55. // phản xạ theo cột
  56. if (cur.y + cur.dy < 1 || cur.y + cur.dy > m)
  57. cur.dy *= -1;
  58.  
  59. // di chuyển
  60. cur.x += cur.dx;
  61. cur.y += cur.dy;
  62.  
  63. time++;
  64. }
  65. }
  66.  
  67. return 0;
  68. }
Success #stdin #stdout 0.01s 5324KB
stdin
5
10 10 6 1
10 10 9 9
9 8 5 6
6 9 2 2
2 2 1 1
stdout
9
10
9
9
1