Advertisement
Alex_tz307

USACO 2016 January Contest, Gold Problem 2. Radio Contact

Jul 26th, 2021
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define sqr(x) (x) * (x)
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("radio.in");
  7. ofstream fout("radio.out");
  8.  
  9. const int64_t INF = 1e18L;
  10.  
  11. int moveX(char c) {
  12.   if (c == 'E')
  13.     return 1;
  14.   if (c == 'W')
  15.     return -1;
  16.   return 0;
  17. }
  18.  
  19. int moveY(char c) {
  20.   if (c == 'N')
  21.     return 1;
  22.   if (c == 'S')
  23.     return -1;
  24.   return 0;
  25. }
  26.  
  27. int cost(const pair<int,int> &a, const pair<int,int> &b) {
  28.   return sqr(b.first - a.first) + sqr(b.second - a.second);
  29. }
  30.  
  31. void min_self(int64_t &x, int64_t y) {
  32.   x = min(x, y);
  33. }
  34.  
  35. int main() {
  36.   int n, m;
  37.   vector<pair<int,int>> a(1), b(1);
  38.   string s, t;
  39.   fin >> n >> m >> a[0].first >> a[0].second >> b[0].first >> b[0].second >> s >> t;
  40.   for (char c : s) {
  41.     int dx = moveX(c), dy = moveY(c);
  42.     a.emplace_back(a.back().first + dx, a.back().second + dy);
  43.   }
  44.   for (char c : t) {
  45.     int dx = moveX(c), dy = moveY(c);
  46.     b.emplace_back(b.back().first + dx, b.back().second + dy);
  47.   }
  48.   vector<vector<int64_t>> dp(n + 1, vector<int64_t>(m + 1, INF));
  49.   dp[0][0] = 0;
  50.   for (int i = 0; i <= n; ++i)
  51.     for (int j = 0; j <= m; ++j) {
  52.       int add = cost(a[i], b[j]);
  53.       if (i && j)
  54.         min_self(dp[i][j], dp[i - 1][j - 1] + add);
  55.       if (i)
  56.         min_self(dp[i][j], dp[i - 1][j] + add);
  57.       if (j)
  58.         min_self(dp[i][j], dp[i][j - 1] + add);
  59.     }
  60.   fout << dp[n][m] << '\n';
  61.   fin.close();
  62.   fout.close();
  63.   return 0;
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement