1 条题解
-
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 4000; int n; int x, y, xx, yy; struct Point { // 位置,区间,点的编号 int pos, l, r, idx; }; // 横向,竖向 vector<Point> row, col; // 图 int tot = 0; vector<int> e[MAXN * 4 + 2 + 5]; void linkRow(int x, int y, int idx) { int L = -1, R = -1; for (int i = 0; i < row.size(); i++) { // 下 if (row[i].pos <= y && row[i].l <= x && x <= row[i].r && (L == -1 || row[i].pos > row[L].pos)) L = i; // 上 if (row[i].pos >= y && row[i].l <= x && x <= row[i].r && (R == -1 || row[i].pos < row[R].pos)) R = i; } if (x == xx && y < yy && (R == -1 || row[R].pos > yy)) e[idx].push_back(tot); else if (R != -1) e[idx].push_back(row[R].idx); if (x == xx && y > yy && (L == -1 || row[L].pos < yy)) e[idx].push_back(tot); else if (L != -1) e[idx].push_back(row[L].idx); } void linkCol(int x, int y, int idx) { int L = -1, R = -1; for (int i = 0; i < col.size(); i++) { // 左 if (col[i].pos <= x && col[i].l <= y && y <= col[i].r && (L == -1 || col[i].pos > col[L].pos)) L = i; // 右 if (col[i].pos >= x && col[i].l <= y && y <= col[i].r && (R == -1 || col[i].pos < col[R].pos)) R = i; } if (y == yy && x < xx && (R == -1 || col[R].pos > xx)) e[idx].push_back(tot); else if (R != -1) e[idx].push_back(col[R].idx); if (y == yy && x > xx && (L == -1 || col[L].pos < xx)) e[idx].push_back(tot); else if (L != -1) e[idx].push_back(col[L].idx); } int dis[MAXN * 4 + 2 + 5]; queue<int> q; signed main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; cin >> x >> y >> xx >> yy; tot = 1; // 1 号点为起点 for (int i = 1; i <= n; i++) { int x_1, y_1, x_2, y_2; cin >> x_1 >> y_1 >> x_2 >> y_2; // 四条边放入 row.push_back({y_1 - 1, x_1, x_2, ++tot}); row.push_back({y_2 + 1, x_1, x_2, ++tot}); col.push_back({x_1 - 1, y_1, y_2, ++tot}); col.push_back({x_2 + 1, y_1, y_2, ++tot}); } tot++; // tot 号点为终点 // 建边 linkRow(x, y, 1), linkCol(x, y, 1); linkRow(x, y, tot), linkCol(x, y, tot); for (int i = 0; i < row.size(); i++) { linkCol(row[i].l, row[i].pos, row[i].idx); linkRow(col[i].pos, col[i].l, col[i].idx); } // bfs 最短路 for (int i = 1; i <= tot; i++) dis[i] = -1; dis[1] = 0; q.push(1); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (dis[v] != -1) continue; dis[v] = dis[u] + 1; q.push(v); } } if (dis[tot] == -1) cout << 0; else cout << dis[tot]; return 0; }
- 1
信息
- ID
- 1831
- 时间
- 1000ms
- 内存
- 125MiB
- 难度
- 5
- 标签
- 递交数
- 8
- 已通过
- 1
- 上传者