usaco-camp

Short, concise solutions for problems from the USACO camp

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;

const int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
const int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };

int d[44][44];
bool b[44][44];
inline int & dist(int i, int j) { return d[i + 22][j + 22]; }
inline bool & vis(int i, int j) { return b[i + 22][j + 22]; }

ii P[202];
int dp[202][101];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);

	int N, R, C;
	cin >> N >> R >> C;
	for (int i = 0; i < N; ++i) {
		cin >> P[i].f >> P[i].s;
		--P[i].f, --P[i].s;
	}

	// BFS to compute distances
	auto valid = [&R, &C](int i, int j) { return i > -R && i < R && j > -C && j < C; };
	dist(0, 0) = 0;
	queue<ii> q;
	q.emplace(0, 0);
	while (q.size()) {
		ii u = q.front();
		q.pop();
		if (vis(u.f, u.s)) continue;
		vis(u.f, u.s) = 1;
		for (int d = 0; d < 8; ++d) {
			ii v(u.f + dx[d], u.s + dy[d]);
			if (valid(v.f, v.s) && !vis(v.f, v.s)) {
				dist(v.f, v.s) = dist(u.f, u.s) + 1;
				q.push(v);
			}
		}
	}

	// O(R^2 * C^2 * N^2)
	int ans = 1e9;
	for (int a = 0; a < R; ++a) {
		for (int b = 0; b < C; ++b) {
			for (int c = a; c < R; ++c) {
				for (int d = 0; d < C; ++d) {
					if (a == c && b == d) continue;
					// First meetup location: (a, b)
					// Second meetup location: (c, d)
					memset(dp, '?', sizeof dp);
					dp[0][0] = 0;
					for (int i = 0; i < N; ++i) {
						for (int j = 0; j <= min(i, N / 2); ++j) {
							dp[i + 1][j] = min(dist(P[i].f - a, P[i].s - b) + dp[i][j], dp[i + 1][j]);
							dp[i + 1][j + 1] = min(dist(P[i].f - c, P[i].s - d) + dp[i][j], dp[i + 1][j + 1]);
						}
					}
					ans = min(dp[N][N / 2], ans);
				}
			}
		}
	}
	cout << ans << '\n';
}