qwq超时了一个点(剩下的有WA的,但我觉得超时可能更好找一些)
不要看我A了这道题,那是复制了题解,方便看别人的代码
#include <bits/stdc++.h>
using namespace std;
const int N = 4005, INF = 1e9 + 1;
typedef unordered_map<int, int> ump;
struct mot {
int _x1, _y1, _x2, _y2;
} a[N];
struct zs {
int x, y;
};
int _x1, _y1, _x2, _y2, n;
unordered_map <int, ump> dis, vis;
bool k;
zs check(int id, int x, int y, int i) {
mot b = a[id];
zs inf = (zs){INF, INF};
int minx = min(b._x1, b._x2), maxx = max(b._x1, b._x2);
int miny = min(b._y1, b._y2), maxy = max(b._y1, b._y2);
if (i <= 1) {
if (x >= minx && x <= maxx) {
if (i == 0 && y > maxy) y = maxy + 1; // 向下
else if (i == 1 && y < miny) y = miny - 1; // 上
else return inf;
} else return inf;
} else {
if (y >= miny && y <= maxy) {
if (i == 2 && x > maxx) x = maxx + 1; // 左
else if (i == 3 && x < minx) x = minx - 1; // 右
else return inf;
} else return inf;
}
if (id == n) k = 1;
return (zs) {x, y};
}
void BFS() {
queue <zs> q;
q.push((zs){_x1, _y1});
vis[_x1][_y1] = 1;
while (q.size()) {
zs u = q.front();
//cout << u.x << " " << u.y << "!\n";
q.pop();
// if (abs(u.x - _x2) + abs(u.y - _y2) <= 1) {
// cout << dis[u.x][u.y] - 1;
// exit(0);
// }
for (int i = 0; i < 4; i++) {
k = 0;
int mx = u.x, my = u.y;
for (int j = 1; j <= n; j++) {
zs now = check(j, u.x, u.y, i);
int nx = now.x, ny = now.y;
if ((nx == INF && ny == INF) || dis[nx][ny]) continue;
//cout << i << " " << nx << " " << ny << "\n";
if (i == 0) { // 下
if (ny > my || (my == u.y)) my = ny;
}
else if (i == 1) { // 上
if (ny < my || (my == u.y)) my = ny;
}
else if (i == 2) { // 左
if (nx > mx || (mx == u.x)) mx = nx;
}
else if (nx < mx || (mx == u.x)) mx = nx; // 右
}
//cout << mx << " " << my << "\n";
if (k && abs(mx - _x2) + abs(my - _y2) <= 1) {
cout << dis[u.x][u.y] + 1;
exit(0);
}
if ((mx != u.x || my != u.y) && !vis[mx][my]) {
vis[mx][my] = 1;
dis[mx][my] = dis[u.x][u.y] + 1;
q.push((zs){mx, my});
}
}
}
}
int main() {
cin >> n >> _x1 >> _y1 >> _x2 >> _y2;
for (int i = 1; i <= n; i++)
cin >> a[i]._x1 >> a[i]._y1 >> a[i]._x2 >> a[i]._y2;
n++;
a[n]._x1 = a[n]._x2 = _x2;
a[n]._y1 = a[n]._y2 = _y2;
BFS();
//cout << dis[_x2][_y2] - 1;
cout << 0;
return 0;
}