1 条题解
-
1
#include #include #include using namespace std;
int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}; int dy[] = {1, 2, 2, 1, -1, -2, -2, -1}; const int MAXL = 305; int dist[MAXL][MAXL];
struct Point { int x, y; Point(int x_, int y_) : x(x_), y(y_) {} };
int bfs(int L, int sx, int sy, int ex, int ey) {
if (sx == ex && sy == ey) return 0; memset(dist, -1, sizeof(dist)); queue<Point> q; dist[sx][sy] = 0; q.emplace(sx, sy); while (!q.empty()) { Point cur = q.front(); q.pop(); for (int i = 0; i < 8; ++i) { int nx = cur.x + dx[i]; int ny = cur.y + dy[i]; if (nx >= 0 && nx < L && ny >= 0 && ny < L && dist[nx][ny] == -1) { dist[nx][ny] = dist[cur.x][cur.y] + 1; if (nx == ex && ny == ey) { return dist[nx][ny]; } q.emplace(nx, ny); } } } return -1;}
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int L; cin >> L; int sx, sy, ex, ey; cin >> sx >> sy; cin >> ex >> ey; cout << bfs(L, sx, sy, ex, ey) << '\n'; } return 0; }
信息
- ID
- 1109
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 9
- 标签
- 递交数
- 15
- 已通过
- 3
- 上传者