代码:
#include <bits/stdc++.h>
using namespace std;
int xb, yb, xw, yw;
struct xy{
int x, y;
};
int a[25][25], b[25][25];
queue <xy> q, w;
int f[20][5] = {{2, 1},
{2, -1},
{1, -2},
{-1, -2},
{-2, -1},
{-2, 1},
{-1, 2},
{1, 2},
{-2, -2},
{-2, 2},
{2, 2},
{2, -2}};
int main(){
cin >> xb >> yb >> xw >> yw;
q.push({xb, yb});
w.push({xw, yw});
while (!q.empty()){
xy d = q.front();
q.pop();
for (int i = 0; i < 12; i++){
int ex = d.x + f[i][0], ey = d.y + f[i][1];
if (ex < 1 || ex > max(xb, xw) || ey < 1 || ey > max(yb, yw) || a[ex][ey] != 0){
continue;
}
a[ex][ey] = a[d.x][d.y] + 1;
q.push({ex, ey});
}
}
while (!w.empty()){
xy d = w.front();
w.pop();
for (int i = 0; i < 12; i++){
int ex = d.x + f[i][0], ey = d.y + f[i][1];
if (ex < 1 || ex > max(xb, xw) || ey < 1 || ey > max(yb, yw) || b[ex][ey] != 0){
continue;
}
b[ex][ey] = b[d.x][d.y] + 1;
w.push({ex, ey});
}
}
cout << a[1][1] << "\n" << b[1][1];
return 0;
}