text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
void oj() {}
long long dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long dy[] = {0, 1, -1, 1, -1, 0, 1, -1};
long long solve() {
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
long long n;
cin >> n;
long long r, a, b;
map<pair<long long, long long>, long long> mp, vis, dist;
for (long long i = 0; i < n; i++) {
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) {
mp[{r, j}]++;
}
}
queue<pair<pair<long long, long long>, long long>> q;
q.push({{x0, y0}, 0});
vis[{x0, y0}]++;
while (!q.empty()) {
auto it = q.front();
q.pop();
long long x = it.first.first;
long long y = it.first.second;
long long steps = it.second;
for (long long i = 0; i < 8; i++) {
long long newi = x + dx[i];
long long newj = y + dy[i];
if (vis.find({newi, newj}) == vis.end() &&
mp.find({newi, newj}) != mp.end()) {
q.push({{newi, newj}, steps + 1});
vis[{newi, newj}]++;
if (newi == x1 && newj == y1) return (steps + 1);
}
}
}
return -1;
}
int32_t main() {
oj();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
cout << solve() << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> dis;
set<pair<int, int> > edge;
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n, r, L, R;
cin >> n;
while (n--) {
cin >> r >> L >> R;
for (int i = L; i <= R; i++) {
edge.insert(make_pair(r, i));
}
}
queue<pair<int, int> > q;
q.push(make_pair(x0, y0));
dis[make_pair(x0, y0)] = 0;
while (!q.empty()) {
pair<int, int> a = q.front();
int x = a.first;
int y = a.second;
int d = dis[make_pair(x, y)];
q.pop();
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int X = x + i, Y = y + j;
if (dis.count(make_pair(X, Y)) || !edge.count(make_pair(X, Y))) {
continue;
}
q.push(make_pair(X, Y));
dis[make_pair(X, Y)] = 1 + d;
}
}
}
if (!dis.count(make_pair(x1, y1))) dis[make_pair(x1, y1)] = -1;
cout << dis[make_pair(x1, y1)];
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 1, 1, -1, -1, -1, 0, 0};
const int dy[] = {1, 0, -1, 1, 0, -1, 1, -1};
int x0, yy0, x1, yy1, n;
struct point {
int x, y;
friend bool operator<(const point &a, const point &b) {
if (a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
} p[100001];
map<point, int> d;
map<point, bool> bo;
queue<point> q;
point s, t;
int main(int argc, char *argv[]) {
scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; ++j) {
point x;
x.x = r;
x.y = j;
d[x] = -1;
}
}
d[s] = 0;
bo[s] = 1;
q.push(s);
while (!q.empty()) {
point x = q.front();
q.pop();
bo[x] = 0;
for (int i = 0; i < 8; ++i) {
point u;
u.x = x.x + dx[i];
u.y = x.y + dy[i];
if (d[u] == -1) {
d[u] = d[x] + 1;
if (!bo[u]) {
q.push(u);
bo[u] = 1;
}
}
}
}
cout << d[t] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
map<pair<int, int>, int> valid;
map<pair<int, int>, int> visited;
int n;
cin >> n;
while (n--) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i < b + 1; i++) {
valid[make_pair(r, i)] = 1;
}
}
queue<pair<int, int> > q;
q.push(make_pair(r1, c1));
visited[make_pair(r1, c1)] = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
int x = p.first, y = p.second;
int level = visited[make_pair(x, y)] + 1;
if ((y > 1) && (valid.find(make_pair(x, y - 1)) != valid.end()) &&
(visited.find(make_pair(x, y - 1)) == visited.end())) {
q.push(make_pair(x, y - 1));
visited[make_pair(x, y - 1)] = level;
}
if ((y < 1000000000) && (valid.find(make_pair(x, y + 1)) != valid.end()) &&
(visited.find(make_pair(x, y + 1)) == visited.end())) {
q.push(make_pair(x, y + 1));
visited[make_pair(x, y + 1)] = level;
}
if ((x > 1) && (valid.find(make_pair(x - 1, y)) != valid.end()) &&
(visited.find(make_pair(x - 1, y)) == visited.end())) {
q.push(make_pair(x - 1, y));
visited[make_pair(x - 1, y)] = level;
}
if ((x < 1000000000) && (valid.find(make_pair(x + 1, y)) != valid.end()) &&
(visited.find(make_pair(x + 1, y)) == visited.end())) {
q.push(make_pair(x + 1, y));
visited[make_pair(x + 1, y)] = level;
}
if ((y > 1) && (x > 1) &&
(valid.find(make_pair(x - 1, y - 1)) != valid.end()) &&
(visited.find(make_pair(x - 1, y - 1)) == visited.end())) {
q.push(make_pair(x - 1, y - 1));
visited[make_pair(x - 1, y - 1)] = level;
}
if ((y < 1000000000) && (x < 1000000000) &&
(valid.find(make_pair(x + 1, y + 1)) != valid.end()) &&
(visited.find(make_pair(x + 1, y + 1)) == visited.end())) {
q.push(make_pair(x + 1, y + 1));
visited[make_pair(x + 1, y + 1)] = level;
}
if ((x > 1) && (y < 1000000000) &&
(valid.find(make_pair(x - 1, y + 1)) != valid.end()) &&
(visited.find(make_pair(x - 1, y + 1)) == visited.end())) {
q.push(make_pair(x - 1, y + 1));
visited[make_pair(x - 1, y + 1)] = level;
}
if ((x < 1000000000) && (y > 1) &&
(valid.find(make_pair(x + 1, y - 1)) != valid.end()) &&
(visited.find(make_pair(x + 1, y - 1)) == visited.end())) {
q.push(make_pair(x + 1, y - 1));
visited[make_pair(x + 1, y - 1)] = level;
}
if (visited.find(make_pair(r2, c2)) != visited.end()) break;
}
if (visited.find(make_pair(r2, c2)) == visited.end())
cout << -1 << "\n";
else
cout << visited[make_pair(r2, c2)] << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
struct vertT {
long long r, c;
};
bool operator<(vertT v1, vertT v2) {
if (v1.r != v2.r)
return v1.r > v2.r;
else
return v1.c > v2.c;
}
set<vertT> allowed;
map<vertT, long long> dist;
void bfs(long long rs, long long cs) {
queue<vertT> verts;
vertT v;
v.r = rs;
v.c = cs;
verts.push(v);
dist[v] = 0;
long long dx[] = {-1, 0, 1};
while (!verts.empty()) {
v = verts.front();
verts.pop();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i != 1 || j != 1) {
vertT w;
w.r = v.r + dx[i];
w.c = v.c + dx[j];
if (allowed.find(w) != allowed.end() && dist.find(w) == dist.end()) {
verts.push(w);
dist[w] = dist[v] + 1;
}
}
}
}
}
}
int main(void) {
long long rs, cs, re, ce;
cin >> rs >> cs >> re >> ce;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
vertT v;
v.r = r;
v.c = j;
allowed.insert(v);
}
}
bfs(rs, cs);
vertT v;
v.r = re;
v.c = ce;
if (dist.find(v) == dist.end())
cout << -1 << endl;
else
cout << dist[v] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007, dx[8] = {1, 1, 1, 0, 0, -1, -1, -1},
dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int xs, ys, xt, yt, n, r[maxn], a[maxn], b[maxn], d[maxn] = {}, cnt = 0;
bool vis[maxn] = {};
vector<int> row, col[maxn];
vector<pair<int, int> > seg[maxn], cells[maxn];
int dist(int x, int y) {
x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;
y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) -
cells[x].begin();
return d[cells[x][y].second];
}
void set_dist(int x, int y, int val) {
x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;
y = lower_bound(cells[x].begin(), cells[x].end(), make_pair(y, 0)) -
cells[x].begin();
d[cells[x][y].second] = val;
}
bool valid(int x, int y) {
if (!binary_search(row.begin(), row.end(), x)) return 0;
x = lower_bound(row.begin(), row.end(), x) - row.begin() + 1;
if (!binary_search(col[x].begin(), col[x].end(), y)) return 0;
return 1;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> xs >> ys >> xt >> yt >> n;
for (int i = 1; i <= n; ++i) {
cin >> r[i] >> a[i] >> b[i];
row.push_back(r[i]);
}
sort(row.begin(), row.end());
for (int i = 1; i <= n; ++i)
r[i] = lower_bound(row.begin(), row.end(), r[i]) - row.begin() + 1;
for (int i = 1; i <= n; ++i) seg[r[i]].push_back({a[i], b[i]});
for (int i = 1; i <= n; ++i) {
if (vis[r[i]]) continue;
sort(seg[r[i]].begin(), seg[r[i]].end());
int cur = 1;
for (auto it : seg[r[i]]) {
cur = max(it.first, cur);
for (int j = cur; j <= it.second; ++j)
cells[r[i]].push_back({j, ++cnt}), col[r[i]].push_back(j);
}
vis[r[i]] = 1;
}
queue<pair<int, int> > q;
q.push({xs, ys});
set_dist(xs, ys, 1);
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 8; ++i) {
int xx = dx[i] + x, yy = dy[i] + y;
if (valid(xx, yy) && dist(xx, yy) == 0)
set_dist(xx, yy, dist(x, y) + 1), q.push({xx, yy});
}
}
cout << dist(xt, yt) - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
pair<int, int> start, target;
scanf("%d %d %d %d", &start.first, &start.second, &target.first,
&target.second);
int n;
set<pair<int, int> > mat;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
for (int q = b; q <= c; q++) mat.insert(make_pair(a, q));
}
map<pair<int, int>, int> dist;
queue<pair<int, int> > q;
q.push(start);
int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
mat.erase(start);
dist[start] = 0;
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
if (u == target) break;
for (int i = 0; i < 9; i++) {
pair<int, int> tmp = make_pair(u.first + dx[i], u.second + dy[i]);
if (!mat.count(tmp)) continue;
dist[tmp] = dist[u] + 1;
q.push(tmp);
mat.erase(tmp);
}
}
printf("%d\n", dist.count(target) ? dist[target] : -1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double eps = (1e-9);
const int MOD = 1000000007;
int dcmp(double x, double y) { return fabs(x - y) <= eps ? 0 : x > y ? -1 : 1; }
set<pair<int, int> > can;
map<pair<int, int>, bool> visit;
int di[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dj[] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool valid(int i, int j) {
if (i < 1 || j < 1 || i > 1000000000 || j > 1000000000 ||
can.find({i, j}) == can.end())
return false;
return true;
}
int main() {
ios_base::sync_with_stdio(false);
int sti, stj, endi, endj;
cin >> sti >> stj >> endi >> endj;
pair<int, int> goal = {endi, endj};
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int row, l, r;
cin >> row >> l >> r;
for (int j = l; j <= r; ++j) {
can.insert({row, j});
}
}
queue<pair<int, int> > q;
q.push({sti, stj});
if (q.front() == goal) {
cout << 0 << endl;
return 0;
}
int sz, depth;
visit[{sti, stj}] = 1;
for (sz = (int)(q).size(), depth = 0; !q.empty();
++depth, sz = (int)(q).size()) {
while (sz--) {
pair<int, int> cur = q.front();
q.pop();
for (int i = 0, j = 0; i < 8; ++i, ++j) {
pair<int, int> to = {cur.first + di[i], cur.second + dj[j]};
if (valid(cur.first + di[i], cur.second + dj[j])) {
if (!visit[{cur.first + di[i], cur.second + dj[j]}]) {
if (to == goal) {
cout << depth + 1 << endl;
return 0;
}
q.push({cur.first + di[i], cur.second + dj[j]});
visit[{cur.first + di[i], cur.second + dj[j]}] = 1;
}
}
}
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dy[] = {1, -1, 1, 1, -1, -1, 0, 0};
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
map<int, vector<pair<int, int> > > allowed;
bool valid(int r, int c) {
if (r <= 10e9 && c <= 10e9 && r >= 0 && c >= 0)
for (auto v : allowed[r]) {
if (c >= v.first && c <= v.second) return true;
}
return false;
}
int bfs(int x1, int y1, int x2, int y2, int c) {
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(x1, y1), c));
map<pair<int, int>, int> vis;
while (!q.empty()) {
pair<pair<int, int>, int> u = q.front();
q.pop();
if (u.first.first == x2 && u.first.second == y2) {
return u.second;
}
if (++vis[u.first] > 1) continue;
u.second++;
for (int i = 0; i < 8; i++) {
int r2 = u.first.first + dy[i];
int c2 = u.first.second + dx[i];
if (valid(r2, c2)) {
q.push(make_pair(make_pair(r2, c2), u.second));
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int t1, t2, t3;
cin >> t1 >> t2 >> t3;
allowed[t1].push_back(make_pair(t2, t3));
}
cout << bfs(x1, y1, x2, y2, 0) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 1, 0, -1, -1, 1, 1, -1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
int xx[] = {-1, 1, 2, 2, 1, -1, -2, -2};
int yy[] = {2, 2, 1, -1, -2, -2, -1, 1};
map<pair<int, int>, int> mp;
pair<int, int> u, v;
int sx, sy, ex, ey;
void BFS() {
queue<pair<int, int> > q;
u.first = sx;
u.second = sy;
q.push(u);
mp[u] = 0;
while (!q.empty()) {
u = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int x = u.first + dx[i];
int y = u.second + dy[i];
v.first = x, v.second = y;
if (mp.find(v) != mp.end() && mp[v] == -1) {
mp[v] = mp[u] + 1;
q.push(v);
}
}
}
}
int main() {
int n, r, a, b, i;
while (scanf("%d %d %d %d", &sx, &sy, &ex, &ey) == 4) {
scanf("%d", &n);
mp.clear();
while (n--) {
scanf("%d %d %d", &r, &a, &b);
for (i = a; i <= b; i++) mp[make_pair(r, i)] = -1;
}
BFS();
printf("%d\n", mp[make_pair(ex, ey)]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > S;
set<pair<int, int> > bio;
int n;
int px, py, ex, ey;
queue<int> Q;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
inline void bfs() {
Q.push(px);
Q.push(py);
bio.insert(make_pair(px, py));
Q.push(0);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
int y = Q.front();
Q.pop();
int dist = Q.front();
Q.pop();
for (int i = 0; i < 8; i++) {
int X = x + dx[i];
int Y = y + dy[i];
if (X < 1 || X > 1000000000 || Y < 1 || Y > 1000000000) continue;
if (!bio.empty() && bio.find(make_pair(X, Y)) != bio.end()) continue;
if (!S.empty() && S.find(make_pair(X, Y)) == S.end()) continue;
if (X == ex && Y == ey) {
printf("%d\n", dist + 1);
return;
}
Q.push(X);
Q.push(Y);
Q.push(dist + 1);
bio.insert(make_pair(X, Y));
}
}
printf("-1\n");
}
int main(void) {
scanf("%d %d %d %d", &px, &py, &ex, &ey);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int r, a, b;
scanf("%d %d %d", &r, &a, &b);
for (int j = a; j <= b; j++) S.insert(make_pair(r, j));
}
bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
queue<pair<int, int> > que;
map<pair<int, int>, int> mm;
int x[8] = {1, -1, 0, 0, -1, -1, 1, 1};
int y[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int main() {
int x0, y0, x1, y1, n, i;
while (scanf("%d%d%d%d", &y0, &x0, &y1, &x1) != EOF) {
scanf("%d", &n);
mm.clear();
while (n--) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (i = a; i <= b; i++) mm[make_pair(i, r)] = -1;
}
mm[make_pair(x0, y0)] = 0;
mm[make_pair(x1, y1)] = -1;
que.push(make_pair(x0, y0));
while (!que.empty()) {
int tx = que.front().first;
int ty = que.front().second;
que.pop();
for (i = 0; i < 8; i++)
if (mm[make_pair(tx + x[i], ty + y[i])] < 0) {
mm[make_pair(tx + x[i], ty + y[i])] = mm[make_pair(tx, ty)] + 1;
que.push(make_pair(tx + x[i], ty + y[i]));
}
}
printf("%d\n", mm[make_pair(x1, y1)]);
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int const MAX = 999999999;
class Point {
public:
int x, y;
Point(int _x = 0, int _y = 0) { x = _x, y = _y; }
bool operator<(const Point &p) const {
if (y == p.y) return x < p.x;
return y < p.y;
}
bool operator==(const Point &p) const { return x == p.x && y == p.y; }
};
class hasher {
public:
unsigned operator()(const Point p) const {
hash<int> h;
return h(p.x) + h(p.y);
}
};
Point s, t;
unordered_map<Point, int, hasher> ma;
int m, r, a, b;
int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
int main() {
scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
scanf("%d", &m);
while (m--) {
scanf("%d%d%d", &r, &a, &b);
Point p(r, a);
while (p.y <= b) {
ma[p] = MAX;
p.y++;
}
}
Point p;
queue<Point> que;
que.push(s);
unordered_map<Point, int, hasher>::iterator it, it2;
it = ma.find(s);
it->second = 0;
while (!que.empty()) {
p = que.front();
que.pop();
it = ma.find(p);
for (int i = 0; i < 8; i++) {
Point q(p.x + dir[i][0], p.y + dir[i][1]);
it2 = ma.find(q);
if (it2 != ma.end() && it2->second > it->second + 1) {
it2->second = it->second + 1;
que.push(it2->first);
}
}
}
it = ma.find(t);
if (it->second == MAX)
printf("-1\n");
else
printf("%d\n", it->second);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void openfile() {
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
}
void input(long &n) { scanf("%ld", &n); }
void output(long n) { printf("%ld", n); }
typedef vector<long> GR[100000 + 10];
const long dirr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const long dirc[] = {-1, 0, 1, -1, 1, -1, 0, 1};
class oPoint {
public:
long r, c;
void input() { scanf("%ld %ld", &r, &c); }
oPoint(long rr, long cc) {
r = rr;
c = cc;
}
oPoint() { r = c = 0; }
inline friend bool operator==(const oPoint &a, const oPoint &b) {
return (a.r == b.r && a.c == b.c);
}
inline friend bool operator<(const oPoint &a, const oPoint &b) {
return (a.r < b.r || (a.r == b.r && a.c < b.c));
}
};
vector<oPoint> pList;
void input(oPoint &p1, oPoint &p2, vector<oPoint> &pList, long &n) {
p1.input();
p2.input();
long m;
input(m);
for (long i = (1); i <= (m); i++) {
long r, a, b;
input(r);
input(a);
input(b);
for (long c = (a); c <= (b); c++) {
pList.push_back(oPoint(r, c));
}
}
sort((pList).begin(), (pList).end());
typeof(pList.begin()) it = unique((pList).begin(), (pList).end());
pList.resize(it - pList.begin());
n = pList.size();
}
inline long find(oPoint p, const vector<oPoint> &pList, long ll, long rr) {
while (rr - ll > 1) {
long mid = (rr + ll) >> 1;
if (pList[mid] < p)
ll = mid + 1;
else
rr = mid;
}
if (pList[rr] == p)
return rr;
else if (pList[ll] == p)
return ll;
else
return -1;
}
void buildgraph(long n, const vector<oPoint> &pList, GR &a) {
for (long i = (0); i <= (n - 1); i++) {
for (long dir = (0); dir <= (8 - 1); dir++) {
oPoint newp = oPoint(pList[i].r + dirr[dir], pList[i].c + dirc[dir]);
long id = find(newp, pList, 0, n - 1);
if (id != -1) {
a[i].push_back(id);
}
}
}
}
queue<long> qx;
long BFS(long n, long st, long en, const GR &a, long x[]) {
for (long i = (0); i <= (n - 1); i++) x[i] = -1;
if (st == -1 || en == -1) return -1;
qx.push(st);
x[st] = 0;
while (!qx.empty()) {
long u = qx.front();
qx.pop();
for (typeof((a[u]).begin()) it = (a[u]).begin(); it != (a[u]).end(); it++) {
long v = *it;
if (x[v] == -1) {
x[v] = x[u] + 1;
if (v == en) return x[v];
qx.push(v);
}
}
}
return -1;
}
oPoint p1, p2;
long n;
GR a;
long x[100000 + 10];
int main() {
input(p1, p2, pList, n);
buildgraph(n, pList, a);
long ps = find(p1, pList, 0, n - 1);
long pt = find(p2, pList, 0, n - 1);
long res = BFS(n, ps, pt, a, x);
output(res);
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
pair<int, int> ini, fin;
cin >> ini.first >> ini.second >> fin.first >> fin.second;
int n;
cin >> n;
set<pair<int, int>> se;
for (int i = 0; i < n; i++) {
int r, be, en;
cin >> r >> be >> en;
for (int j = be; j <= en; j++) {
se.insert({r, j});
}
}
queue<pair<int, pair<int, int>>> q;
q.push({0, ini});
while (q.size()) {
pair<int, pair<int, int>> t = q.front();
if (t.second == fin) {
cout << t.first;
return 0;
}
q.pop();
for (int j = 0; j < 8; j++) {
if (se.count(
make_pair(t.second.first + dx[j], t.second.second + dy[j]))) {
q.push(make_pair(t.first + 1, make_pair(t.second.first + dx[j],
t.second.second + dy[j])));
se.erase(make_pair(t.second.first + dx[j], t.second.second + dy[j]));
}
}
}
cout << "-1";
}
|
#include <bits/stdc++.h>
using namespace std;
long long x, y, x2, y2;
map<long long, pair<long long, long long> > v;
set<pair<long long, long long> > st;
map<pair<long long, long long>, long long> m;
map<pair<long long, long long>, long long> dist;
long long xx[8] = {1, 1, 1, 0, -1, -1, -1, 0},
yy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
long long f = 0;
void bfs() {
queue<pair<long long, long long> > q;
dist[make_pair(x, y)] = 0;
q.push(make_pair(x, y));
while (!q.empty()) {
pair<long long, long long> p = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
long long l = p.first + xx[i], k = p.second + yy[i];
if (!m[make_pair(l, k)] && st.count(make_pair(l, k))) {
if (l == x2 && k == y2) {
f = 1;
}
m[make_pair(l, k)] = 1;
q.push(make_pair(l, k));
dist[make_pair(l, k)] = dist[p] + 1;
}
}
}
}
int main() {
cin >> x >> y >> x2 >> y2;
long long n;
cin >> n;
long long cnt = 0;
for (int i = 0; i < n; i++) {
long long r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
st.insert(make_pair(r, j));
}
}
bfs();
if (f) {
cout << dist[make_pair(x2, y2)] << endl;
} else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
const int MAX_N = 1e5 + 5;
const int INF = 1e9;
const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
struct data {
int r, a, b;
};
bool operator<(data lhs, data rhs) { return lhs.r < rhs.r; }
int n, a, b, x, y;
std::map<std::pair<int, int>, int> map;
std::queue<std::pair<int, int>> q;
int main() {
scanf("%d%d%d%d%d", &a, &b, &x, &y, &n);
for (int i = 0; i < n; ++i) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; ++j) map[{r, j}] = -1;
}
map[{a, b}] = 0;
for (q.push({a, b}); !q.empty(); q.pop()) {
std::pair<int, int> cur = q.front();
for (int i = 0; i < 8; ++i) {
std::pair<int, int> tar = {cur.first + dx[i], cur.second + dy[i]};
if (map.count(tar) && map[tar] == -1) {
map[tar] = map[cur] + 1;
q.push(tar);
}
}
}
if (map[{x, y}] == 0)
printf("-1\n");
else
printf("%d\n", map[{x, y}]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 1000000000;
double Time() { return double(clock()) / double(CLOCKS_PER_SEC); }
int n;
map<pair<int, int>, int> m;
pair<int, int> p0, p1;
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {1, -1, -1, 0, 1, 1, 0, -1};
queue<pair<int, int> > q;
int main() {
cin >> p0.first >> p0.second >> p1.first >> p1.second >> n;
for (int i = 1; i <= n; i++) {
int r, a, b;
cin >> r >> a >> b;
while (a <= b) {
m[make_pair(r, a)] = -1;
a++;
}
}
m[p0] = 0;
q.push(p0);
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> j = make_pair(now.first + dx[i], now.second + dy[i]);
if (m[j] == -1) {
m[j] = m[now] + 1;
q.push(j);
}
}
}
cout << m[p1];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
vector<int> L[100010];
int n;
int x0, y00, x1, y11;
bool used[100010];
int d[100010];
int F[100010], C[100010];
queue<int> Q;
map<pair<int, int>, int> m;
int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
void bfs(int source) {
Q.push(source);
used[source] = true;
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < 8; ++i) {
int xo = F[u] + dx[i];
int yo = C[u] + dy[i];
pair<int, int> p;
p.first = xo;
p.second = yo;
int to = m[p];
if (used[to] || m[p] == 0) continue;
used[to] = true;
Q.push(to);
d[to] = d[u] + 1;
}
}
}
int buscd(int r, int a, int b) {
if (a == b) return a;
int mid = (a + b) / 2;
pair<int, int> p;
p.first = r;
p.second = mid;
if (m[p] == 0)
return buscd(r, a, mid);
else
return buscd(r, mid + 1, b);
}
int busci(int r, int a, int b) {
if (a == b) return a;
int mid = (a + b + 1) / 2;
pair<int, int> p;
p.first = r;
p.second = mid;
if (m[p] == 0)
return buscd(r, mid, b);
else
return buscd(r, a, mid - 1);
}
int main() {
memset(used, false, sizeof(used));
memset(d, -1, sizeof(d));
cin >> x0 >> y00 >> x1 >> y11;
int pos1, pos2;
cin >> n;
int ct = 1;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
int x = a, y = b;
pair<int, int> p1, p2;
p1.first = r;
p1.second = a;
p2.first = r;
p2.second = b;
if (m[p1] != 0 && m[p2] == 0) x = buscd(r, a, b);
if (m[p1] == 0 && m[p2] != 0) y = busci(r, a, b);
if (m[p1] == 0 || m[p2] == 0) {
for (int j = x; j <= y; j++) {
pair<int, int> p;
p.first = r;
p.second = j;
m[p] = ct;
F[ct] = r;
C[ct] = j;
if (r == x0 && j == y00) pos1 = ct;
if (r == x1 && j == y11) pos2 = ct;
ct++;
}
}
}
bfs(pos1);
if (pos1 == pos2)
cout << "0" << endl;
else {
if (d[pos2] == -1)
cout << d[pos2] << endl;
else
cout << d[pos2] + 1 << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
map<pair<int, int>, int> dis;
map<pair<int, int>, bool> vis;
bool canplace(int row, int col) {
if (row > 0 and row <= 1e9 and col > 0 and col <= 1e9 and
mp[pair<int, int>(row, col)] == 1 and
vis[pair<int, int>(row, col)] == false)
return true;
return false;
}
int bfs(int x0, int y0, int x1, int y1) {
int row[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int col[8] = {1, 1, 1, 0, -1, -1, -1, 0};
queue<pair<int, int>> q;
q.push(pair<int, int>(x0, y0));
vis[pair<int, int>(x0, y0)] = true;
while (!q.empty()) {
pair<int, int> x;
x = q.front();
if (x == pair<int, int>(x1, y1)) return dis[pair<int, int>(x1, y1)];
q.pop();
for (int i = 0; i < 8; i++) {
if (canplace(x.first + row[i], x.second + col[i])) {
q.push(pair<int, int>(x.first + row[i], x.second + col[i]));
vis[pair<int, int>(x.first + row[i], x.second + col[i])] = true;
dis[pair<int, int>(x.first + row[i], x.second + col[i])] =
dis[pair<int, int>(x.first, x.second)] + 1;
}
}
}
return -1;
}
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
pair<int, int> p;
int a, b, c;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
for (int j = b; j <= c; j++) {
mp[pair<int, int>(a, j)] = 1;
}
}
cout << bfs(x0, y0, x1, y1) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 100010;
int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[] = {-1, 1, 0, -1, 1, 0, -1, 1};
queue<pair<pair<int, int>, int> > q;
int sx, sy, ex, ey, r, a, b, n;
pair<int, int> target, curpos;
pair<pair<int, int>, int> cur;
map<pair<int, int>, long long int> mmap;
bool vis[1 << 20];
int ind = 1;
int main() {
ios_base::sync_with_stdio(0);
scanf("%d", &(sx));
scanf("%d", &(sy));
scanf("%d", &(ex));
scanf("%d", &(ey));
target = make_pair(ex, ey);
scanf("%d", &(n));
for (int i = 0, _n = (n); i < _n; ++i) {
scanf("%d", &(r));
scanf("%d", &(a));
scanf("%d", &(b));
for (int x = a, _n = (b + 1); x < _n; ++x) {
if (mmap.find(make_pair(r, x)) == mmap.end()) {
mmap[make_pair(r, x)] = ind++;
}
}
}
q.push(make_pair(make_pair(sx, sy), 0));
int ans = -1;
while (!q.empty()) {
cur = q.front();
q.pop();
curpos = cur.first;
vis[mmap[curpos]] = 1;
if (curpos == target) {
ans = cur.second;
break;
}
for (int i = 0, _n = (8); i < _n; ++i) {
pair<int, int> nxt =
make_pair(curpos.first + dx[i], curpos.second + dy[i]);
if (mmap[nxt]) {
if (!vis[mmap[nxt]]) {
vis[mmap[nxt]] = 1;
q.push(make_pair(nxt, cur.second + 1));
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int sr = 0, sc = 0, dr = 0, dc = 0;
cin >> sr >> sc >> dr >> dc;
int ans = -1;
set<pair<int, int> > pts;
map<pair<int, int>, int> vis;
queue<pair<pair<int, int>, int> > qu;
qu.push({{sr, sc}, 0});
int n = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int r = 0, a = 0, b = 0;
cin >> r >> a >> b;
while (a <= b) {
pts.insert({r, a});
a++;
}
}
while (!qu.empty()) {
int r = qu.front().first.first;
int c = qu.front().first.second;
int d = qu.front().second;
qu.pop();
if (r == dr && c == dc) {
ans = d;
break;
}
if (!pts.count({r, c}) || vis[{r, c}] > 0) {
continue;
}
vis[{r, c}]++;
qu.push({{r, c + 1}, d + 1});
qu.push({{r, c - 1}, d + 1});
qu.push({{r + 1, c}, d + 1});
qu.push({{r - 1, c}, d + 1});
qu.push({{r - 1, c + 1}, d + 1});
qu.push({{r - 1, c - 1}, d + 1});
qu.push({{r + 1, c + 1}, d + 1});
qu.push({{r + 1, c - 1}, d + 1});
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
long long gcd(long long a, long long b) {
while (a) {
b %= a;
swap(a, b);
}
return b;
}
map<int, map<int, int>*>* pole = new map<int, map<int, int>*>();
map<int, bool> used;
int dx[] = {-1, 0, 1, 1, 0, -1, 1, -1};
int dy[] = {1, 1, 1, -1, -1, -1, 0, 0};
int main() {
int x0, y0, x1, y1;
int n;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
if (used.find(r) == used.end()) {
(*pole)[r] = new map<int, int>();
used[r] = true;
}
for (int j = a; j <= b; j++) (*(*pole)[r])[j] = -1;
}
swap(y0, x0);
swap(x1, y1);
(*(*pole)[y0])[x0] = 0;
queue<pair<int, int>> q;
q.push(make_pair(y0, x0));
while (!q.empty()) {
int x, y, W;
y = q.front().first;
x = q.front().second;
q.pop();
W = (*(*pole)[y])[x];
for (int i1 = 0; i1 < 8; i1++) {
int tx = x + dx[i1];
int ty = y + dy[i1];
if (used.find(ty) == used.end()) continue;
if ((*pole).find(ty) != (*pole).end())
if ((*(*pole)[ty]).find(tx) != (*(*pole)[ty]).end())
if ((*(*pole)[ty])[tx] == -1) {
(*(*pole)[ty])[tx] = W + 1;
q.push(make_pair(ty, tx));
}
}
}
printf("%d", (*(*pole)[y1])[x1]);
return 0;
}
|
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::make_pair;
using std::pair;
using std::sort;
using std::string;
using std::swap;
using std::vector;
using namespace std;
const int MAXN = 100005;
map<int, vector<int>> m;
int mas[MAXN][2];
bool solved = false;
int answer = -1;
void bfs(int x, int y, int xx, int yy) {
queue<pair<int, int>> q;
q.push(make_pair(x, y));
map<pair<int, int>, bool> used;
map<pair<int, int>, int> d;
used[make_pair(x, y)] = true;
while (!q.empty()) {
pair<int, int> v = q.front();
if (v.first == xx && v.second == yy) {
solved = true;
answer = d[v];
break;
}
q.pop();
pair<int, int> p1 = make_pair(v.first + 1, v.second);
pair<int, int> p2 = make_pair(v.first - 1, v.second);
pair<int, int> p3 = make_pair(v.first + 1, v.second + 1);
pair<int, int> p4 = make_pair(v.first + 1, v.second - 1);
pair<int, int> p5 = make_pair(v.first, v.second + 1);
pair<int, int> p6 = make_pair(v.first, v.second - 1);
pair<int, int> p7 = make_pair(v.first - 1, v.second - 1);
pair<int, int> p8 = make_pair(v.first - 1, v.second + 1);
if (binary_search(m[p1.first].begin(), m[p1.first].end(), p1.second) !=
false) {
if (!used[p1]) {
used[p1] = true;
q.push(p1);
d[p1] = d[v] + 1;
}
}
if (binary_search(m[p2.first].begin(), m[p2.first].end(), p2.second) !=
false) {
if (!used[p2]) {
used[p2] = true;
q.push(p2);
d[p2] = d[v] + 1;
}
}
if (binary_search(m[p3.first].begin(), m[p3.first].end(), p3.second) !=
false) {
if (!used[p3]) {
used[p3] = true;
q.push(p3);
d[p3] = d[v] + 1;
}
}
if (binary_search(m[p4.first].begin(), m[p4.first].end(), p4.second) !=
false) {
if (!used[p4]) {
used[p4] = true;
q.push(p4);
d[p4] = d[v] + 1;
}
}
if (binary_search(m[p5.first].begin(), m[p5.first].end(), p5.second) !=
false) {
if (!used[p5]) {
used[p5] = true;
q.push(p5);
d[p5] = d[v] + 1;
}
}
if (binary_search(m[p6.first].begin(), m[p6.first].end(), p6.second) !=
false) {
if (!used[p6]) {
used[p6] = true;
q.push(p6);
d[p6] = d[v] + 1;
}
}
if (binary_search(m[p7.first].begin(), m[p7.first].end(), p7.second) !=
false) {
if (!used[p7]) {
used[p7] = true;
q.push(p7);
d[p7] = d[v] + 1;
}
}
if (binary_search(m[p8.first].begin(), m[p8.first].end(), p8.second) !=
false) {
if (!used[p8]) {
used[p8] = true;
q.push(p8);
d[p8] = d[v] + 1;
}
}
}
}
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int n = 0;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
m[r].push_back(j);
}
sort(m[r].begin(), m[r].end());
}
bfs(x1, y1, x2, y2);
cout << answer;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
cout << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
const char* comma = strchr(names + 1, ',');
cout.write(names, comma - names) << " : " << arg1 << " | ";
__f(comma + 1, args...);
}
int mpow(int base, int exp);
void ipgraph(int n, int m);
void dfs(int u, int par);
const int N = 5000, M = 100005;
vector<long long> g[N];
int a[N];
bool isvalid(pair<long long, long long> A) {
if (A.first < 1 || A.first > 1000000000) return false;
if (A.second < 1 || A.second > 1000000000) return false;
return true;
}
long long dx[] = {1, 0, -1, -1, 1, 0, -1, 1};
long long dy[] = {1, 1, 0, -1, 0, -1, 1, -1};
void solve() {
long long i, j, n, m, k;
long long x, y, x1, y1;
cin >> x >> y >> x1 >> y1;
map<pair<long long, long long>, bool> make_pair;
cin >> n;
for (i = 0; i < n; ++i) {
long long a, b, r;
cin >> r >> a >> b;
for (j = a; j <= b; j++) make_pair[{r, j}] = 1;
}
queue<pair<long long, long long> > q;
q.push({x, y});
map<pair<long long, long long>, long long> res;
res[{x, y}] = 0;
while (!q.empty()) {
pair<long long, long long> temp = q.front();
q.pop();
if (temp.first == x1 && temp.second == y1) {
cout << res[{temp}] << "\n";
return;
}
for (i = 0; i < 8; i++) {
pair<long long, long long> temp2;
temp2.first = temp.first + dx[i];
temp2.second = temp.second + dy[i];
if (isvalid(temp2) && make_pair.find({temp2}) != make_pair.end() &&
res.find({temp2}) == res.end() ||
res[temp2] > res[temp] + 1) {
res[{temp2}] = res[{temp}] + 1;
q.push({temp2});
}
}
}
cout << -1 << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
solve();
}
int mpow(int base, int exp) {
base %= (int)1e9 + 7;
int result = 1;
while (exp > 0) {
if (exp & 1) result = ((long long)result * base) % (int)1e9 + 7;
base = ((long long)base * base) % (int)1e9 + 7;
exp >>= 1;
}
return result;
}
void ipgraph(int n, int m) {
int i, u, v;
while (m--) {
cin >> u >> v;
u--, v--;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
}
void dfs(int u, int par) {
for (int v : g[u]) {
if (v == par) continue;
dfs(v, u);
}
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MAX = 123456789;
const long long inf = 123456789000000000;
const double EPS = 1e-10;
const double PI = 2 * asin(1.0);
const long long mod = 1e9 + 7;
inline int cmp(double x, double y = 0, double tol = EPS) {
return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;
}
map<pair<int, int>, int> mapa;
int menor;
int mx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, my[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
bool inside(int x, int y) {
return ((x >= 1 && x <= 1e9) && (y >= 1 && y <= 1e9));
}
void bfs(int x0, int y0, int x, int y) {
queue<pair<pair<int, int>, int> > fila;
fila.push(make_pair(pair<int, int>(x0, y0), 0));
mapa[make_pair(x0, y0)] = 0;
bool achou = false;
while (!fila.empty()) {
int a = fila.front().first.first;
int b = fila.front().first.second;
int dist = fila.front().second;
fila.pop();
for (int i = 0; i < 8; i++) {
if (inside(a + mx[i], b + my[i])) {
if (mapa.find(make_pair(a + mx[i], b + my[i])) != mapa.end()) {
if (mapa[make_pair(a + mx[i], b + my[i])] == 1) {
if (a + mx[i] == x && b + my[i] == y) {
menor = dist + 1;
achou = true;
break;
}
fila.push(make_pair(make_pair(a + mx[i], b + my[i]), dist + 1));
mapa[make_pair(a + mx[i], b + my[i])] = 0;
}
}
}
}
if (achou) break;
}
}
int main() {
int x0, y0;
scanf("%d%d", &x0, &y0);
int x, y;
scanf("%d%d", &x, &y);
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
int r;
scanf("%d", &r);
int a, b;
scanf("%d%d", &a, &b);
for (int j = a; j < b + 1; ++j) mapa[pair<int, int>(r, j)] = 1;
}
menor = -1;
bfs(x0, y0, x, y);
cout << menor << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const int M = 1000000000;
bool cmp(pair<int, int> p1, pair<int, int> p2) {
return p1.first < p2.first || (p1.first == p2.first && p1.second < p2.second);
}
int main() {
int x0, y0, x1, y1, n, r, a, b, i, j, k;
map<int, vector<pair<int, int> > > m;
map<int, vector<pair<int, int> > >::iterator it;
vector<pair<int, int> > v, *pv;
pair<int, int> tp;
scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
scanf("%d", &n);
for (i = 0; i < n; ++i) {
scanf("%d%d%d", &r, &a, &b);
m[r].push_back(make_pair(a, b));
}
for (it = m.begin(); it != m.end(); ++it) {
v = it->second;
sort(v.begin(), v.end(), cmp);
pv = &(it->second);
pv->clear();
tp = v[0];
for (i = 1; i < v.size(); ++i) {
if (tp.second < v[i].first) {
pv->push_back(tp);
tp = v[i];
} else {
tp.second = max(tp.second, v[i].second);
}
}
pv->push_back(tp);
}
set<pair<int, int> > visited;
queue<pair<int, int> > q;
q.push(make_pair(x0, y0));
vector<pair<int, int> > tv;
int qsize, qi, step = 0, low, high, mid;
bool found = false;
while (!q.empty()) {
qsize = q.size();
++step;
for (qi = 0; qi < qsize; ++qi) {
tp = q.front();
q.pop();
tv.clear();
tv.push_back(make_pair(tp.first + 1, tp.second));
tv.push_back(make_pair(tp.first - 1, tp.second));
tv.push_back(make_pair(tp.first, tp.second + 1));
tv.push_back(make_pair(tp.first, tp.second - 1));
tv.push_back(make_pair(tp.first - 1, tp.second - 1));
tv.push_back(make_pair(tp.first + 1, tp.second + 1));
tv.push_back(make_pair(tp.first - 1, tp.second + 1));
tv.push_back(make_pair(tp.first + 1, tp.second - 1));
for (i = 0; i < tv.size(); ++i) {
tp = tv[i];
if (tp.first == x1 && tp.second == y1) {
found = true;
break;
}
if (tp.first < 1 || tp.first > M || tp.second < 1 || tp.second > M ||
visited.find(tp) != visited.end() || m.find(tp.first) == m.end()) {
continue;
}
visited.insert(tp);
pv = &m[tp.first];
low = 0;
high = pv->size() - 1;
while (low <= high) {
mid = (low + high) / 2;
if ((*pv)[mid].first <= tp.second && (*pv)[mid].second >= tp.second) {
q.push(tp);
break;
} else if ((*pv)[mid].first > tp.second) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
if (found) {
break;
}
}
if (found) {
break;
}
}
printf("%d\n", found ? step : -1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int xs, ys, xf, yf, n, r, a, b;
set<pair<int, int> > se;
map<pair<int, int>, int> ma;
queue<pair<int, int> > q;
vector<pair<int, int> > go;
int main() {
cin >> xs >> ys >> xf >> yf;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
se.insert(make_pair(r, j));
}
}
ma[make_pair(xs, ys)] = 0;
q.push(make_pair(xs, ys));
for (int i = -1; i < 2; i++) {
for (int j = -1; j < 2; j++) {
if ((i == 0) && (j == 0)) continue;
go.push_back(make_pair(i, j));
}
}
while (!q.empty()) {
pair<int, int> v = q.front();
int d = ma[v];
q.pop();
for (int i = 0; i < go.size(); i++) {
v.first += go[i].first;
v.second += go[i].second;
if (!ma.count(v) && (se.find(v) != se.end())) {
ma[v] = d + 1;
q.push(v);
}
v.first -= go[i].first;
v.second -= go[i].second;
}
}
if (ma.count(make_pair(xf, yf))) {
cout << ma[make_pair(xf, yf)] << endl;
} else {
cout << "-1" << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int OO = (int)1e6;
set<pair<int, int> > nods;
map<pair<int, int>, bool> vis;
pair<int, int> fr, sc;
int n, r, a, p, ans, sz;
bool ok = true;
int rx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int ry[] = {1, 0, -1, 1, -1, 1, 0, -1};
bool val(pair<int, int> per) { return nods.find(per) != nods.end(); }
void fun(pair<int, int> pos) {
pair<int, int> cur;
queue<pair<int, int> > q;
ans = 0;
q.push(pos);
vis[pos] = true;
sz = 1;
ok = true;
for (; !q.empty() && ok; sz = q.size(), ans++) {
while (ok && sz--) {
pos = q.front();
q.pop();
for (int i = 0; ok && i < 8; i++) {
cur.first = (pos.first) + rx[i];
cur.second = (pos.second) + ry[i];
if (val(cur) && !vis[cur]) {
q.push(cur);
vis[cur] = true;
if (cur == sc) ok = false;
}
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> fr.first >> fr.second >> sc.first >> sc.second >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> p;
for (int j = a; j <= p; j++) {
nods.insert({r, j});
vis[{r, j}] = false;
}
}
fun(fr);
if (!ok)
cout << ans << endl;
else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while (t--) {
long long x0, y0, x1, y1, i, j, k, x, y, xn, yn;
cin >> x0 >> y0 >> x1 >> y1;
long long n;
cin >> n;
long long a, b, r, g, h;
map<int, vector<pair<int, int>>> v;
for (i = 0; i < n; i++) {
cin >> r >> a >> b;
v[r].push_back({a, b});
}
map<pair<long long, long long>, long long> vis;
queue<pair<long long, long long>> q;
q.push({x0, y0});
vis[{x0, y0}] = 0;
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
if (x == x1 && y == y1) break;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (i == 0 && j == 0) continue;
xn = x + i;
yn = y + j;
bool check = false;
auto app = v.find(xn);
if (app == v.end()) {
continue;
}
auto z = app->second;
for (h = 0; h < z.size(); h++) {
if (z[h].first <= yn && z[h].second >= yn) {
check = true;
break;
}
}
if (check && vis[{xn, yn}] == 0) {
q.push({xn, yn});
vis[{xn, yn}] = vis[{x, y}] + 1;
}
}
}
}
if (vis[{x1, y1}] == 0)
cout << -1;
else
cout << vis[{x1, y1}] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
const double PI =
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
const int inf_int = 1e9 + 5;
const ll inf_ll = 1e18 + 5;
const int NMax = 1e5 + 5;
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int x0, y0, x1, y1, N;
cin >> x0 >> y0 >> x1 >> y1 >> N;
map<pair<int, int>, bool> allowed;
for (int i = 1; i <= N; ++i) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; ++j) {
allowed[{r, j}] = true;
}
}
struct elem {
int x, y, steps;
};
const int dx[8] = {-1, -1, -1, 0, 0, +1, +1, +1};
const int dy[8] = {-1, 0, +1, -1, +1, -1, 0, +1};
queue<elem> Q;
map<pair<int, int>, bool> vis;
vis[{x0, y0}] = true;
Q.push({x0, y0, 0});
while (Q.size()) {
auto e = Q.front();
Q.pop();
if (e.x == x1 && e.y == y1) {
cout << e.steps << '\n';
return 0;
}
for (int k = 0; k < 8; ++k) {
int nx = e.x + dx[k];
int ny = e.y + dy[k];
if (!allowed[{nx, ny}] || vis[{nx, ny}]) {
continue;
}
vis[{nx, ny}] = true;
Q.push({nx, ny, e.steps + 1});
}
}
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[8] = {-1, 1, 0, 1, -1, 0, 1, -1};
map<pair<int, int>, int> vis;
map<pair<int, int>, int> dis;
bool valid(int x, int y) { return (x > 0 && x <= 1e9 && y > 0 && y <= 1e9); }
queue<pair<int, int> > q;
void bfs(int u, int v) {
q.push(make_pair(u, v));
while (!q.empty()) {
pair<int, int> p = q.front();
u = p.first;
v = p.second;
q.pop();
for (int i = 0; i < 8; i++) {
int x = u + dx[i];
int y = v + dy[i];
if (valid(x, y)) {
if (vis[make_pair(x, y)])
vis[make_pair(x, y)] = 0,
dis[make_pair(x, y)] = dis[make_pair(u, v)] + 1,
q.push(make_pair(x, y));
}
}
}
}
int main() {
int x, y, x0, y0;
cin >> x >> y >> x0 >> y0;
int n;
cin >> n;
while (n--) {
int r, a, b;
scanf("%d %d %d", &r, &a, &b);
for (int i = a; i <= b; i++) {
vis[make_pair(r, i)] = 1;
}
}
bfs(x, y);
if (dis[make_pair(x0, y0)])
cout << dis[make_pair(x0, y0)];
else
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> m;
queue<pair<int, int> > q;
const int LAR = 1000000000;
int main() {
int x0, y0, x1, y1;
scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; j++) m[make_pair(r, j)] = LAR;
}
m[make_pair(x0, y0)] = 0;
q.push(make_pair(x0, y0));
while (!q.empty()) {
pair<int, int> u = q.front();
q.pop();
int d = m[u];
if (u == make_pair(x1, y1)) {
printf("%d\n", d);
return 0;
}
for (int d1 = -1; d1 <= 1; d1++)
for (int d2 = -1; d2 <= 1; d2++) {
pair<int, int> v = make_pair(u.first + d1, u.second + d2);
if (m.find(v) != m.end() && d + 1 < m[v]) {
m[v] = d + 1;
q.push(v);
}
}
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> mp;
int xs, ys, xe, ye, n;
int d[8][2] = {-1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1, -1, 1, 1};
int main() {
std::ios::sync_with_stdio(false);
cin >> xs >> ys >> xe >> ye >> n;
mp.clear();
for (int i = 1; i <= n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
mp[make_pair(r, i)] = -1;
}
}
queue<pair<int, int> > q;
while (!q.empty()) q.pop();
q.push(make_pair(xs, ys));
mp[make_pair(xs, ys)] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int nx = x + d[i][0];
int ny = y + d[i][1];
if (mp[make_pair(nx, ny)] == -1) {
q.push(make_pair(nx, ny));
mp[make_pair(nx, ny)] = mp[make_pair(x, y)] + 1;
}
}
}
cout << mp[make_pair(xe, ye)] << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
long long x;
scanf("%lld", &x);
return x;
}
set<pair<int, int>> vis;
set<pair<int, int>> points;
map<pair<int, int>, int> dis;
map<pair<int, int>, vector<pair<int, int>>> adj;
int level = -1;
pair<int, int> start, last;
bool pre(pair<int, int> x) { return points.find(x) != points.end(); }
void sh(pair<int, int> x) {}
void bfs(pair<int, int> u) {
queue<pair<int, int>> q;
q.push(u);
queue<pair<int, int>> temp;
while (!q.empty()) {
pair<int, int> v = q.front();
if (v == last) {
level = dis[v];
return;
}
sh(v);
vis.insert(v);
q.pop();
for (pair<int, int> p : adj[v]) {
if (vis.find(p) == vis.end()) {
q.push(p);
vis.insert(p);
dis[p] = dis[v] + 1;
}
}
}
}
void solve() {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
start = make_pair(x1, y1);
last = make_pair(x2, y2);
dis[start] = 0;
int k;
cin >> k;
while (k--) {
long long r, a, b;
cin >> r >> a >> b;
for (int c = a; c <= b; c++) {
pair<int, int> p = make_pair(r, c);
if (points.find(p) == points.end()) points.insert(p);
}
}
for (pair<int, int> p : points) {
int r = p.first, c = p.second;
if (pre(make_pair(r - 1, c))) adj[p].push_back(make_pair(r - 1, c));
if (pre(make_pair(r + 1, c))) adj[p].push_back(make_pair(r + 1, c));
if (pre(make_pair(r, c - 1))) adj[p].push_back(make_pair(r, c - 1));
if (pre(make_pair(r, c + 1))) adj[p].push_back(make_pair(r, c + 1));
if (pre(make_pair(r - 1, c - 1))) adj[p].push_back(make_pair(r - 1, c - 1));
if (pre(make_pair(r + 1, c + 1))) adj[p].push_back(make_pair(r + 1, c + 1));
if (pre(make_pair(r + 1, c - 1))) adj[p].push_back(make_pair(r + 1, c - 1));
if (pre(make_pair(r - 1, c + 1))) adj[p].push_back(make_pair(r - 1, c + 1));
}
bfs(start);
cout << level << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t = 1;
while (t--) solve();
cout << "\n\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007, M = 3e5 + 7;
long long powe(long long x, long long y) {
x = x % mod;
long long ans = 1;
while (y > 0) {
if (y & 1) {
ans = (1ll * x * ans) % mod;
}
y >>= 1;
x = (1ll * x * x) % mod;
}
return ans;
}
map<int, vector<pair<int, int>>> pos;
map<pair<int, int>, bool> vis;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, -1, -1, -1, 0, 1, 1, 1};
bool valid(int x, int y) {
if (x > 1e9 || x < 1 || y > 1e9 || y < 1) return false;
if (vis[{x, y}]) return false;
if (!pos.count(x)) return false;
for (auto k : pos[x]) {
if (k.first <= y && k.second >= y) {
vis[{x, y}] = true;
return true;
}
}
return false;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int x1, x2, y1, y2, n, a, b, c;
cin >> x1 >> y1 >> x2 >> y2;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a >> b >> c;
pos[a].push_back({b, c});
}
queue<pair<int, int>> q1, q2, emp;
int dis = 1;
q1.push({x1, y1});
while (1) {
if (q1.size() == 0) {
cout << -1;
break;
}
while (q1.size()) {
auto k = q1.front();
q1.pop();
int x = k.first, y = k.second;
for (int i = 0; i < 8; i++) {
if (valid(x + dx[i], y + dy[i])) {
if (x + dx[i] == x2 && y + dy[i] == y2) {
cout << dis;
return 0;
}
q2.push({x + dx[i], y + dy[i]});
}
}
}
dis++;
swap(q1, q2);
q2 = emp;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int X[4], Y[4];
map<pair<int, int>, bool> m_p, visit;
int dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};
int dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int bfs() {
queue<pair<pair<int, int>, int> > q;
q.push(make_pair(make_pair(X[0], Y[0]), 0));
visit[make_pair(X[0], Y[0])] = 1;
while (!q.empty()) {
pair<pair<int, int>, int> tmp = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int x = tmp.first.first + dr8[i];
int y = tmp.first.second + dc8[i];
if (m_p.find(make_pair(x, y)) != m_p.end() && !visit[make_pair(x, y)]) {
visit[make_pair(x, y)] = 1;
q.push(make_pair(make_pair(x, y), tmp.second + 1));
if (x == X[1] && y == Y[1]) return tmp.second + 1;
}
}
}
return -1;
}
int main() {
cin >> X[0] >> Y[0] >> X[1] >> Y[1];
int n;
scanf("%d", &n);
while (n--) {
int r, l, ro;
scanf("%d %d %d", &ro, &l, &r);
for (int i = l; i <= r; i++) m_p[make_pair(ro, i)] = 1;
}
cout << bfs();
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n = 0;
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int m;
cin >> m;
map<pair<int, int>, bool> mass;
for (int i = 0; i < m; i++) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; j++) {
mass[make_pair(r, j)] = true;
}
}
map<pair<int, int>, bool> flag;
queue<pair<int, int> > q;
map<pair<int, int>, int> dist;
dist[make_pair(x0, y0)] = 1;
q.push(make_pair(x0, y0));
flag[make_pair(x0, y0)] = true;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
bool f = false;
q.pop();
if (mass[make_pair(x - 1, y - 1)]) {
if (!flag[make_pair(x - 1, y - 1)]) {
flag[make_pair(x - 1, y - 1)] = true;
q.push(make_pair(x - 1, y - 1));
dist[make_pair(x - 1, y - 1)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x, y - 1)]) {
if (!flag[make_pair(x, y - 1)]) {
flag[make_pair(x, y - 1)] = true;
q.push(make_pair(x, y - 1));
dist[make_pair(x, y - 1)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x + 1, y - 1)]) {
if (!flag[make_pair(x + 1, y - 1)]) {
flag[make_pair(x + 1, y - 1)] = true;
q.push(make_pair(x + 1, y - 1));
dist[make_pair(x + 1, y - 1)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x - 1, y)]) {
if (!flag[make_pair(x - 1, y)]) {
flag[make_pair(x - 1, y)] = true;
q.push(make_pair(x - 1, y));
dist[make_pair(x - 1, y)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x + 1, y)]) {
if (!flag[make_pair(x + 1, y)]) {
flag[make_pair(x + 1, y)] = true;
q.push(make_pair(x + 1, y));
dist[make_pair(x + 1, y)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x - 1, y + 1)]) {
if (!flag[make_pair(x - 1, y + 1)]) {
flag[make_pair(x - 1, y + 1)] = true;
q.push(make_pair(x - 1, y + 1));
dist[make_pair(x - 1, y + 1)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x, y + 1)]) {
if (!flag[make_pair(x, y + 1)]) {
flag[make_pair(x, y + 1)] = true;
q.push(make_pair(x, y + 1));
dist[make_pair(x, y + 1)] = dist[make_pair(x, y)] + 1;
}
}
if (mass[make_pair(x + 1, y + 1)]) {
if (!flag[make_pair(x + 1, y + 1)]) {
flag[make_pair(x + 1, y + 1)] = true;
q.push(make_pair(x + 1, y + 1));
dist[make_pair(x + 1, y + 1)] = dist[make_pair(x, y)] + 1;
}
}
}
int ans = dist[make_pair(x1, y1)];
if (ans) {
cout << ans - 1;
} else {
cout << -1;
}
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
int m;
scanf("%d", &m);
map<pair<int, int>, int> mp;
for (int i = 0; i < m; ++i) {
int x, y1, y2;
scanf("%d %d %d", &x, &y1, &y2);
while (y1 <= y2) {
mp[{x, y1}] = 1000000000;
y1++;
}
}
mp[{a, b}] = 0;
queue<pair<int, int> > q;
q.emplace(a, b);
while (!q.empty()) {
a = q.front().first, b = q.front().second;
q.pop();
for (int i = 0; i < 8; ++i) {
int x = a + dx[i];
int y = b + dy[i];
if (x < 0 or y < 0 or x >= 1000000000 or y >= 1000000000) continue;
if (!mp.count({x, y})) continue;
if (mp[{x, y}] != 1000000000) continue;
mp[{x, y}] = mp[{a, b}] + 1;
q.emplace(x, y);
}
}
if (mp[{c, d}] == 1000000000)
puts("-1");
else
printf("%d\n", mp[{c, d}]);
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> d;
map<pair<int, int>, bool> valid;
int x11, y11, x2, y2;
int n;
const int N = 1e9 + 3;
deque<pair<int, int> > check;
bool av(int x, int y) {
if ((x > 0) && (x < N) && (y > 0) && (y < N)) return true;
return false;
}
void bfs() {
d[make_pair(x11, y11)] = 0;
check.push_back(make_pair(x11, y11));
while (!check.empty()) {
for (int i = -1; i < 2; ++i)
for (int j = -1; j < 2; ++j) {
if ((i == 0) && (j == 0)) continue;
if (av(check[0].first + i, check[0].second + j))
if ((d[make_pair(check[0].first + i, check[0].second + j)] == 0) &&
(valid[make_pair(check[0].first + i, check[0].second + j)])) {
d[make_pair(check[0].first + i, check[0].second + j)] =
d[make_pair(check[0].first, check[0].second)] + 1;
check.push_back(make_pair(check[0].first + i, check[0].second + j));
}
if ((check[0].first + i == x2) && (check[0].second + j == y2)) {
cout << d[make_pair(check[0].first + i, check[0].second + j)];
return;
}
}
check.pop_front();
}
cout << -1;
}
int main() {
cin >> x11 >> y11 >> x2 >> y2 >> n;
int r, a, b;
for (int i = 0; i < n; ++i) {
cin >> r >> a >> b;
for (int j = a; j < b + 1; ++j) valid[make_pair(r, j)] = true;
}
bfs();
}
|
#include <bits/stdc++.h>
using namespace std;
inline long long in() {
long long x;
scanf("%lld", &x);
return x;
}
set<pair<int, int>> vis;
set<pair<int, int>> points;
map<pair<int, int>, vector<pair<int, int>>> adj;
int level = -1;
pair<int, int> start, last;
bool pre(pair<int, int> x) { return points.find(x) != points.end(); }
void bfs(pair<int, int> u) {
vis.insert(u);
if (u == last) {
level = 0;
return;
}
int l = 0;
queue<pair<int, int>> q;
q.push(u);
queue<pair<int, int>> temp;
while (!q.empty()) {
pair<int, int> v = q.front();
if (v == last) {
level = l;
break;
}
q.pop();
for (pair<int, int> p : adj[v]) {
if (vis.find(p) == vis.end()) {
vis.insert(p);
temp.push(p);
}
}
if (q.empty()) {
l++;
swap(temp, q);
}
}
}
void solve() {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
start = make_pair(x1, y1);
last = make_pair(x2, y2);
int k;
cin >> k;
while (k--) {
long long r, a, b;
cin >> r >> a >> b;
for (int c = a; c <= b; c++) {
points.insert(make_pair(r, c));
}
}
for (pair<int, int> p : points) {
int r = p.first, c = p.second;
if (pre(make_pair(r - 1, c))) adj[p].push_back(make_pair(r - 1, c));
if (pre(make_pair(r + 1, c))) adj[p].push_back(make_pair(r + 1, c));
if (pre(make_pair(r, c - 1))) adj[p].push_back(make_pair(r, c - 1));
if (pre(make_pair(r, c + 1))) adj[p].push_back(make_pair(r, c + 1));
if (pre(make_pair(r - 1, c - 1))) adj[p].push_back(make_pair(r - 1, c - 1));
if (pre(make_pair(r + 1, c + 1))) adj[p].push_back(make_pair(r + 1, c + 1));
if (pre(make_pair(r + 1, c - 1))) adj[p].push_back(make_pair(r + 1, c - 1));
if (pre(make_pair(r - 1, c + 1))) adj[p].push_back(make_pair(r - 1, c + 1));
}
bfs(start);
cout << level << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
;
int t = 1;
while (t--) solve();
cout << "\n\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
map<pair<int, int>, int> mov, cost;
int sr, sc, er, ec;
int xp[] = {1, 1, 1, -1, -1, -1, 0, 0};
int yp[] = {-1, 0, 1, -1, 0, 1, 1, -1};
void bfs() {
queue<pair<int, int>> q;
q.push({sr, sc});
cost[{sr, sc}] = 1;
while (q.size()) {
int y = q.front().first;
int x = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int nx = x + xp[i];
int ny = y + yp[i];
if (nx < 1 || ny < 1 || nx > 1e9 || ny > 1e9 || !mov[{ny, nx}] ||
cost[{ny, nx}] != 0)
continue;
cost[{ny, nx}] = cost[{y, x}] + 1;
q.push({ny, nx});
}
}
}
int main() {
cin >> sr >> sc >> er >> ec;
int n, r, cs, ce;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r >> cs >> ce;
for (int j = cs; j <= ce; j++) mov[{r, j}] = 1;
}
bfs();
cout << cost[{er, ec}] - 1 << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int x, y, fx, fy;
queue<pair<int, int> > b;
map<pair<int, int>, int> d;
map<pair<int, int>, bool> q;
map<pair<int, int>, bool> w;
int n;
int main() {
cin >> x >> y >> fx >> fy;
cin >> n;
int tx, ty, tz;
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &tx, &ty, &tz);
for (int j = ty; j <= tz; j++) {
q[make_pair(tx, j)] = true;
d[make_pair(tx, j)] = (1 << 30);
}
}
pair<int, int> tmp = make_pair(x, y);
w[tmp] = true;
b.push(tmp);
d[tmp] = 0;
int dx[] = {-1, 0, 1}, dy[] = {-1, 0, 1}, mm;
while (b.size() > 0) {
tmp = b.front();
b.pop();
tx = tmp.first;
ty = tmp.second;
mm = d[tmp];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) continue;
tmp = make_pair(tx + dx[i], ty + dy[j]);
if (q[tmp] && !w[tmp])
if (d[tmp] > mm + 1) {
w[tmp] = true;
d[tmp] = mm + 1;
b.push(tmp);
}
}
}
tmp = make_pair(fx, fy);
int val = d[tmp];
if (val == (1 << 30))
cout << -1;
else
cout << val;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > allowed;
int bfs(pair<int, int> start, pair<int, int> end) {
queue<pair<int, int> > q;
pair<int, int> cur;
int level = 0;
q.push(start);
allowed.erase(start);
while (q.size()) {
int sz = q.size();
for (auto i = 0; i < (sz); i++) {
cur = q.front();
q.pop();
if (cur == end) {
return level;
}
if (allowed.find({cur.first, cur.second + 1}) != allowed.end()) {
q.push({cur.first, cur.second + 1});
allowed.erase({cur.first, cur.second + 1});
}
if (allowed.find({cur.first, cur.second - 1}) != allowed.end()) {
q.push({cur.first, cur.second - 1});
allowed.erase({cur.first, cur.second - 1});
}
if (allowed.find({cur.first + 1, cur.second + 1}) != allowed.end()) {
q.push({cur.first + 1, cur.second + 1});
allowed.erase({cur.first + 1, cur.second + 1});
}
if (allowed.find({cur.first + 1, cur.second}) != allowed.end()) {
q.push({cur.first + 1, cur.second});
allowed.erase({cur.first + 1, cur.second});
}
if (allowed.find({cur.first + 1, cur.second - 1}) != allowed.end()) {
q.push({cur.first + 1, cur.second - 1});
allowed.erase({cur.first + 1, cur.second - 1});
}
if (allowed.find({cur.first - 1, cur.second + 1}) != allowed.end()) {
q.push({cur.first - 1, cur.second + 1});
allowed.erase({cur.first - 1, cur.second + 1});
}
if (allowed.find({cur.first - 1, cur.second}) != allowed.end()) {
q.push({cur.first - 1, cur.second});
allowed.erase({cur.first - 1, cur.second});
}
if (allowed.find({cur.first - 1, cur.second - 1}) != allowed.end()) {
q.push({cur.first - 1, cur.second - 1});
allowed.erase({cur.first - 1, cur.second - 1});
}
}
level++;
}
return -1;
}
int main() {
int sx, sy, ex, ey;
cin >> sx >> sy >> ex >> ey;
int a0;
cin >> a0;
for (auto i = 0; i < (a0); i++) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
allowed.insert({r, i});
}
}
cout << bfs({sx, sy}, {ex, ey});
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e9;
const int M = 1e9;
int ix, iy, fx, fy;
map<pair<int, int>, int> pres;
map<pair<int, int>, int> dist;
map<pair<int, int>, bool> vis;
int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
bool check(int x, int y) {
if (x >= 1 && x <= 1e9 && y >= 1 && y <= 1e9) {
if (pres[make_pair(x, y)]) return true;
return false;
}
return false;
}
void bfs(int x, int y) {
queue<pair<int, int> > q;
pair<int, int> tmp;
q.push(make_pair(x, y));
while (q.size() > 0) {
tmp = q.front();
q.pop();
for (int i = 0; i < 9; ++i) {
int x1 = tmp.first + dx[i];
int y1 = tmp.second + dy[i];
if (check(x1, y1) && vis[make_pair(x1, y1)] == 0) {
vis[make_pair(x1, y1)] = 1;
dist[make_pair(x1, y1)] = dist[tmp] + 1;
q.push(make_pair(x1, y1));
}
}
}
}
int main() {
cin >> ix >> iy >> fx >> fy;
int Q;
cin >> Q;
int x, y, z;
for (int q = 1; q <= Q; ++q) {
scanf("%d%d%d", &x, &y, &z);
for (int i = y; i <= z; ++i) {
pres[make_pair(x, i)] = 1;
}
}
dist[make_pair(ix, iy)] = 0;
dist[make_pair(fx, fy)] = 1e9;
vis[make_pair(ix, iy)] = 1;
bfs(ix, iy);
int res = dist[make_pair(fx, fy)];
if (res >= 1e9) res = -1;
cout << res << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, sx, sy, ex, ey;
int fx[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int fy[] = {-1, -1, -1, 0, 1, 1, 1, 0};
set<long long> mp, use;
map<long long, int> len;
inline long long getpoint(int x, int y) {
return ((long long)x * 1000000010) + y;
}
int bfs() {
queue<long long> que;
que.push(getpoint(sx, sy));
use.insert(getpoint(sx, sy));
long long en = getpoint(ex, ey);
while (!que.empty()) {
int leng = len[que.front()];
int x = que.front() / 1000000010;
int y = que.front() % 1000000010;
que.pop();
for (int i = 0; i < 8; ++i) {
long long point = getpoint(x + fx[i], y + fy[i]);
if (mp.find(point) == mp.end()) continue;
if (use.find(point) != use.end()) continue;
que.push(point);
use.insert(point);
len[point] = leng + 1;
if (en == point) return leng + 1;
}
}
return -1;
}
int main() {
scanf("%d%d%d%d%d", &sx, &sy, &ex, &ey, &n);
int r, a, b;
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; ++j) mp.insert(getpoint(r, j));
}
printf("%d\n", bfs());
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int x, y, X, Y, n;
long long p = 1000000007ll;
map<long long, int> d;
vector<long long> g;
vector<long long>::iterator it;
int main() {
int r, a, b;
long long inf = p;
scanf("%d %d %d %d\n", &x, &y, &X, &Y);
scanf("%d\n", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d\n", &r, &a, &b);
for (int j = a; j <= b; j++) {
g.push_back(r * 1ll * p + j);
d[r * 1ll * p + j] = inf;
}
}
sort(g.begin(), g.end());
long long w = X * 1ll * p + Y, W[][2] = {{0, 1}, {0, -1}, {1, 0}, {1, -1},
{1, 1}, {-1, 0}, {-1, 1}, {-1, -1}};
d[w] = 0;
priority_queue<pair<int, long long> > q;
it = lower_bound(g.begin(), g.end(), w);
if (*it == w) q.push(make_pair(0, X * p + Y));
while (!q.empty()) {
long long v = q.top().second, cur = -q.top().first;
q.pop();
if (cur > d[v]) continue;
long long A = v / p, B = v % p;
for (int j = 0; j < 8; j++) {
w = (A + W[j][0]) * p + B + W[j][1];
it = lower_bound(g.begin(), g.end(), w);
if (*it == w) {
if (d[v] + 1 < d[w]) {
d[w] = d[v] + 1;
q.push(make_pair(-d[w], w));
}
}
}
}
w = x * 1ll * p + y;
if (!d.count(w) || d[w] == inf) d[w] = -1;
cout << d[w];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}, dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int x1, x2, y1, y2, n, a, b, r;
cin >> x1 >> y1 >> x2 >> y2 >> n;
map<pair<int, int>, int> h;
for (long long i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) h[make_pair(r, j)] = -1;
}
h[make_pair(x1, y1)] = 0;
queue<pair<int, int> > q;
q.push(make_pair(x1, y1));
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
if (x.first == x2 && x.second == y2) break;
for (int i = 0; i < 8; i++) {
pair<int, int> v = make_pair(x.first + dx[i], x.second + dy[i]);
if (h[v] == -1) {
h[v] = h[x] + 1;
q.push(v);
}
}
}
cout << h[make_pair(x2, y2)];
}
|
#include <bits/stdc++.h>
using namespace std;
struct v {
int x, y, s;
} t, e;
pair<int, int> p;
set<pair<int, int> > S;
queue<v> q;
int d[8][2] = {1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1};
int main() {
int i, j, k, x1, y1, x2, y2, n, a, b, r;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n);
while (n--) {
scanf("%d%d%d", &r, &a, &b);
for (i = a; i <= b; i++) S.insert(pair<int, int>(r, i));
}
t.x = x1;
t.y = y1;
t.s = 0;
q.push(t);
S.erase(pair<int, int>(x1, y1));
while (!q.empty()) {
t = q.front();
q.pop();
for (i = 0; i < 8; i++) {
e.x = t.x + d[i][0];
e.y = t.y + d[i][1];
e.s = t.s + 1;
if (e.x == x2 && e.y == y2) {
printf("%d\n", e.s);
return 0;
}
if (S.find(pair<int, int>(e.x, e.y)) != S.end()) {
S.erase(pair<int, int>(e.x, e.y));
e.s = t.s + 1;
q.push(e);
}
}
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
const double eps = 1e-11;
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
using namespace std;
const int maxn = 1000000000;
int sx, sy, ex, ey;
set<pair<int, int> > mySet;
map<pair<int, int>, int> myMap;
bool judge(int x, int y) {
if (x >= 1 && x <= 1000000000 && y >= 1 && y <= 1000000000) return true;
return false;
}
int bfs() {
queue<pair<int, int> > Q;
Q.push(make_pair(sx, sy));
myMap[make_pair(sx, sy)] = 0;
pair<int, int> pre, cur;
while (!Q.empty()) {
pre = Q.front();
Q.pop();
if (pre.first == ex && pre.second == ey) return myMap[pre];
for (int k = 0; k < (8); k++) {
cur.first = pre.first + dx[k];
cur.second = pre.second + dy[k];
if (judge(cur.first, cur.second) && mySet.find(cur) != mySet.end()) {
if (myMap.find(cur) != myMap.end()) continue;
myMap[cur] = myMap[pre] + 1;
Q.push(cur);
}
}
}
return -1;
}
void solve() {
int sol = bfs();
printf("%d\n", sol);
}
int main() {
while (scanf("%d%d%d%d", &sx, &sy, &ex, &ey) == 4) {
int n;
scanf("%d", &n);
for (int i = 0; i < (n); i++) {
int r, x, y;
scanf("%d%d%d", &r, &x, &y);
for (int i = (x); i < (y + 1); i++) {
mySet.insert(make_pair(r, i));
}
}
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
map<pair<int, int>, bool> have;
map<pair<int, int>, bool> vis;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int xo, yo, x1, y1;
cin >> xo >> yo >> x1 >> y1;
pair<int, int> st, en;
st.first = xo;
en.first = x1;
st.second = yo;
en.second = y1;
have[st] = true;
have[en] = true;
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
int r, a, b;
cin >> r >> a >> b;
for (int c = a; c <= b; ++c) {
pair<int, int> n;
n.first = r;
n.second = c;
if (!have[n]) {
have[n] = true;
}
}
}
queue<pair<pair<int, int>, long long> > q;
q.push({st, (long long)0});
while (!q.empty()) {
pair<int, int> u = q.front().first;
long long lev = q.front().second;
q.pop();
if (u.first == en.first && u.second == en.second) {
cout << lev;
return 0;
}
pair<int, int> v;
v.first = u.first + 1;
v.second = u.second;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first;
v.second = u.second + 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first + 1;
v.second = u.second + 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first;
v.second = u.second - 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first - 1;
v.second = u.second;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first - 1;
v.second = u.second - 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first + 1;
v.second = u.second - 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
v.first = u.first - 1;
v.second = u.second + 1;
if (have[v] && !vis[v]) {
vis[v] = true;
q.push({v, lev + 1});
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e9;
int ax, ay, bx, by;
int n;
set<pair<int, int>> s, vis;
struct point {
int x, y, dis;
};
pair<int, int> adj[] = {{0, -1}, {-1, -1}, {-1, 0}, {-1, 1},
{0, 1}, {1, -1}, {1, 0}, {1, 1}};
bool check(int x, int y) {
return x >= 0 and x <= N and y >= 0 and y <= N and
s.find({x, y}) != s.end() and vis.find({x, y}) == vis.end();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << fixed;
cout.precision(10);
;
cin >> ax >> ay >> bx >> by;
cin >> n;
int r, a, b;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) s.insert({r, j});
}
queue<point> q;
q.push(point{ax, ay, 0});
vis.insert({ax, ay});
int ans = -1;
while (!q.empty()) {
point p = q.front();
q.pop();
int cx = p.x, cy = p.y, dis = p.dis;
if (cx == bx and cy == by) {
ans = dis;
break;
}
for (auto i : adj) {
int nx = cx + i.first, ny = cy + i.second;
if (check(nx, ny)) {
q.push(point{nx, ny, dis + 1});
vis.insert({nx, ny});
}
}
}
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int solve() {
int x, y, p, f;
cin >> x >> y >> p >> f;
int n;
cin >> n;
set<pair<int, int>> allowed;
while (n--) {
int r;
cin >> r;
int start, end;
cin >> start >> end;
for (int c = start; c <= end; c++) {
allowed.insert({r, c});
}
}
struct node {
int i, j, steps;
node(int _i, int _j, int _steps) {
i = _i;
j = _j;
steps = _steps;
}
};
set<pair<int, int>> visi;
visi.insert({x, y});
queue<node> q;
q.push(node(x, y, 0));
int dx[] = {1, -1, 0, 0, 1, -1, 1, -1};
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
while (!q.empty()) {
struct node tp = q.front();
q.pop();
int i = tp.i;
int j = tp.j;
int steps = tp.steps;
if (i == p && j == f) return steps;
for (int ind = 0; ind < 8; ind++) {
int newi = i + dx[ind];
int newj = j + dy[ind];
if (allowed.find({newi, newj}) != allowed.end() &&
visi.find({newi, newj}) == visi.end()) {
visi.insert({newi, newj});
q.push(node(newi, newj, steps + 1));
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout << solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6;
const int INF = 1e9;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {-1, 0, 1, 1, -1, -1, 0, 1};
int Px0, Py0, Px1, Py1, n, l, r, a;
map<pair<int, int>, int> table, black;
pair<int, int> q[MAXN];
void bfs(pair<int, int> a) {
int x = a.first, y = a.second;
table[{x, y}] = 0;
int h = 0, t = 0;
q[t++] = a;
while (t - h) {
a = q[h++];
x = a.first, y = a.second;
for (int i = 0; i < 8; i++) {
if (table.count({x + dx[i], y + dy[i]}) == 0 &&
black[{x + dx[i], y + dy[i]}]) {
table[{x + dx[i], y + dy[i]}] = table[{x, y}] + 1;
q[t++] = {x + dx[i], y + dy[i]};
}
}
}
}
int main() {
cin >> Px0 >> Py0 >> Px1 >> Py1 >> n;
for (int i = 0; i < n; i++) {
cin >> a >> l >> r;
for (int j = l; j <= r; j++) {
black[{a, j}] = true;
}
}
bfs({Px0, Py0});
if (table.count({Px1, Py1}) == 0)
return cout << -1 << '\n', 0;
else
return cout << table[{Px1, Py1}] << '\n', 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int DEBUG = 0;
const int maxn = 2e5;
map<pair<int, int>, int> ok;
pair<int, int> q[maxn];
map<pair<int, int>, int> d;
int x0, y123, x1, y179;
int bfs() {
int qb = 0, qe = 1;
d[make_pair(x0, y123)] = 1;
q[qb] = make_pair(x0, y123);
while (qb != qe) {
pair<int, int> v = q[qb++];
if (DEBUG) printf("v=%d %d %d\n", v.first, v.second, d[v]);
int l = d[v];
for (int dx = -1; dx < 2; dx++) {
for (int dy = (int)-1; dy < (int)2; dy++) {
if (ok[make_pair(v.first + dx, v.second + dy)]) {
if (d.count(make_pair(v.first + dx, v.second + dy)) == 0 ||
d[make_pair(v.first + dx, v.second + dy)] > l + 1) {
d[make_pair(v.first + dx, v.second + dy)] = l + 1;
q[qe++] = make_pair(v.first + dx, v.second + dy);
}
}
}
}
}
if (d.count(make_pair(x1, y179)) == 0) return -1;
return d[make_pair(x1, y179)] - 1;
}
int main() {
scanf("%d%d%d%d", &x0, &y123, &x1, &y179);
int n;
int r, a, b;
scanf("%d", &n);
for (int i = 0; i < (int)n; i++) {
scanf("%d%d%d", &r, &a, &b);
for (int i = (int)a; i < (int)b + 1; i++) {
ok[make_pair(r, i)] = 1;
}
}
for (map<pair<int, int>, int>::iterator it = ok.begin(); it != ok.end(); it++)
if (DEBUG) printf("%d %d\n", it->first.first, it->first.second);
printf("%d", bfs());
}
|
#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const long long INF = 1e9;
inline long long pwr(long long base, long long n, long long m) {
long long ans = 1;
while (n > 0) {
if (n % 2 == 1) ans = (ans * base) % m;
base = (base * base) % m;
n /= 2;
}
return ans;
}
const int N = 1e5 + 10;
int a, b, x, y, row, l, r;
int dx[] = {-1, -1, 1, 1, 1, -1, 0, 0};
int dy[] = {-1, 1, -1, 1, 0, 0, -1, 1};
map<pair<int, int>, int> cost;
map<pair<int, int>, int> good;
int bfs() {
deque<pair<int, int> > mv;
mv.push_back(make_pair(a, b));
cost[make_pair(a, b)] = 1;
while (!mv.empty()) {
pair<int, int> temp = mv.front();
mv.pop_front();
int xx = temp.first;
int yy = temp.second;
int c = cost[make_pair(xx, yy)];
if (xx == x && yy == y) return c;
for (int k = 0; k < 8; k++) {
int i1 = xx + dx[k], j1 = yy + dy[k];
long long q = cost[make_pair(i1, j1)];
if (good[make_pair(i1, j1)] && (q == 0 || q > c + 1)) {
cost[make_pair(i1, j1)] = c + 1;
mv.push_back(make_pair(i1, j1));
}
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> a >> b >> x >> y;
cin >> n;
while (n--) {
cin >> row >> l >> r;
for (int col = l; col < r + 1; col++) good[make_pair(row, col)] = 1;
}
cout << bfs() - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
map<long long, bool> map1;
struct node {
long long x, y, val, step;
};
long long getpos(long long x, long long y) { return 1e9 * x + y; }
int main(void) {
cin.tie(0);
std::ios::sync_with_stdio(false);
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
long long n;
cin >> n;
for (long long i = 1; i <= n; i++) {
long long _r, _a, _b;
cin >> _r >> _a >> _b;
for (long long j = _a; j <= _b; j++) {
map1[getpos(_r, j)] = 1;
}
}
queue<node> q;
q.push({x0, y0, getpos(x0, y0), 0});
map1[getpos(x0, y0)] = 1;
while (q.size()) {
node u = q.front();
q.pop();
if (u.x == x1 && u.y == y1) {
cout << u.step << endl;
return 0;
}
for (int i = 0; i < 8; i++) {
long long v1 = u.x + dx[i];
long long v2 = u.y + dy[i];
long long v3 = getpos(v1, v2);
long long v4 = u.step + 1;
if (map1[v3] == 1 && v1 >= 1 && v1 <= 1e9 && v2 >= 1 && v2 <= 1e9) {
q.push({v1, v2, v3, v4});
map1[v3] = 0;
}
}
}
cout << "-1" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
long long power(long long x, long long y) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x;
y = y >> 1;
x = x * x;
}
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
long long n;
cin >> n;
long long r, a, b;
std::map<pair<long long, long long>, long long> dis, m;
for (long long i = 0; i < n; ++i) {
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) m[{r, j}]++;
}
set<pair<long long, pair<long long, long long>>> s;
s.insert({0, {x0, y0}});
dis[{x0, y0}] = 0;
long long xx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
long long yy[8] = {1, 0, 0, -1, 1, -1, 1, -1};
while (!s.empty()) {
pair<long long, pair<long long, long long>> itr = *(s.begin());
long long diss = itr.first;
long long x = itr.second.first;
long long y = itr.second.second;
s.erase(s.begin());
if (x == x1 && y == y1) break;
for (long long i = 0; i < 8; ++i) {
long long xt = x + xx[i];
long long yt = y + yy[i];
if (m.find({xt, yt}) != m.end() && dis.find({xt, yt}) == dis.end()) {
dis[{xt, yt}] = diss + 1;
s.insert({diss + 1, {xt, yt}});
}
}
}
if (dis.find({x1, y1}) == dis.end())
cout << -1 << "\n";
else
cout << dis[{x1, y1}] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
int x, y, a, b;
map<int, vector<pair<int, int>>> m;
map<pair<int, int>, bool> vis;
int solve() {
queue<pair<int, int>> qu;
queue<int> cst;
qu.push({x, y});
cst.push(0);
vis[{x, y}] = 1;
while (!qu.empty()) {
pair<int, int> cur = qu.front();
int cost = cst.front();
qu.pop();
cst.pop();
if (cur == make_pair(a, b)) {
return cost;
}
for (int j = -1; j < 2; j++) {
for (int i = 0; i < m[cur.first + j].size(); i++) {
pair<int, int> c = m[cur.first + j][i];
for (int k = -1; k < 2; k++) {
if (cur.second + k >= c.first && cur.second + k <= c.second) {
if (!vis[{cur.first + j, cur.second + k}]) {
qu.push({cur.first + j, cur.second + k});
cst.push(cost + 1);
vis[{cur.first + j, cur.second + k}] = 1;
}
}
}
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(0);
cin >> x >> y >> a >> b;
cin >> n;
x--, y--, a--, b--;
while (n--) {
int r, i, j;
cin >> r >> i >> j;
r--, i--, j--;
bool ps = true;
for (int k = 0; k < m[r].size(); k++) {
pair<int, int> second = m[r][k];
if (second.first <= i && second.second >= j) {
ps = false;
break;
} else if ((second.first >= i && second.second <= j)) {
m[r][k] = {i, j};
ps = false;
break;
} else if (second.first > i && second.first == j) {
m[r][k] = {i, second.second};
ps = false;
break;
} else if (second.second < j && second.second == i) {
m[r][k] = {second.first, j};
ps = false;
break;
}
}
if (ps) m[r].push_back({i, j});
}
cout << solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> st;
map<pair<int, int>, int> mp;
map<pair<int, int>, vector<pair<int, int>>> g;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int xSt, ySt, xEn, yEn;
int n;
cin >> xSt >> ySt >> xEn >> yEn;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
st.insert({r, j});
}
}
for (auto it : st) {
int x = it.first;
int y = it.second;
if (st.find({x - 1, y - 1}) != st.end()) {
g[it].push_back({x - 1, y - 1});
}
if (st.find({x - 1, y}) != st.end()) {
g[it].push_back({x - 1, y});
}
if (st.find({x - 1, y + 1}) != st.end()) {
g[it].push_back({x - 1, y + 1});
}
if (st.find({x, y - 1}) != st.end()) {
g[it].push_back({x, y - 1});
}
if (st.find({x, y + 1}) != st.end()) {
g[it].push_back({x, y + 1});
}
if (st.find({x + 1, y - 1}) != st.end()) {
g[it].push_back({x + 1, y - 1});
}
if (st.find({x + 1, y}) != st.end()) {
g[it].push_back({x + 1, y});
}
if (st.find({x + 1, y + 1}) != st.end()) {
g[it].push_back({x + 1, y + 1});
}
}
mp[{xSt, ySt}] = 1;
queue<pair<int, int>> q;
q.push({xSt, ySt});
while (q.size() > 0) {
pair<int, int> pr = q.front();
q.pop();
for (auto it : g[pr]) {
if (mp[it] == 0) {
mp[it] = mp[pr] + 1;
q.push(it);
}
}
}
cout << mp[{xEn, yEn}] - 1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using uint = unsigned int;
const double PI =
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862;
const int inf_int = 1e9 + 5;
const ll inf_ll = 1e18 + 5;
const int NMax = 1e5 + 5;
bool valid(map<ll, vector<pair<ll, bool>>>& sm, ll r, ll c) {
const vector<pair<ll, bool>>& v = sm[r];
ll N = v.size();
if (N == 0) {
return false;
}
ll pos = -1;
for (ll e = 30; e >= 0; --e) {
if (pos + (1 << e) < N && v[pos + (1 << e)].first <= c) {
pos += (1 << e);
}
}
if (pos == -1) {
return false;
}
if (c == v[pos].first || v[pos].second) {
return true;
}
return false;
}
bool cmp(const pair<ll, bool>& a, const pair<ll, bool>& b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
ll x0, y0, x1, y1, N;
cin >> x0 >> y0 >> x1 >> y1 >> N;
map<ll, vector<pair<ll, bool>>> fm;
for (ll i = 1; i <= N; ++i) {
ll r, a, b;
cin >> r >> a >> b;
fm[r].push_back({a, true});
fm[r].push_back({b, false});
}
map<ll, vector<pair<ll, bool>>> sm;
for (auto& it : fm) {
ll r = it.first;
vector<pair<ll, bool>>& v = it.second;
sort(v.begin(), v.end(), cmp);
ll start = -1, num = 0;
for (auto p : v) {
if (p.second == true) {
++num;
if (num == 1) {
start = p.first;
}
} else {
--num;
if (num == 0) {
assert(start != -1);
sm[r].push_back({start, true});
sm[r].push_back({p.first, false});
start = -1;
}
}
}
assert(num == 0);
}
struct elem {
ll x, y, steps;
};
const ll dx[8] = {-1, -1, -1, 0, 0, +1, +1, +1};
const ll dy[8] = {-1, 0, +1, -1, +1, -1, 0, +1};
queue<elem> Q;
map<pair<ll, ll>, bool> vis;
vis[{x0, y0}] = true;
Q.push({x0, y0, 0});
while (Q.size()) {
auto e = Q.front();
Q.pop();
if (e.x == x1 && e.y == y1) {
cout << e.steps << '\n';
return 0;
}
for (ll k = 0; k < 8; ++k) {
ll nx = e.x + dx[k];
ll ny = e.y + dy[k];
if (!valid(sm, nx, ny) || vis[{nx, ny}]) {
continue;
}
vis[{nx, ny}] = true;
Q.push({nx, ny, e.steps + 1});
}
}
cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void write(vector<int> &v) {
for (auto i : v) cout << i << " ";
cout << "\n";
}
void read(vector<int> &v) {
for (auto &i : v) cin >> i;
}
const int INF = 1e9;
const int64_t INFF = 1e18;
const int N = 1e6 + 69;
map<pair<int, int>, int> grid;
vector<int> dRow = {0, 1, 0, -1, 1, 1, -1, -1};
vector<int> dCol = {1, 0, -1, 0, 1, -1, -1, 1};
map<pair<int, int>, int> vis;
void solve() {
int x0, x1, y0, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, c1, c2;
cin >> r >> c1 >> c2;
for (int j = c1; j <= c2; j++) {
grid[{r, j}] = 1;
}
}
queue<pair<int, int> > q;
q.push({x0, y0});
vis[{x0, y0}] = 1;
bool exist = false;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int newx = x + dRow[i], newy = y + dCol[i];
if (vis[{newx, newy}] == 0 && grid[{newx, newy}] && 0 <= newx &&
newx < (int)1e9 && 0 <= newy < (int)1e9) {
vis[{newx, newy}] = vis[{x, y}] + 1;
q.push({newx, newy});
}
}
if (exist) break;
}
cout << vis[{x1, y1}] - 1 << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
for (int i = 1; i <= t; i++) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int II;
long long I_O;
char CC, SS[20];
const int N = 1e5 + 4;
inline long long read() {
II = 1, I_O = 0;
while (!isdigit(CC = getchar()))
if (CC == '-') II = -1;
while (isdigit(CC)) I_O = I_O * 10 + CC - '0', CC = getchar();
return I_O * II;
}
inline void wonl() { putchar('\n'); }
inline void wws() {}
inline void dbg() { cout << endl; }
inline void ww(long long k) {
if (k < 0) putchar('-'), k *= -1;
II = 0;
while (k) SS[++II] = k % 10, k /= 10;
if (!II) SS[++II] = 0;
while (II) putchar(SS[II--] + '0');
}
inline void ww(pair<long long, long long> p) {
ww(p.first), putchar(' '), ww(p.second);
}
template <typename T, typename... V>
inline void wonl(T t, V... v) {
ww(t);
if (sizeof...(v)) putchar(' ');
wonl(v...);
}
template <typename T, typename... V>
inline void wws(T t, V... v) {
ww(t);
putchar(' ');
wws(v...);
}
template <typename T, typename... V>
inline void dbg(T t, V... v) {
cout << ' ' << t;
dbg(v...);
}
void solve() {
int x0 = read(), y0 = read();
int x1 = read(), y1 = read();
int n = read();
unordered_map<int, map<int, int>> ma;
for (int i = 0; i < n; i++) {
int r = read(), a = read(), b = read();
ma[r][a] = max(ma[r][a], b);
}
std::function<bool(int, int)> isValid;
isValid = [&](int x, int y) {
if (ma[x].size() == 0) return false;
auto it = ma[x].upper_bound(y);
if (it == ma[x].begin()) return false;
it--;
return y <= it->second;
};
int dx[8] = {-1, +1, 0, 0, -1, -1, 1, 1};
int dy[8] = {0, 0, +1, -1, -1, 1, -1, 1};
map<int, map<int, int>> dist;
queue<pair<int, int>> q;
q.push({x0, y0});
dist[x0][y0] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int dir = 0; dir < 8; dir++) {
int nx = x + dx[dir];
int ny = y + dy[dir];
if (isValid(nx, ny) && dist[nx].find(ny) == dist[nx].end()) {
dist[nx][ny] = dist[x][y] + 1;
q.push({nx, ny});
}
}
}
if (dist[x1].find(y1) == dist[x1].end())
wonl(-1);
else
wonl(dist[x1][y1]);
}
int main() {
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000 + 10;
const int INF = 0x7f7f7f7f;
const int MOD = 1000000007;
const double eps = 1e-10;
const double pi = acos(-1.0);
inline int compareTo(double a, double b) {
return (a > b + eps) ? 1 : ((a + eps < b) ? -1 : 0);
}
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, -1, 1, 1, -1};
int main() {
int n, x[2], y[2], r, a, b;
set<pair<int, int> > allow;
ios::sync_with_stdio(0);
for (int i = 0; i < 2; i++) cin >> x[i] >> y[i];
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) allow.insert(pair<int, int>(r, j));
}
queue<pair<int, int> > que;
map<pair<int, int>, int> dist;
que.push(pair<int, int>(x[0], y[0]));
dist[pair<int, int>(x[0], y[0])] = 0;
while (!que.empty()) {
pair<int, int> t = que.front();
que.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> it(t.first + dx[i], t.second + dy[i]);
if (allow.find(it) != allow.end() && dist.find(it) == dist.end()) {
que.push(it);
dist[it] = dist[t] + 1;
}
}
}
if (dist.find(pair<int, int>(x[1], y[1])) == dist.end())
printf("-1\n");
else
printf("%d\n", dist[pair<int, int>(x[1], y[1])]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long int n, m, i, j, k, t, tc, a, b, c, d, x, y, q, l, r, x0, y0, x1, y1;
tc = 1;
t = 1;
int xr[] = {1, -1, 0};
int yr[] = {1, -1, 0};
while (tc <= t) {
map<pair<long long int, long long int>, long long int> umap;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
umap[make_pair(x0, y0)] = 0;
umap[make_pair(x1, y1)] = 0;
for (k = 0; k < n; k++) {
cin >> r >> a >> b;
for (i = a; i <= b; i++) {
umap[make_pair(r, i)] = 0;
}
}
queue<pair<long long int, long long int>> q;
q.push(make_pair(x0, y0));
while (!q.empty()) {
auto u = q.front();
x = u.first;
y = u.second;
d = umap[u];
q.pop();
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (!(xr[i] == 0 && yr[j] == 0) &&
umap.find(make_pair(x + xr[i], y + yr[j])) != umap.end() &&
umap[make_pair(x + xr[i], y + yr[j])] == 0) {
umap[make_pair(x + xr[i], y + yr[j])] = d + 1;
q.push(make_pair(x + xr[i], y + yr[j]));
}
}
}
}
if (umap[make_pair(x1, y1)] == 0)
cout << "-1\n";
else
cout << umap[make_pair(x1, y1)] << "\n";
tc++;
}
}
|
#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
if (b == 0) return 1;
long long int p = power(a, b / 2);
if (b % 2 == 0)
return p * p;
else
return a * p * p;
}
long int gcd(long int a, long int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
pair<long int, long int> p, s, t;
map<pair<long int, long int>, long int> allow, vis, ans;
void bfs(long int a, long int b) {
queue<pair<long int, long int> > q;
p = make_pair(a, b);
vis[p] = 1;
q.push(p);
ans[p] = 1;
while (q.size()) {
p = q.front();
q.pop();
if (p.first < 1000000000 && p.second < 1000000000) {
s = make_pair((p.first) + 1, (p.second) + 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.first < 1000000000 && p.second > 1) {
s = make_pair((p.first) + 1, (p.second) - 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.first < 1000000000) {
s = make_pair((p.first) + 1, (p.second));
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.second < 1000000000) {
s = make_pair((p.first), (p.second) + 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.second > 1) {
s = make_pair((p.first), (p.second) - 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.first > 1 && p.second > 1) {
s = make_pair((p.first) - 1, (p.second) - 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.first > 1) {
s = make_pair((p.first) - 1, (p.second));
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
if (p.first > 1 && p.second < 1000000000) {
s = make_pair((p.first) - 1, (p.second) + 1);
if (!vis.count(s) && allow.count(s)) {
q.push(s);
vis[s] = 1;
ans[s] = ans[p] + 1;
if (s == t) break;
}
}
}
}
int main() {
std::ios_base::sync_with_stdio(false);
long int x0, y0, x1, y1, n, i, j, r, a, b;
cin >> x0 >> y0 >> x1 >> y1 >> n;
for (i = 0; i < n; i++) {
cin >> r >> a >> b;
for (j = a; j <= b; j++) {
p = make_pair(r, j);
allow[p] = 1;
}
}
t = make_pair(x1, y1);
bfs(x0, y0);
if (!ans.count(t))
cout << "-1";
else
cout << ans[t] - 1;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int dx[10] = {0, 0, 1, 1, -1, -1, 1, -1};
int dy[10] = {1, -1, 1, -1, 1, -1, 0, 0};
struct point {
int x, y;
bool operator<(const point &p) const {
if (x == p.x) return y < p.y;
return x < p.x;
}
point(int xx = 0, int yy = 0) {
x = xx;
y = yy;
}
};
map<point, int> vk;
map<point, int> vis;
struct node {
int x, y;
int step;
} st;
int x0, shduiqy, x1, sdjqwie, n, ans;
void bfs() {
queue<node> que;
point gk;
gk.x = x0, gk.y = shduiqy;
vis[gk] = 1;
st.x = x0, st.y = shduiqy;
st.step = 0;
que.push(st);
ans = -1;
while (!que.empty()) {
node k = que.front();
que.pop();
if (k.x == x1 && k.y == sdjqwie) {
ans = k.step;
break;
}
for (int i = 0; i < 8; i++) {
int xx = k.x + dx[i];
int yy = k.y + dy[i];
gk.x = xx, gk.y = yy;
if (vk[gk] && vis[gk] == 0) {
node kk;
kk.x = xx, kk.y = yy;
kk.step = k.step + 1;
vis[gk] = 1;
que.push(kk);
}
}
}
printf("%d\n", ans);
}
int main() {
scanf("%d%d%d%d", &x0, &shduiqy, &x1, &sdjqwie);
scanf("%d", &n);
int g, l, r;
for (int i = 1; i <= n; i++) {
point gk;
scanf("%d%d%d", &g, &l, &r);
for (int i = l; i <= r; i++) vk[point(g, i)] = 1;
}
bfs();
}
|
#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
long long power(long long a, long long b) {
long long res = 1;
a = a % 998244353;
while (b > 0) {
if (b & 1) {
res = (res * a) % 998244353;
}
a = (a * a) % 998244353;
b >>= 1;
}
return res;
}
long long fermat_inv(long long y) { return power(y, 998244353 - 2); }
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
const long long dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const long long dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
bool valid(long long x, long long y) {
if (x < 1 || x > (long long)1e9 || y < 1 || y > (long long)1e9) return false;
return true;
}
void solve() {
long long x, y, x1, y1;
cin >> x >> y >> x1 >> y1;
long long qu;
cin >> qu;
map<pair<long long, long long>, long long> m;
while (qu--) {
long long row, l, r;
cin >> row >> l >> r;
for (long long i = l; i <= r; i++) m[{row, i}] = 1;
}
queue<pair<long long, long long> > q;
q.push({x, y});
map<pair<long long, long long>, long long> dep, vis;
dep[{x, y}] = 0;
vis[{x, y}] = 1;
while (!q.empty()) {
long long xx = q.front().first;
long long yy = q.front().second;
q.pop();
if (xx == x1 && yy == y1) {
cout << dep[{xx, yy}];
return;
}
for (long long i = 0; i < 8; i++) {
long long xn = xx + dx[i];
long long yn = yy + dy[i];
if (valid(xn, yn) && m[{xn, yn}] == 1 && vis[{xn, yn}] == 0) {
vis[{xn, yn}] = 1;
q.push({xn, yn});
dep[{xn, yn}] = dep[{xx, yy}] + 1;
}
}
}
cout << "-1";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long T = 1;
while (T--) {
solve();
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100009;
const int INF = 1000000;
int dir_x[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dir_y[] = {-1, -1, -1, 0, 1, 1, 1, 0};
map<pair<int, int>, int> NID;
int dist[MAXN], vis[MAXN];
vector<int> grafo[MAXN];
void BFS() {
int lim = NID.size();
for (int i = 1; i <= lim; ++i) {
vis[i] = 0;
dist[i] = INF;
}
dist[1] = 0;
vis[1] = 1;
queue<int> Q;
Q.push(1);
while (!Q.empty()) {
int u = Q.front();
Q.pop();
for (int i = 0; i < grafo[u].size(); ++i) {
int v = grafo[u][i];
if (vis[v]) continue;
vis[v] = 1;
dist[v] = dist[u] + 1;
Q.push(v);
}
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
pair<int, int> ori, dest;
int N;
cin >> ori.first >> ori.second >> dest.first >> dest.second;
NID[ori] = 1;
NID[dest] = 2;
int cnt = 3;
int M;
cin >> M;
for (int i = 0; i < M; ++i) {
int ri, ai, bi;
cin >> ri >> ai >> bi;
for (int j = ai; j <= bi; ++j) {
pair<int, int> pos = pair<int, int>(ri, j);
if (NID.find(pos) == NID.end()) NID[pos] = cnt++;
}
}
for (const auto& it : NID) {
int cx = it.first.first;
int cy = it.first.second;
int u = it.second;
for (int i = 0; i < 8; ++i) {
pair<int, int> puv = pair<int, int>(cx + dir_x[i], cy + dir_y[i]);
if (NID.find(puv) == NID.end()) continue;
grafo[u].push_back(NID[puv]);
grafo[NID[puv]].push_back(u);
}
}
BFS();
cout << ((dist[2] != INF) ? (dist[2]) : (-1)) << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 100 * 1000 + 5;
int xs, ys, xt, yt, n, a[N], b[N], r[N];
map<pair<int, int>, int> mark;
map<pair<int, int>, int> dist;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
void bfs(int x, int y) {
queue<pair<int, int> > q;
dist[{x, y}] = 0;
q.push({x, y});
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
x = p.first, y = p.second;
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (!mark[{nx, ny}])
continue;
else if (dist[{nx, ny}] > dist[{x, y}] + 1) {
dist[{nx, ny}] = dist[{x, y}] + 1;
q.push({nx, ny});
}
}
}
}
void input() {
cin >> xs >> ys >> xt >> yt >> n;
for (int i = 0; i < n; i++) cin >> r[i] >> a[i] >> b[i];
}
void solve() {
for (int i = 0; i < n; i++) {
for (int j = a[i]; j <= b[i]; j++) {
mark[{r[i], j}] = true;
dist[{r[i], j}] = 1e9;
}
}
bfs(xs, ys);
}
void output() {
int ans = dist[{xt, yt}];
if (ans == 1e9)
cout << -1 << "\n";
else
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
input();
solve();
output();
}
|
#include <bits/stdc++.h>
using namespace std;
template <class T>
using min_heap = priority_queue<T, std::vector<T>, std::greater<T>>;
struct hash_pair {
template <class T1, class T2>
size_t operator()(const pair<T1, T2>& p) const {
auto hash1 = hash<T1>{}(p.first);
auto hash2 = hash<T2>{}(p.second);
return hash1 ^ hash2;
}
};
const int fx[] = {+0, +0, +1, -1, -1, +1, -1, +1};
const int fy[] = {-1, +1, +0, +0, +1, +1, -1, -1};
map<pair<long long, long long>, long long> mp;
long long srcx, srcy, desx, desy;
map<pair<long long, long long>, long long> cost;
long long ans = 1e18;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long i, j, k, n, m, T;
cin >> srcx >> srcy >> desx >> desy;
cin >> n;
for (i = 0; i < n; i++) {
long long r, x, y;
cin >> r >> x >> y;
for (j = x; j <= y; j++) {
mp[{r, j}] = 1;
cost[{r, j}] = 1e18;
}
}
queue<pair<long long, long long>> q;
q.push({srcx, srcy});
cost[{srcx, srcy}] = 0;
while (!q.empty()) {
auto cn = q.front();
q.pop();
for (k = 0; k < 8; k++) {
long long x = cn.first + fx[k];
long long y = cn.second + fy[k];
if (!mp.count({x, y})) continue;
if (cost[{x, y}] > 1 + cost[cn]) {
cost[{x, y}] = 1 + cost[cn];
q.push({x, y});
}
}
}
if (cost[{desx, desy}] == 1e18)
cout << -1 << "\n";
else
cout << cost[{desx, desy}] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const ll LINF = (ll)(1e18) + (ll)(1e9);
ll start_x, start_y;
ll finish_x, finish_y;
ll cntN;
int dx[] = {1, -1, -1, 1, 0, 0, -1, 1};
int dy[] = {1, 1, -1, -1, 1, -1, 0, 0};
map<pair<ll, ll>, ll> dpi;
bool get_norm_position(ll x, ll y) {
return (1 <= x && x <= 1e9 && 1 <= y && y <= 1e9 && dpi[{x, y}]);
}
ll bfs() {
map<pair<ll, ll>, ll> minlen;
minlen[{start_x, start_y}] = 1;
queue<pair<ll, ll>> que;
que.push({start_x, start_y});
while (!que.empty()) {
pair<ll, ll> top = que.front();
que.pop();
ll ui = top.first;
ll uj = top.second;
for (int k = 0; k < 8; ++k) {
ll ito = ui + dx[k];
ll jto = uj + dy[k];
if (get_norm_position(ito, jto)) {
if (minlen[{ito, jto}] == 0 ||
minlen[{ito, jto}] > minlen[{ui, uj}] + 1) {
minlen[{ito, jto}] = minlen[{ui, uj}] + 1;
que.push({ito, jto});
}
}
}
}
if (minlen[{finish_x, finish_y}] == 0)
return -1;
else
return minlen[{finish_x, finish_y}] - 1;
}
int main() {
scanf("%lld%lld%lld%lld%lld", &start_x, &start_y, &finish_x, &finish_y,
&cntN);
while (cntN--) {
ll r, a, b;
scanf("%lld%lld%lld", &r, &a, &b);
for (ll j = a; j <= b; ++j) dpi[{r, j}] = 1;
}
cout << bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, -1, 1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
set<pair<int, int> > allowed;
for (int i = 0; i < n; i++) {
int a, b, r;
cin >> r >> a >> b;
for (int x = a; x <= b; x++) {
allowed.insert(pair<int, int>(r, x));
}
}
queue<int> q;
q.push(x0);
q.push(y0);
q.push(0);
set<pair<int, int> > seen;
seen.insert(pair<int, int>(x0, y0));
while (q.size()) {
int x = q.front();
q.pop();
int y = q.front();
q.pop();
int d = q.front();
q.pop();
if (x == x1 && y == y1) {
cout << d << '\n';
return 0;
}
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
pair<int, int> c = pair<int, int>(nx, ny);
if (!seen.count(c) && allowed.count(c)) {
seen.insert(c);
q.push(nx);
q.push(ny);
q.push(d + 1);
}
}
}
cout << -1 << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
long long int Mod = 100000000;
map<pair<long long int, long long int>, int> hmp;
queue<pair<long long int, long long int> > Q;
map<pair<long long int, long long int>, long long int> visited;
int main() {
long long int i, j, k, l, m, n, x, y, z, a, b, r, t, x0, y0, x1, y1,
a1 = 0, a2 = 0, a3 = 0;
scanf("%lld", &x0);
scanf("%lld", &y0);
scanf("%lld", &x1);
scanf("%lld", &y1);
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &r);
scanf("%lld", &a);
scanf("%lld", &b);
for (j = a; j <= b; j++) {
hmp[make_pair(r, j)] = 1;
}
}
Q.push(make_pair(x0, y0));
visited[make_pair(x0, y0)] = 1;
long long int ans = 0;
while (!Q.empty()) {
pair<long long int, long long int> p = Q.front();
Q.pop();
a3 = 0;
for (i = -1; i < 2; i++) {
for (j = -1; j < 2; j++) {
x = p.first + i;
y = p.second + j;
if (hmp[make_pair(x, y)] == 1) {
if (!visited[make_pair(x, y)]) {
visited[make_pair(x, y)] = visited[p] + 1;
Q.push(make_pair(x, y));
}
}
if (hmp[make_pair(x, y)] != 1) {
continue;
}
}
}
}
if (!visited[make_pair(x1, y1)]) {
printf("-1\n");
return 0;
}
printf("%lld\n", visited[make_pair(x1, y1)] - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void solve() {
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
map<pair<long long, long long>, long long> dist;
map<pair<long long, long long>, bool> ok;
long long m;
cin >> m;
while (m--) {
long long r, a, b;
cin >> r >> a >> b;
for (long long i = a; i <= b; i++) ok[{r, i}] = true;
}
queue<pair<long long, long long>> q;
q.push({x0, y0});
dist[{x0, y0}] = 0;
map<pair<long long, long long>, bool> vis;
vis[{x0, y0}] = true;
while (!q.empty()) {
long long x = q.front().first;
long long y = q.front().second;
q.pop();
for (long long j = -1; j <= 1; j++) {
for (long long i = -1; i <= 1; i++) {
if (ok[{x + i, y + j}] && !vis[{x + i, y + j}]) {
dist[{x + i, y + j}] = dist[{x, y}] + 1;
q.push({x + i, y + j});
vis[{x + i, y + j}] = true;
ok[{x + i, y + j}] = false;
}
}
}
}
if (!dist[{x1, y1}]) {
cout << -1;
return;
}
cout << dist[{x1, y1}];
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
;
long long t = 1;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
using namespace std;
int alowed[3] = {0, 1, -1};
int ans = 0;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
map<pair<int, int>, int> s;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int m;
cin >> m;
int r, a, b;
while (m--) {
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
s[make_pair(r, i)]++;
}
}
queue<pair<int, int>> bfs;
bfs.push(make_pair(x1, y1));
while (!bfs.empty()) {
int x = bfs.size();
while (x--) {
pair<int, int> cor = bfs.front();
bfs.pop();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 0 && j == 0) continue;
int xH = cor.first + alowed[i];
int yH = cor.second + alowed[j];
pair<int, int> s2 = make_pair(xH, yH);
if (s.count(s2)) {
s.erase(s2);
bfs.push(s2);
if (xH == x2 && yH == y2) return cout << ans + 1, 0;
}
}
}
}
ans++;
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
struct Edge {
int src, dst;
int weight;
Edge(int src_, int dst_, int weight_)
: src(src_), dst(dst_), weight(weight_) {}
Edge(int dst_, int weight_) : src(-2), dst(dst_), weight(weight_) {}
};
bool operator<(const Edge &e, const Edge &f) {
return e.weight != f.weight ? e.weight > f.weight : e.dst < f.dst;
}
void dijkstra(const vector<vector<Edge> > &g, int start, vector<int> &dist) {
int n = g.size();
dist.assign(n, INF);
dist[start] = 0;
vector<int> prev(n, -1);
priority_queue<Edge> q;
for (q.push(Edge(-2, start, 0)); !q.empty();) {
Edge e = q.top();
q.pop();
if (prev[e.dst] != -1) continue;
prev[e.dst] = e.src;
for (typeof((g[e.dst]).begin()) f = ((g[e.dst]).begin());
f != (g[e.dst]).end(); ++f) {
if (dist[f->dst] > e.weight + f->weight) {
dist[f->dst] = e.weight + f->weight;
q.push(Edge(f->src, f->dst, e.weight + f->weight));
}
}
}
}
int main() {
int n;
int x0, y0, x1, y1;
vector<pair<int, pair<int, int> > > v;
cin >> y0 >> x0 >> y1 >> x1;
cin >> n;
for (int(i) = 0; (i) < (int)(n); ++(i)) {
int o, a, b;
cin >> o >> a >> b;
v.push_back(make_pair((o), (make_pair((a), (b)))));
}
sort((v).begin(), (v).end());
map<pair<int, int>, int> m;
int t = 0;
static const int dy[4] = {0, -1, -1, -1}, dx[4] = {-1, 0, -1, 1};
vector<vector<Edge> > g;
for (typeof((v).begin()) i = ((v).begin()); i != (v).end(); ++i) {
for (int(j) = (int)(i->second.first); (j) <= (int)(i->second.second);
++(j)) {
if (m.count(make_pair((i->first), (j)))) continue;
int q = m[make_pair((i->first), (j))] = t++;
g.push_back(vector<Edge>());
for (int(d) = 0; (d) < (int)(4); ++(d))
if (m.count(make_pair((i->first + dy[d]), (j + dx[d])))) {
g[q].push_back(
Edge(q, m[make_pair((i->first + dy[d]), (j + dx[d]))], 1));
}
}
}
vector<vector<Edge> > gg(t);
for (int(i) = 0; (i) < (int)(t); ++(i))
for (typeof((g[i]).begin()) j = ((g[i]).begin()); j != (g[i]).end(); ++j) {
gg[j->src].push_back(Edge(j->src, j->dst, 1));
gg[j->dst].push_back(Edge(j->dst, j->src, 1));
}
int r = -1;
if (!m.count(make_pair((y0), (x0))))
r = -999;
else if (!m.count(make_pair((y1), (x1))))
r = -999;
else {
vector<int> d;
dijkstra(gg, m[make_pair((y0), (x0))], d);
r = d[m[make_pair((y1), (x1))]];
if (r == INF) r = -1;
}
cout << r << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void oj() {}
static long long dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
static long long dy[] = {0, 1, -1, 1, -1, 0, 1, -1};
long long solve() {
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
long long n;
cin >> n;
long long r, a, b;
map<pair<long long, long long>, long long> vis, mp;
for (long long i = 0; i < n; i++) {
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) {
mp[{r, j}]++;
}
}
queue<pair<pair<long long, long long>, long long>> q;
q.push({{x0, y0}, 0});
vis[{x0, y0}]++;
while (!q.empty()) {
auto it = q.front();
q.pop();
long long x = it.first.first;
long long y = it.first.second;
long long steps = it.second;
for (long long i = 0; i < 8; i++) {
long long newi = x + dx[i];
long long newj = y + dy[i];
if (vis.find({newi, newj}) == vis.end() &&
mp.find({newi, newj}) != mp.end()) {
q.push({{newi, newj}, steps + 1});
vis[{newi, newj}]++;
if (newi == x1 && newj == y1) return (steps + 1);
}
}
}
return -1;
}
int32_t main() {
oj();
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long t = 1;
while (t--) {
cout << solve() << "\n";
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = numeric_limits<int>::max();
const long long LLINF = numeric_limits<long long>::max();
const unsigned long long ULLINF = numeric_limits<unsigned long long>::max();
const double PI = acos(-1.0);
map<int, set<int>> good;
map<int, map<int, int>> used;
int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[] = {1, -1, 0, 1, -1, 0, 1, -1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) good[r].insert(j);
}
queue<pair<int, int>> q;
q.push(make_pair(x0, y0));
used[x0][y0] = 1;
while (!q.empty()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (good[nx].find(ny) != good[nx].end() && !used[nx][ny]) {
used[nx][ny] = used[x][y] + 1;
q.push(make_pair(nx, ny));
}
}
}
cout << used[x1][y1] - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N |= (1LL << pos); }
int Reset(int N, int pos) { return N &= ~(1LL << pos); }
bool Check(int N, int pos) { return N & (1LL << pos); }
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1};
inline void cn(long &n) {
n = 0;
long ch = getchar();
int sign = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') sign = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
n = (n << 3) + (n << 1) + ch - '0', ch = getchar();
n = n * sign;
}
template <class T>
void cmin(T &a, T b) {
if (b < a) a = b;
}
template <class T>
void cmax(T &a, T b) {
if (b > a) a = b;
}
template <class T>
int len(const T &c) {
return (int)c.size();
}
template <class T>
int len(char c[]) {
return (int)strlen(c);
}
string itos(long n) {
string s;
while (n) {
s += (n % 10 + 48);
n /= 10;
}
reverse(s.begin(), s.end());
return s;
}
long stoi(string s) {
long n = 0;
for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48);
return n;
}
int main() {
long x, y, xx, yy, n, r, a, b;
map<pair<int, int>, int> m;
cin >> x >> y >> xx >> yy;
cin >> n;
for (int i(0), _n(n); i < _n; ++i) {
cin >> r >> a >> b;
for (int j(a), _b(b); j <= _b; ++j) {
m[make_pair(r, j)] = -1;
}
}
pair<int, int> st, end, temp;
st = make_pair(x, y);
end = make_pair(xx, yy);
queue<pair<int, int> > q;
q.push(st);
m[st] = 1;
while (!q.empty()) {
temp = q.front();
q.pop();
x = temp.first;
y = temp.second;
for (int i(0), _n(8); i < _n; ++i) {
if (m[make_pair(x + dx[i], y + dy[i])] < 0) {
q.push(make_pair(x + dx[i], y + dy[i]));
m[make_pair(x + dx[i], y + dy[i])] = m[make_pair(x, y)] + 1;
}
}
}
if (m[end] == -1) m[end] = 0;
cout << m[end] - 1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
int row;
int col;
bool operator==(const node& p) const {
return (row == p.row && col == p.col);
}
bool operator<(const node& p) const { return (row < p.row && col < p.col); }
};
map<pair<int, int>, int> allow;
map<pair<int, int>, int> level;
map<pair<int, int>, int> visit;
int frow[8] = {1, -1, 1, -1, 1, -1, 0, 0};
int fcol[8] = {0, 0, 1, 1, -1, -1, 1, -1};
void bfs(pair<int, int> source) {
queue<pair<int, int> > q;
pair<int, int> top, novo;
int i;
q.push(source);
visit[source] = 1;
level[source] = 1;
while (!q.empty()) {
top = q.front();
q.pop();
for (i = 0; i < 8; i++) {
novo.first = top.first + frow[i];
novo.second = top.second + fcol[i];
if (visit[novo] == 1 || allow[novo] == 0) continue;
if (novo.first > 0 && novo.first <= 1000000000 && novo.second > 0 &&
novo.second <= 1000000000) {
visit[novo] = 1;
level[novo] = level[top] + 1;
q.push(novo);
}
}
}
}
int main() {
allow.clear();
level.clear();
visit.clear();
pair<int, int> source;
pair<int, int> dest;
int n, i, j, a, b;
scanf("%d%d%d%d", &source.first, &source.second, &dest.first, &dest.second);
scanf("%d", &n);
for (i = 0; i < n; i++) {
pair<int, int> g;
scanf("%d%d%d", &g.first, &a, &b);
for (j = a; j <= b; j++) {
g.second = j;
allow[g] = 1;
}
}
bfs(source);
printf("%d", level[dest] - 1);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> mp;
map<pair<int, int>, int> depth;
void bfs(int x0, int y0, int x1, int y1) {
queue<pair<int, int> > q;
q.push({x0, y0});
depth[{x0, y0}] = 0;
while (!q.empty()) {
auto fr = q.front();
q.pop();
if (mp[make_pair(fr.first + 1, fr.second)] == true &&
depth[make_pair(fr.first + 1, fr.second)] == -1) {
depth[make_pair(fr.first + 1, fr.second)] = depth[fr] + 1;
q.push(make_pair(fr.first + 1, fr.second));
}
if (mp[{fr.first, fr.second + 1}] == 1 &&
depth[{fr.first, 1 + fr.second}] == -1) {
depth[{fr.first, fr.second + 1}] = depth[fr] + 1;
q.push({fr.first, fr.second + 1});
}
if (mp[{fr.first - 1, fr.second}] == 1 &&
depth[{fr.first - 1, fr.second}] == -1) {
depth[{fr.first - 1, fr.second}] = depth[fr] + 1;
q.push({fr.first - 1, fr.second});
}
if (mp[{fr.first, fr.second - 1}] == 1 &&
depth[{fr.first, fr.second - 1}] == -1) {
depth[{fr.first, fr.second - 1}] = depth[fr] + 1;
q.push({fr.first, fr.second - 1});
}
if (mp[{fr.first + 1, fr.second + 1}] == 1 &&
depth[{fr.first + 1, fr.second + 1}] == -1) {
depth[{fr.first + 1, fr.second + 1}] = depth[fr] + 1;
q.push({fr.first + 1, fr.second + 1});
}
if (mp[{fr.first + 1, fr.second - 1}] == 1 &&
depth[{fr.first + 1, fr.second - 1}] == -1) {
depth[{fr.first + 1, fr.second - 1}] = depth[fr] + 1;
q.push({fr.first + 1, fr.second - 1});
}
if (mp[{fr.first - 1, fr.second + 1}] == 1 &&
depth[{fr.first - 1, fr.second + 1}] == -1) {
depth[{fr.first - 1, fr.second + 1}] = depth[fr] + 1;
q.push({fr.first - 1, fr.second + 1});
}
if (mp[{fr.first - 1, fr.second - 1}] == 1 &&
depth[{fr.first - 1, fr.second - 1}] == -1) {
depth[{fr.first - 1, fr.second - 1}] = depth[fr] + 1;
q.push({fr.first - 1, fr.second - 1});
}
}
}
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
int r, c1, c2;
while (n--) {
cin >> r >> c1 >> c2;
for (int i = c1; i <= c2; i++) {
mp[{r, i}] = true;
depth[{r, i}] = -1;
}
}
bfs(x0, y0, x1, y1);
cout << depth[{x1, y1}];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<int, map<int, bool> > ok;
int n, a, b, r;
int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[] = {1, -1, 1, 0, -1, 1, 0, -1};
bool vaild(int x, int y, int x1, int y1) {
if (x > 0 && y > 0 && x <= 1e9 && y <= 1e9 && (x != x1 || y != y1) &&
ok[x][y])
return true;
return false;
}
int dijkstra(int x1, int y1, int x2, int y2) {
map<int, map<int, int> > dist;
priority_queue<pair<int, pair<int, int> >,
vector<pair<int, pair<int, int> > >,
greater<pair<int, pair<int, int> > > >
q;
pair<int, pair<int, int> > p;
p = {0, {x1, y1}};
q.push(p);
while (!q.empty()) {
p = q.top();
q.pop();
int ux = p.second.first, uy = p.second.second, cost = p.first;
if (ux == x2 && uy == y2) return cost;
for (int i = 0; i < 8; i++) {
int x = dx[i], y = dy[i];
if (vaild(ux + x, uy + y, x1, y1) &&
(dist[ux + x][uy + y] == 0 || dist[ux + x][uy + y] > cost + 1))
dist[ux + x][uy + y] = cost + 1, p = {cost + 1, {ux + x, uy + y}},
q.push(p);
}
}
return (dist[x2][y2] == 0 ? -1 : dist[x2][y2]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2 >> n;
while (n--) {
cin >> r >> a >> b;
for (int i = a; i <= b; i++) ok[r][i] = true;
}
cout << dijkstra(x1, y1, x2, y2);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> start;
pair<int, int> endd;
int dx[] = {1, 0, -1};
int dy[] = {1, 0, -1};
struct node {
pair<int, int> p;
int s;
};
map<pair<int, int>, bool> visi;
int bfs(int x, int y) {
struct node star;
star.p = make_pair(x, y);
star.s = 0;
visi[star.p] = true;
queue<struct node> q;
q.push(star);
while (q.empty() == false) {
struct node ay = q.front();
q.pop();
if (ay.p == endd) {
return ay.s;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
pair<int, int> xx = make_pair(ay.p.first + dx[i], ay.p.second + dy[j]);
if (visi.count(xx)) {
if (visi[xx] == false) {
visi[xx] = true;
struct node xxx;
xxx.p = xx;
xxx.s = ay.s + 1;
q.push(xxx);
}
}
}
}
}
return -1;
}
int main() {
cin >> start.first >> start.second >> endd.first >> endd.second;
int m;
cin >> m;
while (m--) {
int r;
int a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
pair<int, int> x = make_pair(r, i);
visi[x] = false;
}
}
cout << bfs(start.first, start.second) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long mak = 1e9 + 5;
map<long long, int> mapas;
vector<int> grafas[maxn];
int visited[maxn] = {};
int X[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int Y[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long x1, y1, x2, y2;
cin >> y1 >> x1 >> y2 >> x2;
int n;
cin >> n;
mapas[x1 * mak + y1] = 1;
if (mapas[x2 * mak + y2] == 0) mapas[x2 * mak + y2] = 2;
if (abs(x1 - x2) <= 1 && abs(y1 - y2) <= 1) {
grafas[1].push_back(2);
grafas[2].push_back(1);
}
for (int i = 0; i < n; ++i) {
long long r, a, b;
cin >> r >> a >> b;
for (long long x = a; x <= b; ++x) {
if (mapas[x * mak + r] == 0) {
mapas[x * mak + r] = mapas.size();
for (int c = 0; c < 8; ++c) {
int is = mapas[x * mak + r];
int dbr = mapas[(x + X[c]) * mak + r + Y[c]];
if (dbr != 0) {
grafas[dbr].push_back(is);
grafas[is].push_back(dbr);
} else
mapas.erase((x + X[c]) * mak + r + Y[c]);
}
}
}
}
deque<int> dekas;
dekas.push_back(1);
visited[1] = 1;
while (dekas.size()) {
int dbr = dekas.front();
int kada = visited[dbr] + 1;
dekas.pop_front();
if (dbr == 2) {
cout << kada - 2;
return 0;
}
for (int v : grafas[dbr]) {
if (visited[v] == 0) {
visited[v] = kada;
dekas.push_back(v);
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int xa, ya, xb, yb;
int n;
map<pair<int, int>, int> mp, d;
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
int bfs(int x, int y) {
queue<pair<int, int> > q;
q.push(make_pair(x, y));
d[make_pair(x, y)] = 0;
while (!q.empty()) {
pair<int, int> t = q.front();
q.pop();
if (t.first == xb && t.second == yb) {
return d[t];
}
for (int i = 0; i < 8; i++) {
int a = t.first + dx[i], b = t.second + dy[i];
pair<int, int> now = make_pair(a, b);
if (mp.count(now) != 0 && d.count(now) == 0) {
d[now] = d[t] + 1;
q.push(now);
}
}
}
pair<int, int> goal = make_pair(xb, yb);
if (d.count(goal) == 0)
return -1;
else
return d[goal];
}
int main() {
cin >> xa >> ya >> xb >> yb;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
for (int j = a; j <= b; j++) mp[make_pair(r, j)] = 1;
}
int res = bfs(xa, ya);
cout << res << '\n';
return 0;
}
|
#include <bits/stdc++.h>
const double PI = 3.141592653589793238460;
using namespace std;
void kingsPath() {
int i, j, di, dj;
cin >> i >> j >> di >> dj;
set<pair<int, int>> st;
int t;
cin >> t;
int r, x, y;
while (t--) {
cin >> r >> x >> y;
for (int i = x; i <= y; i++) st.insert({r, i});
}
queue<pair<pair<int, int>, int>> qu;
qu.push({{i, j}, 0});
int ans = -1;
while (qu.size()) {
pair<int, int> curr = qu.front().first;
int moves = qu.front().second;
qu.pop();
i = curr.first;
j = curr.second;
if (st.find(curr) == st.end())
continue;
else
st.erase(curr);
if (curr.first == di && curr.second == dj) {
ans = moves;
break;
}
qu.push({{i - 1, j}, moves + 1});
qu.push({{i - 1, j + 1}, moves + 1});
qu.push({{i, j + 1}, moves + 1});
qu.push({{i + 1, j + 1}, moves + 1});
qu.push({{i + 1, j}, moves + 1});
qu.push({{i + 1, j - 1}, moves + 1});
qu.push({{i, j - 1}, moves + 1});
qu.push({{i - 1, j - 1}, moves + 1});
}
cout << ans << '\n';
}
int main() { kingsPath(); }
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> valid, dist;
int solve(int x0, int y0, int x1, int y1) {
queue<pair<int, int> > q;
q.push(pair<int, int>(x0, y0));
dist[pair<int, int>(x0, y0)] = 0;
while (!q.empty()) {
pair<int, int> f = q.front();
q.pop();
int x = f.first, y = f.second;
if (x == x1 && y == y1) return dist[pair<int, int>(x, y)];
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++) {
int xx = x + dx, yy = y + dy;
if (valid[pair<int, int>(xx, yy)] &&
dist.count(pair<int, int>(xx, yy)) == 0) {
dist[pair<int, int>(xx, yy)] = dist[pair<int, int>(x, y)] + 1;
q.push(pair<int, int>(xx, yy));
}
}
}
return -1;
}
int main() {
cout.sync_with_stdio(false);
cin.tie(NULL);
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) valid[pair<int, int>(r, j)] = 1;
}
cout << solve(x0, y0, x1, y1) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main(int argc, char** argv) {
int x0, y0, x, y, n;
int i, j, r, a, b;
set<pair<int, int> > s;
queue<pair<int, pair<int, int> > > q;
cin >> x0 >> y0 >> x >> y >> n;
for (i = 0; i < n; i++) {
cin >> r >> a >> b;
for (j = a; j <= b; j++) s.insert(make_pair(r, j));
}
q.push(make_pair(0, make_pair(x0, y0)));
while (!q.empty()) {
int cr = q.front().first;
int cx = q.front().second.first;
int cy = q.front().second.second;
q.pop();
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++) {
if (cx + i == x && cy + j == y) {
cout << cr + 1 << endl;
return 0;
}
if (i == 0 && j == 0) continue;
if (s.count(make_pair(cx + i, cy + j)) > 0) {
q.push(make_pair(cr + 1, make_pair(cx + i, cy + j)));
s.erase(make_pair(cx + i, cy + j));
}
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int> > S, O;
queue<pair<int, int> > Q[2];
int dx[] = {0, 0, 1, 1, 1, -1, -1, -1}, dy[] = {1, -1, 1, 0, -1, 1, 0, -1};
int bfs(int a, int b, int c, int d) {
O.insert(make_pair(a, b));
Q[0].push(make_pair(a, b));
for (int i = 0; true; i++) {
if (Q[i % 2].empty()) break;
while (!Q[i % 2].empty()) {
int a = Q[i % 2].front().first, b = Q[i % 2].front().second;
Q[i % 2].pop();
for (int k = 0; k < 8; k++) {
int na = a + dx[k], nb = b + dy[k];
if (na == c && nb == d) return i + 1;
if ((O.find(make_pair(na, nb)) == O.end()) &&
(S.find(make_pair(na, nb)) != S.end())) {
O.insert(make_pair(na, nb));
Q[(i + 1) % 2].push(make_pair(na, nb));
}
}
}
}
return -1;
}
int main() {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int n;
scanf("%d", &n);
while (n--) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
for (int i = y; i <= z; i++) S.insert(make_pair(x, i));
}
printf("%d\n", bfs(a, b, c, d));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct point {
long long int x;
long long int y;
bool operator<(const point p2) const {
if (x != p2.x) {
return x < p2.x;
} else {
return y < p2.y;
}
}
};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
long long int n;
cin >> n;
set<point> st;
for (long long int i = 0; i < n; i++) {
long long int r, a, b;
cin >> r >> a >> b;
for (long long int i = a; i <= b; i++) {
st.insert({r, i});
}
}
set<point> visited;
queue<point> q;
q.push({x1, y1});
visited.insert({x1, y1});
long long int moves = 0;
vector<long long int> dx = {-1, -1, -1, 0, 0, 1, 1, 1};
vector<long long int> dy = {-1, 0, 1, -1, 1, -1, 0, 1};
long long int limit = 1e9;
while (!q.empty()) {
long long int size = q.size();
while (size--) {
point curr = q.front();
q.pop();
if (curr.x == x2 && curr.y == y2) {
cout << moves;
return 0;
}
for (long long int i = 0; i < 8; i++) {
long long int X = curr.x + dx[i];
long long int Y = curr.y + dy[i];
if (X >= 1 && X <= limit && Y >= 1 && Y <= limit &&
st.find({X, Y}) != st.end() &&
visited.find({X, Y}) == visited.end()) {
q.push({X, Y});
visited.insert({X, Y});
}
}
}
moves++;
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
map<pair<int, int>, bool> matrix;
map<pair<int, int>, long long> d;
void bfs(int x, int y) {
queue<pair<int, int> > q;
q.push(make_pair(x, y));
matrix[make_pair(x, y)] = false;
while (!q.empty()) {
auto v = q.front();
q.pop();
if (matrix[make_pair(v.first - 1, v.second)]) {
matrix[make_pair(v.first - 1, v.second)] = false;
q.push(make_pair(v.first - 1, v.second));
d[make_pair(v.first - 1, v.second)] = d[v] + 1;
}
if (matrix[make_pair(v.first + 1, v.second)]) {
matrix[make_pair(v.first + 1, v.second)] = false;
q.push(make_pair(v.first + 1, v.second));
d[make_pair(v.first + 1, v.second)] = d[v] + 1;
}
if (matrix[make_pair(v.first, v.second - 1)]) {
matrix[make_pair(v.first, v.second - 1)] = false;
q.push(make_pair(v.first, v.second - 1));
d[make_pair(v.first, v.second - 1)] = d[v] + 1;
}
if (matrix[make_pair(v.first, v.second + 1)]) {
matrix[make_pair(v.first, v.second + 1)] = false;
q.push(make_pair(v.first, v.second + 1));
d[make_pair(v.first, v.second + 1)] = d[v] + 1;
}
if (matrix[make_pair(v.first + 1, v.second + 1)]) {
matrix[make_pair(v.first + 1, v.second + 1)] = false;
q.push(make_pair(v.first + 1, v.second + 1));
d[make_pair(v.first + 1, v.second + 1)] = d[v] + 1;
}
if (matrix[make_pair(v.first - 1, v.second + 1)]) {
matrix[make_pair(v.first - 1, v.second + 1)] = false;
q.push(make_pair(v.first - 1, v.second + 1));
d[make_pair(v.first - 1, v.second + 1)] = d[v] + 1;
}
if (matrix[make_pair(v.first + 1, v.second - 1)]) {
matrix[make_pair(v.first + 1, v.second - 1)] = false;
q.push(make_pair(v.first + 1, v.second - 1));
d[make_pair(v.first + 1, v.second - 1)] = d[v] + 1;
}
if (matrix[make_pair(v.first - 1, v.second - 1)]) {
matrix[make_pair(v.first - 1, v.second - 1)] = false;
q.push(make_pair(v.first - 1, v.second - 1));
d[make_pair(v.first - 1, v.second - 1)] = d[v] + 1;
}
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int x0, y0, x1, y1, n;
cin >> x0 >> y0 >> x1 >> y1 >> n;
for (int i = 0; i < n; ++i) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; ++j) matrix[make_pair(r, j)] = true;
}
d[make_pair(x0, y0)] = 0;
bfs(x0, y0);
int ans = -1;
if (d.find(make_pair(x1, y1)) != d.end()) ans = d[make_pair(x1, y1)];
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int xx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int yy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
int main() {
int x, y, z, i, t;
int st1, st, en, en1;
map<pair<int, int>, int> m;
queue<pair<int, int> > q;
while (cin >> st >> en >> st1 >> en1) {
cin >> x;
for (i = 0; i < x; i++) {
cin >> y >> z >> t;
for (int j = z; j <= t; j++) {
m[make_pair(y, j)] = -1;
}
}
m[make_pair(st, en)] = 0;
q.push(make_pair(st, en));
while (!q.empty()) {
int xxx = q.front().first;
int yyy = q.front().second;
q.pop();
for (int j = 0; j < 8; j++) {
int n = xxx + xx[j];
int l = yyy + yy[j];
if (m[make_pair(n, l)] < 0) {
m[make_pair(n, l)] = m[make_pair(xxx, yyy)] + 1;
q.push(make_pair(n, l));
}
}
}
cout << m[make_pair(st1, en1)] << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> point;
map<pair<int, int>, int> S, S1;
int X0, Y0, X1, Y1;
int bfs(void) {
S1.clear();
queue<pair<int, int> > Q;
Q.push(make_pair(X0, Y0));
S[make_pair(X0, Y0)] = 0;
while (!Q.empty()) {
int xx = Q.front().first;
int yy = Q.front().second;
Q.pop();
for (int i = (-1); i <= (1); i++)
for (int j = (-1); j <= (1); j++) {
if (!i && !j) continue;
pair<int, int> next = make_pair(xx + i, yy + j);
if (!S.count(next) || S1.count(next)) continue;
Q.push(next);
S1[next] = S1[make_pair(xx, yy)] + 1;
if (next.first == X1 && next.second == Y1) return S1[next];
}
}
return -1;
}
int main() {
while (cin >> X0 >> Y0 >> X1 >> Y1) {
int n;
cin >> n;
int r, a, b;
S.clear();
for (int i = 0; i < (n); i++) {
cin >> r >> a >> b;
for (int i = (a); i <= (b); i++) {
S[make_pair(r, i)]++;
}
}
printf("%d\n", bfs());
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 100010;
int dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy[] = {-1, 1, 0, -1, 1, 0, -1, 1};
queue<pair<pair<int, int>, int> > q;
int sx, sy, ex, ey, r, a, b, n;
pair<int, int> target, curpos;
pair<pair<int, int>, int> cur;
map<pair<int, int>, long long int> mmap;
bool vis[1 << 20];
int ind = 1;
int main() {
ios_base::sync_with_stdio(0);
scanf("%d", &(sx));
scanf("%d", &(sy));
scanf("%d", &(ex));
scanf("%d", &(ey));
target = make_pair(ex, ey);
scanf("%d", &(n));
for (int i = 0, _n = (n); i < _n; ++i) {
scanf("%d", &(r));
scanf("%d", &(a));
scanf("%d", &(b));
for (int x = a, _n = (b + 1); x < _n; ++x) {
if (mmap.find(make_pair(r, x)) == mmap.end()) {
mmap[make_pair(r, x)] = ind++;
}
}
}
q.push(make_pair(make_pair(sx, sy), 0));
int ans = -1;
while (!q.empty()) {
cur = q.front();
q.pop();
curpos = cur.first;
if (curpos == target) {
ans = cur.second;
break;
}
for (int i = 0, _n = (8); i < _n; ++i) {
pair<int, int> nxt =
make_pair(curpos.first + dx[i], curpos.second + dy[i]);
if (mmap[nxt]) {
if (!vis[mmap[nxt]]) {
vis[mmap[nxt]] = 1;
q.push(make_pair(nxt, cur.second + 1));
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
clock_t time_p = clock();
void aryanc403() {}
const long long int INF = 0xFFFFFFFFFFFFFFFL;
long long int seed;
mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count());
inline long long int rnd(long long int l = 0, long long int r = INF) {
return uniform_int_distribution<long long int>(l, r)(rng);
}
class CMP {
public:
bool operator()(pair<long long int, long long int> a,
pair<long long int, long long int> b) {
return !(a.first < b.first || (a.first == b.first && a.second <= b.second));
}
};
void add(map<long long int, long long int> &m, long long int x,
long long int cnt = 1) {
auto jt = m.find(x);
if (jt == m.end())
m.insert({x, cnt});
else
jt->second += cnt;
}
void del(map<long long int, long long int> &m, long long int x,
long long int cnt = 1) {
auto jt = m.find(x);
if (jt->second <= cnt)
m.erase(jt);
else
jt->second -= cnt;
}
bool cmp(const pair<long long int, long long int> &a,
const pair<long long int, long long int> &b) {
return a.first < b.first || (a.first == b.first && a.second < b.second);
}
const long long int mod = 1000000007L;
long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y;
map<pair<long long int, long long int>, long long int> m;
string s;
vector<long long int> a;
void bfs(long long int x, long long int y, long long int x1, long long int y1) {
std::queue<pair<long long int, pair<long long int, long long int>>> q;
q.push({0, {x, y}});
vector<pair<long long int, long long int>> dr;
dr.push_back({1, 0});
dr.push_back({-1, 0});
dr.push_back({0, 1});
dr.push_back({0, -1});
dr.push_back({1, 1});
dr.push_back({-1, -1});
dr.push_back({-1, 1});
dr.push_back({1, -1});
while (!q.empty()) {
auto u = q.front();
q.pop();
auto it = m.find(u.second);
if (it == m.end() || it->second <= u.first) continue;
;
if (x1 == u.second.first && y1 == u.second.second) {
cout << u.first << "\n";
exit(0);
}
it->second = u.first;
for (auto dx : dr)
q.push({u.first + 1,
{u.second.first + dx.first, u.second.second + dx.second}});
}
}
int main(void) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
{
long long int x0, x1, y0, y1;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
while (n--) {
cin >> r >> x >> y;
for (i = (x); i <= (y); ++i) m[make_pair(r, i)] = INF;
}
bfs(x0, y0, x1, y1);
;
cout << -1 << "\n";
}
aryanc403();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int x3, y3, x2, y2;
int o1[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int o2[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
set<pair<int, int> > s;
map<pair<int, int>, int> f;
bool check(int x, int y) {
return (x >= 1 && x <= 1000000000 && y >= 1 && y <= 1000000000) &&
s.count(pair<int, int>(x, y)) == 1;
}
int bfs() {
queue<pair<int, int> > q;
q.push(pair<int, int>(x3, y3));
f[pair<int, int>(x3, y3)] = 0;
while (q.size()) {
int u = q.front().first, v = q.front().second;
q.pop();
for (int i = 0; i <= 7; i++) {
int x = u + o1[i], y = v + o2[i];
if (check(x, y) &&
f[pair<int, int>(u, v)] + 1 < f[pair<int, int>(x, y)]) {
f[pair<int, int>(x, y)] = f[pair<int, int>(u, v)] + 1;
q.push(pair<int, int>(x, y));
}
}
}
int ans = f[pair<int, int>(x2, y2)];
if (ans == INT_MAX) return -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> x3 >> y3 >> x2 >> y2;
if (x3 > x2) swap(x3, x2), swap(y3, y2);
int n;
cin >> n;
while (n--) {
int x, a, b;
cin >> x >> a >> b;
for (int i = a; i <= b; i++) {
s.insert(pair<int, int>(x, i));
f[pair<int, int>(x, i)] = INT_MAX;
}
}
cout << bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int power(int x, unsigned int y) {
int res = 1;
while (y > 0) {
if (y & 1) {
res = res * x;
}
y = y >> 1;
x = x * x;
}
return res;
}
int powermod(int x, unsigned int y, int p) {
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
int Mod(int x, int m) { return (x % m + m) % m; }
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
const int mxn = 2e5 + 5;
map<pair<int, int>, int> p;
vector<int> g[mxn];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
;
int a, b, c, d;
cin >> a >> b >> c >> d;
int k;
cin >> k;
int n = 0;
vector<pair<int, pair<int, int>>> v;
for (int i = 0; i < k; i++) {
int a, b, c;
cin >> a >> b >> c;
v.push_back(make_pair(a, make_pair(b, c)));
}
sort(v.begin(), v.end());
for (int j = 0; j < k; j++) {
int l, r;
int row;
row = v[j].first;
l = v[j].second.first;
r = v[j].second.second;
for (int i = l; i <= r; i++) {
if (!p.count({row, i})) {
p[{row, i}] = n;
n++;
}
if (p.count({row, i - 1})) {
g[p[{row, i}]].push_back(p[{row, i - 1}]);
g[p[{row, i - 1}]].push_back(p[{row, i}]);
}
if (p.count({row, i + 1})) {
g[p[{row, i}]].push_back(p[{row, i + 1}]);
g[p[{row, i + 1}]].push_back(p[{row, i}]);
}
if (p.count({row - 1, i})) {
g[p[{row, i}]].push_back(p[{row - 1, i}]);
g[p[{row - 1, i}]].push_back(p[{row, i}]);
}
if (p.count({row - 1, i - 1})) {
g[p[{row, i}]].push_back(p[{row - 1, i - 1}]);
g[p[{row - 1, i - 1}]].push_back(p[{row, i}]);
}
if (p.count({row - 1, i + 1})) {
g[p[{row, i}]].push_back(p[{row - 1, i + 1}]);
g[p[{row - 1, i + 1}]].push_back(p[{row, i}]);
}
if (p.count({row + 1, i + 1})) {
g[p[{row, i}]].push_back(p[{row + 1, i + 1}]);
g[p[{row + 1, i + 1}]].push_back(p[{row, i}]);
}
if (p.count({row + 1, i - 1})) {
g[p[{row, i}]].push_back(p[{row + 1, i - 1}]);
g[p[{row + 1, i - 1}]].push_back(p[{row, i}]);
}
}
}
int s = p[make_pair(a, b)];
int f = p[make_pair(c, d)];
queue<pair<int, int>> q;
q.push(make_pair(s, 0));
int vis[n + 1];
memset(vis, 0, sizeof(vis));
while (!q.empty()) {
int u = q.front().first;
int d = q.front().second;
q.pop();
if (u == f) {
cout << d << '\n';
exit(0);
}
for (auto x : g[u]) {
if (!vis[x]) {
q.push(make_pair(x, d + 1));
vis[x] = 1;
}
}
}
cout << -1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
int dx[] = {-1, 0, 1};
int dy[] = {-1, 0, 1};
int r, a, b;
set<pair<int, int> > m;
map<pair<int, int>, int> dist;
for (int i = 1; i <= n; i++) {
scanf("%d %d %d", &r, &a, &b);
for (int j = a; j <= b; j++) {
m.insert(make_pair(r, j));
}
}
queue<pair<int, int> > q1;
q1.push(make_pair(x0, y0));
dist[make_pair(x0, y0)] = 0;
while (!q1.empty()) {
pair<int, int> coord = q1.front();
q1.pop();
if (coord.first == x1 && coord.second == y1) {
cout << dist[coord] << endl;
return 0;
}
for (int x : dx) {
for (int y : dy) {
if (x == 0 && y == 0) continue;
pair<int, int> next = make_pair(coord.first + x, coord.second + y);
if (m.find(next) != m.end() && !dist[next]) {
q1.push(next);
dist[next] = dist[coord] + 1;
}
}
}
}
cout << "-1" << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double EPS = 1e-9;
int mods(int a, int b) { return (b + (a % b)) % b; }
int n, r, a, b, sx, sy, ex, ey;
int dr[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dc[8] = {0, 1, 1, 1, 0, -1, -1, -1};
bool finish;
map<pair<int, int>, int> dist;
pair<int, int> u, v;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> sx >> sy >> ex >> ey >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) dist[pair<int, int>(r, j)] = -1;
}
if (!dist.count(pair<int, int>(ex, ey))) {
cout << -1 << '\n';
return 0;
}
dist[pair<int, int>(sx, sy)] = 0;
queue<pair<int, int> > q;
q.push(pair<int, int>(sx, sy));
while (!q.empty() && !finish) {
u = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
v.first = u.first + dr[i];
v.second = u.second + dc[i];
if (dist.count(v)) {
if (dist[v] != -1) continue;
dist[v] = dist[u] + 1;
if (v == pair<int, int>(ex, ey)) {
finish = 1;
break;
}
q.push(v);
}
}
}
cout << dist[pair<int, int>(ex, ey)] << '\n';
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.