http://www.lydsy.com/JudgeOnline/problem.php?id=2541
https://vijos.org/p/1366
loli秘制大爆搜_(:з」∠)_坑了好久啊QAQ一上午花了2h+写这道题,最后WA了2个点,调了一下午多。
在矩形一条边上的所有位置都是等效的,所以把每个矩形的四条边拆开,表示为点。每个点只会连出去一条边,暴力找边建图,最后跑堆优dij。
时间复杂度(O(n^2))(附带大常数16一脸不可过?不过最后竟然过了)
一开始认为有环爆搜不可做,便建图跑了最短路。后来xyx0711跟我说直接bfs不走环,因为正确的路不会走环,后来想想也是。。。但是懒得改了。
恶心经历:冰块一次推进洞真心没想到,改完之后还WA第8个点,后来发现判断和T的连边时少写了一个条件,快速写上过了那个点,再评测发现第一个点突然又WA了,查了好久发现因为之前写得太急把坐标打反了(坐标打反了都能放过我9个点,这数据。。。)
改第8个点改了一下午TWT,自己画了一下原图,还手动模拟出了正确答案QAQ
起点是最左上角的点,终点是最靠右边的点,这个是离散过坐标轴后画出来的图
真心代码题
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int in() {
int k = 0, fh = 1; char c = getchar();
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for (; c >= '0' && c <= '9'; c = getchar())
k = k * 10 + c - 48;
return k * fh;
}
const int N = 4003;
const int jie[4][2] = {3, 1, 2, 0, 3, 1, 2, 0};
int n, xs, xt, ys, yt, xa[N], ya[N], xb[N], yb[N], num[N][4], to[N][4][2], tot = 0, l, r, up, tt[4];
struct node {int nxt, to;} E[N * 4 * 2 + 100];
int cnt = 0, point[N * 4], dist[N * 4], S, T;
bool inq[N * 4];
void ins(int u, int v) {E[++cnt] = (node) {point[u], v}; point[u] = cnt;}
struct Qnode {
int id, dist;
Qnode (int _id = 0, int _dist = 0) : id(_id), dist(_dist) {}
bool operator < (const Qnode &A) const {
return dist > A.dist;
}
};
priority_queue <Qnode> Q;
int main() {
n = in(); xs = in(); ys = in(); xt = in(); yt = in();
for (int i = 1; i <= n; ++i) {
xa[i] = in();
ya[i] = in();
xb[i] = in();
yb[i] = in();
for (int j = 0; j < 4; ++j)
num[i][j] = ++tot;
}
if (xs == xt) {
l = ys; r = yt;
if (l > r) swap(l, r);
up = 0;
for (int i = 1; i <= n; ++i)
if (yb[i] < l || ya[i] > r || xa[i] > xs || xb[i] < xs)
++up;
if (up == n) {puts("1"); return 0;}
}
if (ys == yt) {
l = xs; r = xt;
if (l > r) swap(l, r);
up = 0;
for (int i = 1; i <= n; ++i)
if (yb[i] < ys || ya[i] > ys || xa[i] > r || xb[i] < l)
++up;
if (up == n) {puts("1"); return 0;}
}
for (int i = 1; i <= n; ++i) {
l = ya[i]; r = yb[i]; up = xa[i];
for (int j = 1; j <= n; ++j)
if (xb[j] < up) {
if (l <= ya[j] - 1 && ya[j] - 1 <= r) {
if ((to[j][1][1] == 0) || (xa[to[j][1][1]] > up))
to[j][1][1] = i;
}
if (l <= yb[j] + 1 && yb[j] + 1 <= r) {
if ((to[j][3][1] == 0) || (xa[to[j][3][1]] > up))
to[j][3][1] = i;
}
}
l = xa[i]; r = xb[i]; up = ya[i];
for (int j = 1; j <= n; ++j)
if (yb[j] < up) {
if (l <= xa[j] - 1 && xa[j] - 1 <= r) {
if ((to[j][0][1] == 0) || (ya[to[j][0][1]] > up))
to[j][0][1] = i;
}
if (l <= xb[j] + 1 && xb[j] + 1 <= r) {
if ((to[j][2][1] == 0) || (ya[to[j][2][1]] > up))
to[j][2][1] = i;
}
}
l = ya[i]; r = yb[i]; up = xb[i];
for (int j = 1; j <= n; ++j)
if (xa[j] > up) {
if (l <= ya[j] - 1 && ya[j] - 1 <= r) {
if ((to[j][1][0] == 0) || (xb[to[j][1][0]] < up))
to[j][1][0] = i;
}
if (l <= yb[j] + 1 && yb[j] + 1 <= r) {
if ((to[j][3][0] == 0) || (xb[to[j][3][0]] < up))
to[j][3][0] = i;
}
}
l = xa[i]; r = xb[i]; up = yb[i];
for (int j = 1; j <= n; ++j)
if (ya[j] > up) {
if (l <= xa[j] - 1 && xa[j] - 1 <= r) {
if ((to[j][0][0] == 0) || (yb[to[j][0][0]] < up))
to[j][0][0] = i;
}
if (l <= xb[j] + 1 && xb[j] + 1 <= r) {
if ((to[j][2][0] == 0) || (yb[to[j][2][0]] < up))
to[j][2][0] = i;
}
}
}
for (int i = 1; i <= n; ++i)
for (int j = 0; j < 4; ++j)
for (int k = 0; k < 2; ++k)
if (to[i][j][k])
ins(num[i][j], num[to[i][j][k]][jie[j][k]]);
S = ++tot; T = ++tot;
int x = xs, y = ys;
for (int i = 1; i <= n; ++i) {
if (xa[i] > x && ya[i] <= y && y <= yb[i]) {
if ((tt[0] == 0) || (xa[i] < xa[tt[0]]))
tt[0] = i;
}
if (ya[i] > y && xa[i] <= x && x <= xb[i]) {
if ((tt[1] == 0) || (ya[i] < ya[tt[1]]))
tt[1] = i;
}
if (xb[i] < x && ya[i] <= y && y <= yb[i]) {
if ((tt[2] == 0) || (xb[i] > xb[tt[2]]))
tt[2] = i;
}
if (yb[i] < y && xa[i] <= x && x <= xb[i]) {
if ((tt[3] == 0) || (yb[i] > yb[tt[3]]))
tt[3] = i;
}
}
for (int i = 0; i < 4; ++i)
if (tt[i]) ins(S, num[tt[i]][i]);
x = xt; y = yt;
for (int i = 1; i <= n; ++i) {
if (xa[i] - 1 == x) {
if (y < ya[i] && ((to[i][0][0] == 0 && ya[i] > y) || (to[i][0][0] != 0 && yb[to[i][0][0]] < y)))
ins(num[i][0], T);
if (y > yb[i] && ((to[i][0][1] == 0 && yb[i] < y) || (to[i][0][1] != 0 && ya[to[i][0][1]] > y)))
ins(num[i][0], T);
}
if (ya[i] - 1 == y) {
if (x < xa[i] && ((to[i][1][0] == 0 && xa[i] > x) || (to[i][1][0] != 0 && xb[to[i][1][0]] < x)))
ins(num[i][1], T);
if (x > xb[i] && ((to[i][1][1] == 0 && xb[i] < x) || (to[i][1][1] != 0 && xa[to[i][1][1]] > x)))
ins(num[i][1], T);
}
if (xb[i] + 1 == x) {
if (y < ya[i] && ((to[i][2][0] == 0 && ya[i] > y) || (to[i][2][0] != 0 && yb[to[i][2][0]] < y)))
ins(num[i][2], T);
if (y > yb[i] && ((to[i][2][1] == 0 && yb[i] < y) || (to[i][2][1] != 0 && ya[to[i][2][1]] > y)))
ins(num[i][2], T);
}
if (yb[i] + 1 == y) {
if (x < xa[i] && ((to[i][3][0] == 0 && xa[i] > x) || (to[i][3][0] != 0 && xb[to[i][3][0]] < x)))
ins(num[i][3], T);
if (x > xb[i] && ((to[i][3][1] == 0 && xb[i] < x) || (to[i][3][1] != 0 && xa[to[i][3][1]] > x)))
ins(num[i][3], T);
}
}
memset(dist, 127, sizeof(dist));
dist[S] = 0;
Q.push(Qnode(S, 0));
Qnode u;
while (!Q.empty()) {
u = Q.top(); Q.pop();
if (inq[u.id]) continue;
inq[u.id] = true;
for (int i = point[u.id]; i; i = E[i].nxt)
if (dist[u.id] + 1 < dist[E[i].to]) {
dist[E[i].to] = dist[u.id] + 1;
Q.push(Qnode(E[i].to, dist[E[i].to]));
}
}
printf("%d
", dist[T] == dist[0] ? 0 : dist[T]);
return 0;
}