4 条题解
-
-4
大范围贪心,小范围暴搜。
应该可以将贪心过程优化到 ,但我懒得想了。
#include <bits/stdc++.h> using namespace std; int bfs(int x, int y) { queue<pair<pair<int, int>, int> > q; q.push(make_pair(make_pair(0, 0), 0)); while (!q.empty()) { auto tp = q.front(); q.pop(); if (tp.first.first == x && tp.first.second == y) return tp.second; q.push(make_pair(make_pair(tp.first.first + 1, tp.first.second + 2), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first + 2, tp.first.second + 1), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first + 2, tp.first.second - 1), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first + 1, tp.first.second - 2), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first - 1, tp.first.second - 2), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first - 2, tp.first.second - 1), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first - 2, tp.first.second + 1), tp.second + 1)); q.push(make_pair(make_pair(tp.first.first - 1, tp.first.second + 2), tp.second + 1)); } return INT32_MAX; } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int a, b, c, d, cnt = 0; cin >> a >> b >> c >> d; int x = abs(a - c), y = abs(b - d); while (x >= 4 || y >= 4) { if (x < 0) x = -x; if (y < 0) y = -y; if (x < y) swap(x, y); x -= 2, y -= 1; ++cnt; } cout << bfs(x, y) + cnt; return 0; }
信息
- ID
- 111
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 34
- 已通过
- 7
- 上传者