[HNOI2006] 马步距离
2024年12月25日大约 2 分钟
打表
#include <bits/stdc++.h>
using namespace std;
int a[40][40];
queue<pair<int, int>> q;
int dx[] = {2, 2, 1, 1, -2, -2, -1, -1};
int dy[] = {1, -1, 2, -2, 1, -1, 2, -2};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 1; i <= 40; i++)
for (int j = 1; j <= 40; j++)
a[i][j] = -1;
q.push(make_pair(20, 20));
a[20][20] = 0;
while (!q.empty())
{
pair<int, int> now = q.front();
q.pop();
for (int i = 0; i < 8; i++)
{
int nx = now.first + dx[i];
int ny = now.second + dy[i];
if (1 <= nx && nx <= 40 &&
1 <= ny && ny <= 40 &&
a[nx][ny] == -1)
{
a[nx][ny] = a[now.first][now.second] + 1;
q.push(make_pair(nx, ny));
}
}
}
for (int i = 20; i <= 30; i++)
{
for (int j = 20; j <= 30; j++)
cout << a[i][j] << ",";
cout << "\n";
}
return 0;
}
/*
0,3,2,3,2,3,4,5,4,5,6,
3,2,1,2,3,4,3,4,5,6,5,
2,1,4,3,2,3,4,5,4,5,6,
3,2,3,2,3,4,3,4,5,6,5,
2,3,2,3,4,3,4,5,4,5,6,
3,4,3,4,3,4,5,4,5,6,5,
4,3,4,3,4,5,4,5,6,5,6,
5,4,5,4,5,4,5,6,5,6,7,
4,5,4,5,4,5,6,5,6,7,6,
5,6,5,6,5,6,5,6,7,6,7,
6,5,6,5,6,5,6,7,6,7,8
*/
大范围贪心,小范围搜索枚举
#include <bits/stdc++.h>
using namespace std;
int len[11][11] = {
0, 3, 2, 3, 2, 3, 4, 5, 4, 5, 6,
3, 2, 1, 2, 3, 4, 3, 4, 5, 6, 5,
2, 1, 4, 3, 2, 3, 4, 5, 4, 5, 6,
3, 2, 3, 2, 3, 4, 3, 4, 5, 6, 5,
2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6,
3, 4, 3, 4, 3, 4, 5, 4, 5, 6, 5,
4, 3, 4, 3, 4, 5, 4, 5, 6, 5, 6,
5, 4, 5, 4, 5, 4, 5, 6, 5, 6, 7,
4, 5, 4, 5, 4, 5, 6, 5, 6, 7, 6,
5, 6, 5, 6, 5, 6, 5, 6, 7, 6, 7,
6, 5, 6, 5, 6, 5, 6, 7, 6, 7, 8};
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int a, b, aa, bb;
cin >> a >> b >> aa >> bb;
// 算出要走的距离 +a +b,保证 a>=b
a = abs(a - aa);
b = abs(b - bb);
if (a < b)
swap(a, b);
int ans = 0;
// 贪心通过 +2 +1 完成 +a +b
while (a >= 10 || b >= 10)
{
ans++;
a = abs(a - 2);
b = abs(b - 1);
if (a < b)
swap(a, b);
}
cout << ans + len[a][b];
return 0;
}