Advertisement
Alickon

Untitled

Jan 30th, 2023 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define flash ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
  6. #define stop return 0
  7. #define pb push_back
  8. #define ll long long
  9. #define dbg(x) cerr << #x << " = " << x << "\n" ;
  10. #define ff first
  11. #define ss second
  12.  
  13. /*
  14. #pragma GCC target ("avx2")
  15. #pragma GCC optimization ("O3")
  16. #pragma comment (linker, "/stack:200000000")
  17. #pragma GCC optimize("Ofast")
  18. #pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. */
  20.  
  21. const long long INF = 1e9 + 7;
  22. const double MIN = 1e-6;
  23. const long long ARR = 1e6 + 11;
  24. const long long DRR = 1e3 + 11;
  25. const long long MOD = 12347;
  26. const int maxn = 2e5 + 10;
  27.  
  28. int n, m;
  29. int dst[2111][2111];
  30. queue <pair <int, int> > q;
  31.  
  32. pair <int, int> col[] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
  33.  
  34. void press_F_() {
  35. cin >> n >> m;
  36. if (n == 1) {
  37. int x, y, t;
  38. cin >> x >> y >> t;
  39. int mx = 0;
  40. int ans1, ans2, ans3, ans4;
  41. ans1 = max(abs(m - y), abs(m - x));
  42. ans2 = max(abs(1 - y), abs(m - x));
  43. ans3 = max(abs(m - y), abs(1 - x));
  44. ans4 = max(abs(1 - y), abs(1 - x));
  45. cout << max(max(ans1, ans2), max(ans3, ans4)) + t - 1;
  46. return;
  47. }
  48. for (int i = 1; i <= m; i++) {
  49. for (int j = 1; j <= m; j++) {
  50. dst[i][j] = INF;
  51. }
  52. }
  53. for (int i = 1; i <= n; i++) {
  54. int x, y, t;
  55. cin >> x >> y >> t;
  56. dst[x][y] = t;
  57. q.push({x, y});
  58. }
  59. while(!q.empty()) {
  60. pair <int, int> v;
  61. v = q.front();
  62. q.pop();
  63. for (int i = 0; i < 8; i++) {
  64. int dx = v.ff + col[i].ff, dy = v.ss + col[i].ss;
  65. if (dx >= 1 && dx <= m && dy >= 1 && dy <= m) {
  66. if (dst[dx][dy] > dst[v.ff][v.ss] + 1) {
  67. dst[dx][dy] = dst[v.ff][v.ss] + 1;
  68. q.push({dx, dy});
  69. }
  70. }
  71. }
  72. }
  73. int mx = 0, pos1, pos2;
  74. for (int i = 1; i <= m; i++) {
  75. for (int j = 1; j <= m; j++) {
  76. if (dst[i][j] > mx) {
  77. mx = dst[i][j];
  78. }
  79. }
  80. }
  81. cout << mx - 1;
  82. }
  83.  
  84. int main() {
  85. flash;
  86. int T = 1;
  87. //cin >> T;
  88. for (int i = 1; i <= T; i++) {
  89. // cout << "Case" << " " << i << ':' << " ";
  90. press_F_();
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement