text stringlengths 49 983k |
|---|
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int x0, Y0, x1, Y1;
int n;
set<pair<int, int> > g, vis;
int dir[8][2] = {{0, 1}, {1, 1}, {1, 0}, {1, -1},
{0, -1}, {-1, -1}, {-1, 0}, {-1, 1}};
map<pair<int, int>, int> dist;
bool bfs(int x, int y) {
queue<pair<int, int> > q;
q.push(pair<int, int>(x, y));
dist[pair<int, int>(x, y)] = 0;
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
if (x == x1 and y == Y1) return 1;
for (int i = 0; i < 8; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (g.count(pair<int, int>(nx, ny)) and
!vis.count(pair<int, int>(nx, ny))) {
q.push(pair<int, int>(nx, ny));
vis.insert(pair<int, int>(nx, ny));
dist[pair<int, int>(nx, ny)] = dist[pair<int, int>(x, y)] + 1;
}
}
}
return 0;
}
int main(void) {
scanf("%d%d%d%d", &x0, &Y0, &x1, &Y1);
scanf("%d", &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++) {
g.insert(pair<int, int>(r, j));
}
}
bool ok = bfs(x0, Y0);
if (ok) {
printf("%d\n", dist[pair<int, int>(x1, Y1)]);
} else {
printf("-1\n");
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int mxn = 1e5 + 10;
map<pair<int, int>, int> mp;
int pointCnt;
bool vis[mxn + 100];
int dis[mxn + 100];
pair<int, int> ar[mxn + 100];
int dr[] = {0, -1, -1, -1, 0, 1, 1, 1};
int dc[] = {1, 1, 0, -1, -1, -1, 0, 1};
void dfs(int u) {
queue<int> q;
q.push(u);
dis[u] = 0;
vis[u] = true;
while (!q.empty()) {
u = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int x = ar[u].first + dr[i];
int y = ar[u].second + dc[i];
if (mp.find(make_pair(x, y)) != mp.end()) {
int pos = mp[make_pair(x, y)];
if (!vis[pos]) {
dis[pos] = dis[u] + 1;
vis[pos] = true;
q.push(pos);
}
}
}
}
}
int main() {
int sx, sy, ex, ey;
int n;
pointCnt = 0;
cin >> sx >> sy >> ex >> ey;
cin >> n;
int r, c1, c2;
for (int i = 0; i < n; i++) {
cin >> r >> c1 >> c2;
for (int j = c1; j <= c2; j++) {
mp[make_pair(r, j)] = ++pointCnt;
ar[pointCnt] = make_pair(r, j);
}
}
if (mp.find(make_pair(sx, sy)) == mp.end()) {
mp[make_pair(sx, sy)] = ++pointCnt;
ar[pointCnt] = make_pair(sx, sy);
}
if (mp.find(make_pair(ex, ey)) == mp.end()) {
mp[make_pair(ex, ey)] = ++pointCnt;
ar[pointCnt] = make_pair(ex, ey);
}
memset(vis, false, sizeof vis);
dfs(mp[make_pair(sx, sy)]);
if (vis[mp[make_pair(ex, ey)]]) {
cout << dis[mp[make_pair(ex, ey)]] << endl;
} else
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int BOARD_SIZE = 1e9;
template <typename T, typename MAX = int>
struct dynamic_fenwick_tree {
private:
unordered_map<MAX, T> a;
MAX n;
public:
dynamic_fenwick_tree() {
a.clear();
n = BOARD_SIZE;
}
void initialize(MAX k) {
n = k;
a.clear();
}
void update(MAX pos, T val) {
for (; pos <= n; pos += pos & (-pos)) a[pos] += val;
}
T query(MAX pos) {
T ans = 0;
for (; pos >= 1; pos -= pos & (-pos)) ans += a[pos];
return ans;
}
};
int x0, iuadoghasdgj, x1, taklahgjkla;
unordered_map<int, dynamic_fenwick_tree<int> > it;
unordered_map<int, unordered_map<int, int> > used;
unordered_map<int, unordered_map<int, int> > dist;
int n;
queue<pair<int, int> > q;
bool allowed(int r, int c) {
if (r < 1 || r > BOARD_SIZE || c < 1 || c > BOARD_SIZE) return false;
if (it.find(r) == it.end()) return false;
return (it[r].query(c) > 0);
}
void bfs(int r, int c) {
int p, t, i, j;
q.push(make_pair(r, c));
used[r][c] = true;
dist[r][c] = 0;
while (!q.empty()) {
i = q.front().first;
j = q.front().second;
q.pop();
p = i + 1;
t = j;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i - 1;
t = j;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i;
t = j + 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i;
t = j - 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i + 1;
t = j + 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i + 1;
t = j - 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i - 1;
t = j + 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
p = i - 1;
t = j - 1;
if (allowed(p, t))
if (!used[p][t]) {
used[p][t] = true;
dist[p][t] = dist[i][j] + 1;
q.push(make_pair(p, t));
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int i, r, x, y;
scanf("%d %d %d %d", &x0, &iuadoghasdgj, &x1, &taklahgjkla);
scanf("%d", &n);
for (i = 1; i <= n; i++) {
scanf("%d %d %d", &r, &x, &y);
it[r].update(x, 1);
it[r].update(y + 1, -1);
}
bfs(x0, iuadoghasdgj);
if (!used[x1][taklahgjkla])
printf("-1\n");
else
printf("%d\n", dist[x1][taklahgjkla]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int movX[] = {0, 0, 1, -1, 1, 1, -1, -1}, movY[] = {1, -1, 0, 0, 1, -1, -1, 1};
map<pair<int, int>, int> vis;
int shortest(int a, int b, int x, int y) {
queue<pair<int, int> > q;
vis[make_pair(a, b)] = 1;
q.push(make_pair(a, b));
while (!q.empty()) {
int ch1 = q.front().first, ch2 = q.front().second;
q.pop();
if (ch1 == x && ch2 == y) return vis[make_pair(ch1, ch2)];
for (int i = 0; i < 8; i++) {
int nx = movX[i] + ch1, ny = movY[i] + ch2;
if (vis.find(make_pair(nx, ny)) != vis.end()) {
if (!vis[make_pair(nx, ny)])
q.push(make_pair(nx, ny)),
vis[make_pair(nx, ny)] = vis[make_pair(ch1, ch2)] + 1;
}
}
}
return 0;
}
int main() {
int a, b, x, y, q;
cin >> a >> b >> x >> y >> q;
while (q--) {
int r, c1, c2;
scanf("%d%d%d", &r, &c1, &c2);
for (int i = c1; i <= c2; i++) vis[make_pair(r, i)] = 0;
}
cout << shortest(a, b, x, y) - 1 << "\n";
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int x0, y0, x1, y1;
scanf("%d%d%d%d", &x0, &y0, &x1, &y1);
int n;
scanf("%d", &n);
unordered_map<long long, bool> av, vis;
int a, b, c;
long long tril = 1000000000;
for (int i = 0; i < n; i++) {
scanf("%d%d%d", &a, &b, &c);
for (int j = b; j <= c; j++) {
av[tril * a + j] = 1;
}
}
unordered_map<long long, int> cnt;
queue<long long> q;
q.push(tril * x0 + y0);
cnt[tril * x0 + y0] = 0;
vis[tril * x0 + y0] = 1;
while (!q.empty()) {
long long f = q.front();
q.pop();
long long x, y;
x = f / tril;
y = f - x * tril;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (av[tril * i + j] && !vis[tril * i + j]) {
cnt[tril * i + j] = cnt[f] + 1;
vis[tril * i + j] = 1;
q.push(tril * i + j);
}
}
}
}
if (!vis[x1 * tril + y1])
printf("-1");
else
printf("%d", cnt[x1 * tril + y1]);
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> m;
map<pair<int, int>, int> dist;
map<pair<int, int>, bool> visited;
int main() {
pair<int, int> init;
pair<int, int> final;
cin >> init.first >> init.second >> final.first >> final.second;
m[init] = true;
m[final] = true;
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++) {
m[make_pair(r, j)] = true;
}
}
queue<pair<int, int> > q;
q.push(init);
visited[init] = true;
dist[init] = 0;
while (!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
int x = curr.first;
int y = curr.second;
int a = x - 1;
int b = y;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x + 1;
b = y;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x;
b = y - 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x;
b = y + 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x - 1;
b = y - 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x - 1;
b = y + 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x + 1;
b = y - 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
a = x + 1;
b = y + 1;
if ((m[make_pair(a, b)] == true) && (!visited[make_pair(a, b)])) {
q.push(make_pair(a, b));
visited[make_pair(a, b)] = true;
dist[make_pair(a, b)] = dist[curr] + 1;
}
}
if (dist.count(final) == 0) {
cout << -1;
return 0;
}
cout << dist[final] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const long long int mxn = 1e5 + 1;
const string fuck = "fuck";
void solve() {
long long int x1, x2, y1, y2, n;
set<pair<long long int, long long int> > pos;
cin >> x1 >> y1 >> x2 >> y2;
cin >> n;
for (long long int i = 0; i < n; i++) {
long long int a, b, r;
cin >> r >> a >> b;
for (a = a; a <= b; a++) {
pos.insert(pair<long long int, long long int>(r, a));
}
}
vector<pair<long long int, long long int> > move;
move.push_back(pair<long long int, long long int>(-1, -1));
move.push_back(pair<long long int, long long int>(-1, 1));
move.push_back(pair<long long int, long long int>(-1, 0));
move.push_back(pair<long long int, long long int>(0, -1));
move.push_back(pair<long long int, long long int>(0, 1));
move.push_back(pair<long long int, long long int>(1, -1));
move.push_back(pair<long long int, long long int>(1, 0));
move.push_back(pair<long long int, long long int>(1, 1));
queue<pair<long long int, long long int> > q;
pair<long long int, long long int> start = {x1, y1};
pair<long long int, long long int> end = {x2, y2};
q.push(start);
map<pair<long long int, long long int>, bool> used;
map<pair<long long int, long long int>, long long int> dis;
set<pair<long long int, long long int> > par;
dis[start] = 0;
while (!q.empty()) {
pair<long long int, long long int> top = q.front();
q.pop();
if (pair<long long int, long long int>(top) ==
pair<long long int, long long int>(end)) {
cout << dis[end];
return;
}
if (used[pair<long long int, long long int>(top)]) continue;
used[pair<long long int, long long int>(top)] = true;
for (pair<long long int, long long int> x : move) {
pair<long long int, long long int> cur =
pair<long long int, long long int>(top.first + x.first,
top.second + x.second);
if (!used[pair<long long int, long long int>(cur)] &&
pos.find(pair<long long int, long long int>(cur)) != pos.end() &&
!dis[cur]) {
dis[pair<long long int, long long int>(cur)] =
dis[pair<long long int, long long int>(top)] + 1;
q.push(pair<long long int, long long int>(cur));
}
}
}
cout << -1;
return;
}
int main() {
long long int cse = 1;
while (cse--) {
solve();
cout << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
int dr[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[] = {-1, 0, 1, -1, 1, -1, 0, 1};
struct node {
int r, c, dist;
node() {}
node(int _r, int _c, int _dist) : r(_r), c(_c), dist(_dist) {}
};
set<pair<int, int> > valid, visited;
int main() {
int r0, c0, r1, c1, n;
int i, j, r, a, b;
scanf("%d%d%d%d%d", &r0, &c0, &r1, &c1, &n);
for (i = 1; i <= n; ++i) {
scanf("%d%d%d", &r, &a, &b);
for (j = a; j <= b; ++j) valid.insert(make_pair(r, j));
}
int ans = -1;
queue<node> Q;
Q.push(node(r0, c0, 0));
visited.insert(make_pair(r0, c0));
while (Q.size() > 0) {
node cur = Q.front();
Q.pop();
if (cur.c == c1 && cur.r == r1) {
ans = cur.dist;
break;
}
for (i = 0; i < 8; ++i) {
int r2 = cur.r + dr[i];
int c2 = cur.c + dc[i];
if (valid.find(make_pair(r2, c2)) != valid.end() &&
visited.find(make_pair(r2, c2)) == visited.end()) {
visited.insert(make_pair(r2, c2));
Q.push(node(r2, c2, cur.dist + 1));
}
}
}
printf("%d", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> d;
queue<pair<int, int> > q;
int main() {
int sx, sy, ex, ey, n, a, b, r;
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
cin >> sx >> sy >> ex >> ey >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) d[make_pair(r, j)] = -1;
}
d[make_pair(sx, sy)] = 0;
q.push(make_pair(sx, sy));
while (q.size()) {
pair<int, int> u = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> v = make_pair(u.first + dx[i], u.second + dy[i]);
if (d.count(v) && d[v] == -1) d[v] = d[u] + 1, q.push(v);
}
}
printf("%d", d[make_pair(ex, ey)]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, x0, y0, x1, y1;
map<pair<int, int>, int> cases;
scanf("%d %d", &x0, &y0);
scanf("%d %d", &x1, &y1);
scanf("%d", &n);
int r, a, b;
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &r, &a, &b);
for (int i = a; i < b + 1; i++) {
cases[make_pair(r, i)] = 0;
}
}
set<pair<int, int> > attente;
attente.insert(make_pair(x0, y0));
set<pair<int, int> >::iterator it;
int x, y, res = 0;
while (attente.size() > 0) {
set<pair<int, int> > temp;
for (it = attente.begin(); it != attente.end(); it++) {
tie(x, y) = *it;
cases[make_pair(x, y)] = 1;
if (x == x1 && y == y1) {
printf("%d\n", res);
return 0;
}
int X[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int Y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int i = 0; i < 8; i++) {
if (cases.find(make_pair(x + X[i], y + Y[i])) != cases.end() &&
cases[make_pair(x + X[i], y + Y[i])] == 0) {
temp.insert(make_pair(x + X[i], y + Y[i]));
}
}
}
res++;
attente = temp;
}
printf("%d\n", -1);
}
|
#include <bits/stdc++.h>
using namespace std;
const pair<int, int> dir[] = {
make_pair(-1, -1), make_pair(-1, 0), make_pair(-1, 1), make_pair(0, -1),
make_pair(0, 1), make_pair(1, -1), make_pair(1, 0), make_pair(1, 1)};
int n;
pair<int, int> source, target;
set<pair<int, int> > mapping;
map<pair<int, int>, int> dist;
__inline pair<int, int> operator+(const pair<int, int> &a,
const pair<int, int> &b) {
return make_pair(a.first + b.first, a.second + b.second);
}
int main(void) {
ios::sync_with_stdio(false);
cin >> source.first >> source.second >> target.first >> target.second;
cin >> n;
for (int i = 0; i < n; ++i) {
int x, left, right;
cin >> x >> left >> right;
for (int y = left; y <= right; ++y) {
mapping.insert(make_pair(x, y));
}
}
vector<pair<int, int> > queue;
queue.push_back(source);
dist[source] = 0;
for (int head = 0; head < (int)queue.size(); ++head) {
pair<int, int> x = queue[head];
for (int i = 0; i < 8; ++i) {
pair<int, int> y = x + dir[i];
if (mapping.count(y) && !dist.count(y)) {
dist[y] = dist[x] + 1;
queue.push_back(y);
}
}
}
if (!dist.count(target)) {
cout << -1 << endl;
} else {
cout << dist[target] << endl;
}
}
|
#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);
cin.tie(0);
cout.tie(0);
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;
const int inf = 1e9;
set<pair<int, int>> segments;
const pair<int, int> dirs[] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1},
{1, 1}, {-1, -1}, {1, -1}, {-1, 1}};
int bfs(pair<int, int> s, pair<int, int> d) {
map<pair<int, int>, int> level;
queue<pair<int, int>> Q;
level[s] = 0;
Q.push(s);
while (!Q.empty()) {
auto p = Q.front();
Q.pop();
if (p == d) return level[d];
for (auto d : dirs) {
int r = p.first + d.first;
int c = p.second + d.second;
if (!segments.count({r, c})) continue;
if (level.count({r, c}) == 1) continue;
level[{r, c}] = level[p] + 1;
Q.emplace(r, c);
}
}
return -1;
}
int main() {
pair<int, int> start, dest;
cin >> start.first >> start.second >> dest.first >> dest.second;
int n;
cin >> n;
while (n--) {
int r, a, b;
cin >> r >> a >> b;
for (int c = a; c <= b; c++) {
segments.emplace(r, c);
}
}
int moves = bfs(start, dest);
cout << moves << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
struct node {
int x, y;
int d;
};
int bfs(int n, int x0, int y0, int x1, int y1) {
node st, nd;
node now, next;
int i, r, a, b;
map<int, map<int, int> > myMap;
scanf("%d", &n);
while (n--) {
scanf("%d%d%d", &r, &a, &b);
for (i = a; i <= b; i++) myMap[r][i] = 1;
}
st.x = x0, st.y = y0, st.d = 0;
nd.x = x1, nd.y = y1;
queue<node> que;
que.push(st);
myMap[st.x][st.y] = 0;
while (!que.empty()) {
now = que.front();
if (now.x == nd.x && now.y == nd.y) return now.d;
que.pop();
for (i = 0; i < 8; i++) {
next.x = now.x + dx[i];
next.y = now.y + dy[i];
next.d = now.d + 1;
if (myMap[next.x][next.y] == 1) {
myMap[next.x][next.y] = 0;
que.push(next);
}
}
}
return -1;
}
int main() {
int x0, y0, x1, y1;
int n;
while (scanf("%d%d%d%d", &x0, &y0, &x1, &y1) != EOF) {
printf("%d\n", bfs(n, x0, y0, x1, y1));
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const long long INF = 1e18;
const int MX = 1e9;
set<pair<int, int> > pool;
map<pair<int, int>, int> m;
pair<int, int> st, ed;
int N, R, A, B;
int dx[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy[8] = {1, 1, 1, 0, 0, -1, -1, -1};
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
cin >> st.first >> st.second >> ed.first >> ed.second;
cin >> N;
for (int i = (0); i < (N); ++i) {
cin >> R >> A >> B;
for (int j = (A); j < (B + 1); ++j) pool.insert(make_pair(R, j));
}
for (auto& iter : pool) m[iter] = -1;
queue<pair<int, int> > q;
while (!q.empty()) q.pop();
q.push(st);
m[st] = 0;
while (!q.empty()) {
pair<int, int> curr = q.front();
q.pop();
for (int i = (0); i < (8); ++i) {
int x = curr.first + dx[i], y = curr.second + dy[i];
pair<int, int> to = make_pair(x, y);
if (m[to] == -1) {
m[to] = m[curr] + 1;
q.push(to);
}
}
}
cout << m[ed];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
namespace my {
const int SIZE = 100001;
int x0, y0, x1, y1;
int n;
int R[SIZE], A[SIZE], B[SIZE];
set<pair<int, int> > F;
void init() { F.clear(); }
bool input() {
if (!(cin >> x0 >> y0 >> x1 >> y1)) return false;
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> R[i] >> A[i] >> B[i];
}
return true;
}
priority_queue<pair<int, pair<int, int> >, vector<pair<int, pair<int, int> > >,
greater<pair<int, pair<int, int> > > >
Q;
set<pair<int, int> > V;
const int dr[8] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dc[8] = {1, -1, 0, 0, 1, -1, 1, -1};
void dijkstra_init() {
Q = priority_queue<pair<int, pair<int, int> >,
vector<pair<int, pair<int, int> > >,
greater<pair<int, pair<int, int> > > >();
V.clear();
}
bool check_state(pair<int, int> state) {
set<pair<int, int> >::iterator it_begin =
F.lower_bound(pair<int, int>(state.first, -1));
for (set<pair<int, int> >::iterator it_i = it_begin; it_i != F.end();
++it_i) {
int r = (*it_i).first;
int k = (*it_i).second;
if (r != state.first) break;
int a = A[k];
int b = B[k];
if (a <= state.second && state.second <= b) {
return true;
}
}
return false;
}
bool check_visited(pair<int, int> state) {
if (V.count(state)) return false;
V.insert(state);
return true;
}
int dijkstra(pair<int, int> start_cell, pair<int, int> goal_cell) {
dijkstra_init();
pair<int, int> start_state(start_cell);
pair<int, pair<int, int> > start_node(0, start_state);
Q.push(start_node);
while (!Q.empty()) {
pair<int, pair<int, int> > node = Q.top();
Q.pop();
int steps = node.first;
int r = node.second.first;
int c = node.second.second;
if (r == goal_cell.first && c == goal_cell.second) {
return steps;
}
for (int i = 0; i < 8; ++i) {
int next_steps = steps + 1;
int nr = r + dr[i];
int nc = c + dc[i];
pair<int, int> next_state(nr, nc);
if (!check_state(next_state)) {
continue;
}
if (!check_visited(next_state)) {
continue;
}
pair<int, pair<int, int> > next_node(next_steps, next_state);
Q.push(next_node);
}
}
return -1;
}
void solve_init() {
for (int i = 0; i < n; ++i) {
F.insert(pair<int, int>(R[i], i));
}
}
int solve() {
solve_init();
return dijkstra(pair<int, int>(x0, y0), pair<int, int>(x1, y1));
}
} // namespace my
int main() {
while (my::init(), my::input()) {
cout << my::solve() << endl;
}
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int xi, yi, xdes, ydes, n;
map<pair<int, int>, int> mp;
pair<int, int> d[8] = {make_pair(-1, -1), make_pair(+0, -1), make_pair(+1, -1),
make_pair(-1, +0), make_pair(+1, +0), make_pair(-1, +1),
make_pair(+0, +1), make_pair(+1, +1)};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> xi >> yi >> xdes >> ydes >> n;
int r, a, b;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
mp[make_pair(r, j)] = -1;
}
}
queue<pair<int, int>> q;
q.push({xi, yi});
mp[make_pair(xi, yi)] = 0;
mp[make_pair(xdes, ydes)] = -1;
while (!q.empty()) {
auto u = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> v = {u.first + d[i].first, u.second + d[i].second};
if (mp[v] == -1 || mp[v] > mp[u] + 1) {
mp[v] = mp[u] + 1;
q.push(v);
}
}
}
cout << mp[{xdes, ydes}];
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;
multimap<int, pair<long long int, long long int> > sections;
for (long long int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
sections.insert(make_pair(r, make_pair(a, b)));
}
map<pair<long long int, long long int>, bool> visited;
map<pair<long long int, long long int>, int> dist;
pair<long long int, long long int> start = make_pair(x0, y0);
pair<long long int, long long int> target = make_pair(x1, y1);
queue<pair<long long int, long long int> > que;
que.push(start);
visited[start] = true;
dist[start] = 0;
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
while (que.size()) {
pair<long long int, long long int> cur = que.front();
que.pop();
if (cur == target) {
break;
};
for (long long int i = 0; i < 8; i++) {
long long int nr = cur.first + dy[i];
long long int nc = cur.second + dx[i];
bool ok = false;
if (visited[make_pair(nr, nc)]) continue;
auto range = sections.equal_range(nr);
for (auto it = range.first; it != range.second; it++) {
pair<long long int, long long int> sec = it->second;
if (nc <= sec.second && nc >= sec.first) {
ok = true;
break;
}
}
if (ok) {
visited[make_pair(nr, nc)] = true;
dist[make_pair(nr, nc)] = dist[cur] + 1;
que.push(make_pair(nr, nc));
}
}
}
if (dist.find(target) == dist.end()) {
cout << -1 << endl;
} else {
cout << dist[target] << endl;
}
}
|
#include <bits/stdc++.h>
using namespace std;
pair<int, int> operator+(pair<int, int> p1, pair<int, int> p2) {
return make_pair(p1.first + p2.first, p1.second + p2.second);
}
int64_t bfs(pair<int32_t, int32_t> source, pair<int32_t, int32_t> dest,
map<pair<int, int>, bool>& mp) {
map<pair<int, int>, int64_t> dist;
dist[source] = 0;
queue<pair<int, int> > Q;
Q.push(source);
while (!Q.empty()) {
pair<int, int> top = Q.front();
Q.pop();
if (mp[top] == true) {
mp[top] = false;
vector<pair<int, int> > vec = {{0, 1}, {0, -1}, {1, 0}, {1, 1},
{1, -1}, {-1, 0}, {-1, 1}, {-1, -1}};
for (pair<int, int> p : vec) {
if (dist.find(top + p) == dist.end() || dist[top + p] > dist[top] + 1)
dist[top + p] = dist[top] + 1;
Q.push(top + p);
}
}
}
if (dist.find(dest) == dist.end())
return numeric_limits<int64_t>::max();
else
return dist[dest];
}
int main() {
ios::sync_with_stdio(false);
pair<int32_t, int32_t> source, dest;
cin >> source.first >> source.second >> dest.first >> dest.second;
int n;
cin >> n;
map<pair<int, int>, bool> mp;
for (int i = 0; i < (n); i++) {
int row, start, end;
cin >> row >> start >> end;
for (int j = (start); j < (end + 1); j++) mp[make_pair(row, j)] = true;
}
int64_t dist = bfs(source, dest, mp);
cout << (dist == numeric_limits<int64_t>::max() ? -1 : dist) << "\n";
return (0);
}
|
#include <bits/stdc++.h>
using namespace std;
int go[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {1, 0},
{-1, 0}, {-1, 1}, {0, 1}, {1, 1}};
queue<pair<int, int> > qq;
map<pair<int, int>, int> m;
int sx, sy, ex, ey, n;
int r, li, ri;
long long ans;
void bfs() {
pair<int, int> start;
start = make_pair(sx, sy);
m[start] = 0;
qq.push(start);
while (!qq.empty()) {
pair<int, int> p1, p2;
p1 = qq.front();
if (p1.first == ex && p1.second == ey) {
ans = m[p1];
return;
}
qq.pop();
for (int i = 0; i < 8; i++) {
p2.first = p1.first + go[i][0];
p2.second = p1.second + go[i][1];
if (m.find(p2) != m.end() && m[p2] == -1) {
m[p2] = m[p1] + 1;
qq.push(p2);
}
}
}
}
int main() {
cin >> sx >> sy >> ex >> ey;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r >> li >> ri;
for (int j = li; j <= ri; j++) m[make_pair(r, j)] = -1;
}
ans = -1;
bfs();
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct node {
long long x, y;
long long num;
};
queue<node> q;
long long stx, sty, n, xx, yy, fnx, fny, a, b, c, i, j;
long long ind, ans;
node k;
long long dx[8] = {0, 0, 1, 1, 1, -1, -1, -1};
long long dy[8] = {1, -1, 0, -1, 1, 1, 0, -1};
map<pair<long long, long long>, long long> m, fix;
int main() {
scanf("%I64d %I64d %I64d %I64d", &stx, &sty, &fnx, &fny);
scanf("%I64d", &n);
for (i = 0; i < n; i++) {
scanf("%I64d %I64d %I64d", &a, &b, &c);
for (j = b; j <= c; j++) m[make_pair(a, j)] = 1;
}
q.push((node){stx, sty, 0});
fix[make_pair(stx, sty)] = 1;
ans = 1000000000;
while (!q.empty()) {
k = q.front();
q.pop();
fix[make_pair(xx, yy)] = 1;
xx = k.x;
yy = k.y;
ind = k.num;
if (xx == fnx && yy == fny) {
ans = min(ans, ind);
break;
}
for (i = 0; i < 8; i++)
if (fix[make_pair(dx[i] + xx, dy[i] + yy)] != 1 &&
m[make_pair(dx[i] + xx, dy[i] + yy)] == 1) {
q.push((node){dx[i] + xx, dy[i] + yy, ind + 1});
fix[make_pair(dx[i] + xx, dy[i] + yy)] = 1;
}
}
if (ans == 1000000000)
cout << -1 << endl;
else
cout << ans << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[8] = {1, 0, -1, 0, -1, 1, -1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
set<pair<int, int>> pnts;
int main() {
long long x, y, a, b, n;
cin >> x >> y >> a >> b >> n;
for (int i = 0; i < (int)(n); ++i) {
int r, a, b;
cin >> r >> a >> b;
for (int j = (a); j < (int)(b + 1); ++j) pnts.insert({r, j});
}
map<pair<int, int>, int> len;
queue<pair<int, int>> q;
pair<int, int> s;
s = {x, y};
q.push(s);
int dep = 0, sz = 1;
for (; !q.empty(); ++dep, sz = q.size()) {
while (sz--) {
pair<int, int> g;
g = q.front();
q.pop();
pair<int, int> n;
for (int i = 0; i < 8; i++) {
n.first = g.first + dx[i];
n.second = g.second + dy[i];
if (pnts.count(n) == 0 || len.count(n) != 0) continue;
len[n] = dep + 1;
q.push(n);
if (n.first == a && n.second == b) {
cout << dep + 1;
return 0;
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> nodeSet;
set<pair<int, int>> alreadyTaken;
queue<pair<int, int>> que;
queue<int> dist;
int x[] = {1, 1, 1, 0, 0, -1, -1, -1};
int y[] = {-1, 0, 1, -1, 1, -1, 0, 1};
pair<int, int> currentNode;
pair<int, int> node;
int currentDistance;
int BFS(pair<int, int> start, pair<int, int> end) {
que.push(start);
dist.push(0);
alreadyTaken.insert(start);
while (!que.empty()) {
currentNode = que.front();
que.pop();
currentDistance = dist.front(), dist.pop();
if (currentNode == end) {
return currentDistance;
}
for (int i = 0; i < 8; i++) {
node.first = currentNode.first + x[i];
node.second = currentNode.second + y[i];
if ((nodeSet.find(node) != nodeSet.end()) &&
(alreadyTaken.find(node) == alreadyTaken.end())) {
alreadyTaken.insert(node);
que.push(node);
dist.push(currentDistance + 1);
}
}
}
return -1;
}
int main() {
pair<int, int> start, end;
pair<int, int> node;
int row, f, e, n;
cin >> start.first >> start.second >> end.first >> end.second;
cin >> n;
while (n--) {
cin >> row >> f >> e;
node.first = row;
for (int j = f; j <= e; j++) {
node.second = j;
if (nodeSet.find(node) == nodeSet.end()) nodeSet.insert(node);
}
}
cout << BFS(start, end) << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> M;
map<pair<int, int>, int>::iterator it, it1;
vector<int> graph[100005];
int dis[100005], visited[100005];
int main() {
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int m;
cin >> m;
long long a, b, c;
int cnt = 0;
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
for (int j = b; j <= c; j++) {
it = M.find(make_pair(a, j));
if (it == M.end()) M[make_pair(a, j)] = (++cnt);
}
}
int x, y, z, t;
for (it = M.begin(); it != M.end(); ++it) {
x = it->first.first;
y = it->first.second;
t = it->second;
it1 = M.find(make_pair(x - 1, y - 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x - 1, y));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x - 1, y + 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x, y - 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x, y + 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x + 1, y - 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x + 1, y));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
it1 = M.find(make_pair(x + 1, y + 1));
if (it1 != M.end()) {
z = it1->second;
graph[t].push_back(z);
}
}
queue<int> q;
int src, dst;
src = M[make_pair(x1, y1)];
dst = M[make_pair(x2, y2)];
q.push(src);
visited[src] = 1;
dis[src] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < graph[u].size(); i++) {
if (!visited[graph[u][i]]) {
visited[graph[u][i]] = 1;
dis[graph[u][i]] = dis[u] + 1;
q.push(graph[u][i]);
}
}
}
if (dis[dst] == 0)
cout << -1;
else
cout << dis[dst];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
long long x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
pair<long long, long long> s1, d1;
s1 = make_pair(x1, y1);
d1 = make_pair(x2, y2);
long long n;
cin >> n;
set<pair<long long, long long> > s;
for (long long i = 0; i < n; i++) {
long long r, a, b;
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) {
s.insert(make_pair(r, j));
}
}
long long t[3] = {1, -1, 0};
queue<pair<long long, long long> > q;
q.push(s1);
map<pair<long long, long long>, long long> m;
m[s1] = 0;
while (!q.empty()) {
pair<long long, long long> temp;
temp = q.front();
q.pop();
for (long long i = 0; i < 3; i++) {
for (long long j = 0; j < 3; j++) {
if (i == 2 && j == 2) continue;
pair<long long, long long> w;
w.first = temp.first + t[i];
w.second = temp.second + t[j];
if (s.find(w) != s.end() && m.find(w) == m.end()) {
m[w] = m[temp] + 1;
q.push(w);
}
}
}
}
if (m.find(d1) != m.end()) {
cout << m[d1];
return 0;
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int r, a, b, i, n, j, u, v;
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
set<pair<int, int> > mp;
for (i = 0; i < n; i++) {
cin >> r >> a >> b;
for (j = a; j < b + 1; j++) mp.insert(pair<int, int>(r, j));
}
int dx[] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy[] = {1, 1, 0, -1, -1, -1, 0, 1};
queue<pair<int, pair<int, int> > > q;
int ans = 0;
bool flag = false;
q.push(make_pair(ans, pair<int, int>(x0, y0)));
mp.erase(mp.find(pair<int, int>(x0, y0)));
while (!q.empty()) {
pair<int, pair<int, int> > x = q.front();
q.pop();
if (x.second.first == x1 && x.second.second == y1) {
flag = true;
cout << x.first << endl;
break;
}
for (i = 0; i < 8; i++) {
ans = x.first;
u = x.second.first + dx[i];
v = x.second.second + dy[i];
if (mp.find(pair<int, int>(u, v)) != mp.end()) {
q.push(make_pair(ans + 1, pair<int, int>(u, v)));
mp.erase(mp.find(pair<int, int>(u, v)));
}
}
}
if (flag == false) cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e9;
const long long N = 1e5 + 1;
map<pair<int, int>, int> cost;
queue<pair<int, int>> q;
set<pair<int, int>> pts;
int xi, xf, yi, yf, r, a, b, n;
int dx[8] = {1, 0, -1, 0, -1, 1, -1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> xi >> yi >> xf >> yf >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
pts.insert({r, i});
}
}
q.push({xi, yi});
cost[{xi, yi}] = 0;
while (!q.empty()) {
int r = q.front().first;
int c = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int nx = r + dx[i];
int ny = c + dy[i];
if (pts.find({nx, ny}) != pts.end() && cost[{nx, ny}] == 0) {
q.push({nx, ny});
cost[{nx, ny}] = cost[{r, c}] + 1;
}
if (nx == xf && ny == yf) {
cout << cost[{nx, ny}];
return 0;
}
}
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
constexpr double pi = 3.14159265358979323846;
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
map<pair<long long, long long>, long long> bound;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
while (n--) {
long long r, a, b;
cin >> r >> a >> b;
for (; a <= b; ++a) {
bound[{r, a}] = -1;
}
}
queue<pair<long long, long long>> q;
q.push({x0, y0});
bound[{x0, y0}] = 0;
while (!q.empty()) {
long long x = q.front().first;
long long y = q.front().second;
pair<long long, long long> u = {x, y};
q.pop();
for (int i(0); i < 8; ++i) {
long long mx = x + dx[i];
long long my = y + dy[i];
pair<long long, long long> p = {mx, my};
if (bound.count(p) && bound[p] == -1) {
bound[p] = bound[u] + 1;
q.push(p);
}
}
}
cout << bound[{x1, y1}];
cout << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long sr, sc, er, ec, a, b, n, r;
map<long long, vector<pair<long long, long long>>> m;
pair<pair<long long, long long>, long long> test;
map<pair<long long, long long>, bool> vis;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
bool valid(long long a, long long b) { return (a > 0 && b > 0); }
long long bfs() {
queue<pair<pair<long long, long long>, long long>> q;
q.push({{sr, sc}, 0});
vis[{sr, sc}] = 1;
while (!q.empty()) {
test = q.front();
q.pop();
long long l, r;
l = test.first.first;
r = test.first.second;
for (int i = 0; i < 8; ++i) {
if (vis[{l + dx[i], r + dy[i]}] || !valid(l + dx[i], r + dy[i])) continue;
if (l + dx[i] == er && r + dy[i] == ec) return test.second + 1;
for (auto it : m[l + dx[i]]) {
if (r + dy[i] >= it.first && r + dy[i] <= it.second) {
vis[{l + dx[i], r + dy[i]}] = 1;
q.push({{l + dx[i], r + dy[i]}, test.second + 1});
break;
}
}
}
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
cin >> sr >> sc >> er >> ec;
cin >> n;
while (n--) {
cin >> r >> a >> b;
m[r].push_back({a, b});
}
cout << bfs();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
long long power(long long x, long long y) {
long long res = 1LL;
x = x % 1000000007;
while (y > 0) {
if (y & 1) res = (res * x) % 1000000007;
y = y >> 1;
x = (x * x) % 1000000007;
}
return res % 1000000007;
}
long long inv(long long n) { return power(n, 1000000007 - 2) % 1000000007; }
long long isprime(long long n) {
if (n < 2) return 0;
long long i;
for (i = 2; i * i <= n; i++)
if (n % i == 0) return 0;
return 1;
}
void files() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
}
long long egcd(long long a, long long b, long long &x, long long &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
long long x1, y1;
long long g = egcd(b, a % b, x1, y1);
x = y1;
y = x1 - (a / b) * y1;
return g;
}
const long long N = 2e5 + 10;
long long p, q, r, s, j;
long long dx[] = {0, 0, 1, 1, -1, -1, 1, -1};
long long dy[] = {1, -1, 1, -1, 1, -1, 0, 0};
map<long long, map<long long, long long>> mp, dis, vis;
void solve() {
cin >> p >> q >> r >> s;
long long n;
long long i, r1, c1, c2;
cin >> n;
for (i = 1; i <= n; i++) {
cin >> r1 >> c1 >> c2;
for (j = c1; j <= c2; j++) {
mp[r1][j] = 1;
dis[r1][j] = 1e17;
vis[r1][j] = 0;
}
}
queue<pair<long long, long long>> qi;
qi.push({p, q});
vis[p][q] = 1;
dis[p][q] = 0;
while (qi.size()) {
auto f = qi.front();
qi.pop();
long long x, y;
x = f.first;
y = f.second;
for (i = 0; i < 8; i++) {
long long nx, ny;
nx = x + dx[i];
ny = y + dy[i];
if (vis[nx][ny] == 0 && mp[nx][ny]) {
vis[nx][ny] = 1;
dis[nx][ny] = 1 + dis[x][y];
qi.push({nx, ny});
}
}
}
if (!(dis[r][s] < 1e17)) dis[r][s] = -1;
cout << dis[r][s] << "\n";
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long t = 1;
while (t--) {
solve();
}
}
|
#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:100000000,100000000")
using namespace std;
const long long inf = 1e18 + 7;
const long long mod = 1e9 + 7;
const double eps = 1e-10;
const double PI = 2 * acos(0.0);
const double E = 2.71828;
int main(void) {
long long x0, y0, x1, oh_my_humble_y1;
cin >> x0 >> y0;
cin >> x1 >> oh_my_humble_y1;
map<long long, map<long long, long long> > M;
long long n;
cin >> n;
for (long long(i) = 0; (i) < (long long)(n); (i)++) {
long long r1, a, b;
cin >> r1 >> a >> b;
for (long long i = a; i <= b; i++) {
M[r1][i] = 1;
}
}
map<long long, map<long long, long long> > dist, was;
queue<pair<long long, long long> > Q;
long long dx[] = {0, 0, 1, -1, -1, 1, 1, -1};
long long dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
Q.push(make_pair(x0, y0));
dist[x0][y0] = 0;
was[x0][y0] = 1;
while (!Q.empty()) {
long long x = Q.front().first, y = Q.front().second;
Q.pop();
if (x == x1 && y == oh_my_humble_y1) cout << dist[x][y] << endl, exit(0);
for (long long(i) = 0; (i) < (long long)(8); (i)++) {
long long X = x + dx[i], Y = y + dy[i];
if (X > 0 && X <= 1e9 && Y > 0 && Y <= 1e9 && M[X][Y] == 1 &&
was[X][Y] == 0) {
dist[X][Y] = dist[x][y] + 1;
was[X][Y] = 1;
Q.push(make_pair(X, Y));
}
}
}
cout << -1 << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long OO = (long long)1e18 + 9;
const int oo = 2147483647;
const double EPS = 1e-9;
const double PI = acos(-1.0);
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int dX[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dY[] = {0, 1, 0, -1, 1, 1, -1, -1};
char letters[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int dot(int x, int y, int x2, int y2) { return x * x2 + y * y2; }
long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); }
int ord(int r, int c, int nCol) { return (nCol * r) + c + 1; }
int cmpDouble(long double d1, long double d2) {
return fabs(d1 - d2) <= EPS ? 0 : d1 > d2 ? 1 : -1;
}
int main() {
int ans = 0, r, c1, c2, n, x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1 >> n;
set<pair<int, int> > st;
for (int i = 0; i < n; i++) {
cin >> r >> c1 >> c2;
for (int i = c1; i < c2 + 1; i++) st.insert(make_pair(r, i));
}
map<pair<int, int>, int> mp;
queue<pair<int, int> > q;
q.push(make_pair(x0, y0));
int dep = 0, sz = 1;
pair<int, int> cur = make_pair(x0, y0);
bool ok = true;
mp[cur] = 1;
for (; ok && !q.empty(); ++dep, sz = q.size()) {
while (ok && sz--) {
cur = q.front(), q.pop();
for (int i = 0; i < 8; i++)
if (st.count(make_pair(cur.first + dX[i], cur.second + dY[i])) &&
!mp[make_pair(cur.first + dX[i], cur.second + dY[i])]) {
q.push(make_pair(cur.first + dX[i], cur.second + dY[i])),
mp[make_pair(cur.first + dX[i], cur.second + dY[i])] = 1,
ans = max(ans,
dep + 1);
if (cur.first + dX[i] == x1 && cur.second + dY[i] == y1) {
ok = false;
break;
}
}
}
}
if (ok)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
|
#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;
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;
}
}
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}) && !cost.count({x, y})) {
cost[{x, y}] = cost[cn] + 1;
q.push({x, y});
}
}
}
if (!cost.count({desx, desy}))
cout << -1 << "\n";
else
cout << cost[{desx, desy}] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INF = (int)1e9 + 7;
const int MXN = (int)1e5 + 7;
int sx, sy, fx, fy, n;
int dx[] = {1, 1, 1, -1, -1, -1, 0, 0}, dy[] = {-1, 0, 1, -1, 0, 1, 1, -1};
map<pair<int, int>, bool> ok, u;
bool check(int x, int y) {
return x <= 1e9 && y <= 1e9 && x > 0 && y > 0 && ok[make_pair(x, y)] &&
!u[make_pair(x, y)];
}
int main() {
scanf("%d%d%d%d", &sx, &sy, &fx, &fy);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
for (int pos = y; pos <= z; pos++) ok[make_pair(x, pos)] = true;
}
queue<int> moves;
queue<pair<int, int> > q;
moves.push(0);
q.push(make_pair(sx, sy));
if (!check(sx, sy)) {
puts("-1");
return 0;
}
u[make_pair(sx, sy)] = true;
while (!q.empty()) {
int cx = q.front().first;
int cy = q.front().second;
int ca = moves.front();
if (cx == fx && cy == fy) {
printf("%d", ca);
return 0;
}
q.pop();
moves.pop();
for (int i = 0; i < 8; i++) {
int nx = cx + dx[i];
int ny = cy + dy[i];
if (check(nx, ny)) {
u[make_pair(nx, ny)] = true;
q.push(make_pair(nx, ny));
moves.push(ca + 1);
}
}
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct Node {
int r, c, dis;
};
int sr, sc, dr, dc, n;
int ri, ai, bi;
map<int, vector<pair<int, int> > > m;
map<int, map<int, bool> > visited;
bool isValid(int x, int y) {
bool segOk = false;
for (int i = 0; i < m[x].size(); i++) {
if (m[x][i].first <= y && m[x][i].second >= y) {
segOk = true;
break;
}
}
return x >= 1 && y >= 1 && x <= 1000000000 && y <= 1000000000 && segOk && y &&
!visited[x][y];
}
int moveC[] = {1, -1, 0, 0, 1, -1, 1, -1};
int moveR[] = {0, 0, 1, -1, 1, 1, -1, -1};
int bfs() {
queue<Node> q;
Node s;
s.r = sr;
s.c = sc;
s.dis = 0;
q.push(s);
while (!q.empty()) {
Node t = q.front();
if (t.r == dr && t.c == dc) return t.dis;
for (int x = 0; x < 8; x++) {
if (isValid(t.r + moveR[x], t.c + moveC[x])) {
Node r;
r.r = t.r + moveR[x];
r.c = t.c + moveC[x];
r.dis = t.dis + 1;
visited[r.r][r.c] = true;
q.push(r);
}
}
q.pop();
}
return -1;
}
int main() {
cin >> sr >> sc >> dr >> dc;
cin >> n;
for (int x = 0; x < n; x++) {
cin >> ri >> ai >> bi;
m[ri].push_back(make_pair(ai, bi));
}
cout << bfs();
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int x, y, x2, y2, n, row, l, r;
map<int, vector<pair<int, int> > > mp;
map<pair<int, int>, bool> vis;
bool okay(int fi, int se) {
for (int i = 0; i < mp[fi].size(); i++) {
if (mp[fi][i].first <= se && mp[fi][i].second >= se) return 1;
}
return 0;
}
int bfs() {
queue<pair<int, int> > q;
pair<int, int> cur;
q.push({x, y});
int steps = 0, siz = 1;
while (!q.empty()) {
siz = q.size();
while (siz--) {
cur = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int tox = cur.first + dx[i];
int toy = cur.second + dy[i];
if (okay(tox, toy)) {
if (tox == x2 && toy == y2) return steps + 1;
if (!vis[{tox, toy}]) q.push({tox, toy});
vis[{tox, toy}] = 1;
}
}
}
steps++;
}
return -1;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
;
cin >> x >> y >> x2 >> y2 >> n;
for (int i = 0; i < n; i++) {
cin >> row >> l >> r;
mp[row].push_back({l, r});
}
int ans = bfs();
if (ans == -1)
cout << -1 << endl;
else
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int INF = 2000000000;
double EPS = 1e-8;
int dist(int x0, int y0, int x1, int y1) {
return max(x0, x1) - min(x0, x1) + max(y0, y1) - min(y0, y1);
}
int dx[8] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[8] = {1, -1, 0, 0, 1, -1, -1, 1};
struct P {
int c, y, x;
P(int c, int y, int x) : c(c), y(y), x(x) {}
bool operator<(const P &p) const { return c > p.c; }
};
int main() {
int x0, y0, x1, y1;
scanf("%d %d %d %d", &y0, &x0, &y1, &x1);
if (dist(y0, x0, y1, x1) > 100000) {
puts("-1");
return 0;
}
int n;
scanf("%d", &n);
vector<int> r(n), a(n), b(n);
int h = INF, H = 0, w = INF, W = 0;
for (int i = 0; i < n; ++i) {
scanf("%d %d %d", &r[i], &a[i], &b[i]);
w = min(w, a[i]);
W = max(W, b[i]);
h = min(h, r[i]);
H = max(H, r[i]);
}
map<int, map<int, bool> > s;
for (int i = 0; i < n; ++i) {
for (int j = a[i]; j < b[i] + 1; ++j) s[r[i] - h][j - w] = 1;
}
y0 -= h, x0 -= w, y1 -= h, x1 -= w;
priority_queue<P> q;
q.push(P(0, y0, x0));
while (!q.empty()) {
P p = q.top();
q.pop();
if (p.y == y1 && p.x == x1) {
printf("%d\n", p.c);
return 0;
}
for (int i = 0; i < 8; ++i) {
int ny = p.y + dy[i];
int nx = p.x + dx[i];
if (ny >= 0 && ny <= H - h && nx >= 0 && nx <= W - w) {
if (s[ny][nx]) {
q.push(P(p.c + 1, ny, nx));
s[ny][nx] = 0;
}
}
}
}
puts("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool mark[100001];
int X[100001], Y[100001];
map<pair<int, long long>, int> mp;
int d[8][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0},
{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
int main() {
pair<int, long long> s, e;
cin >> s.first >> s.second >> e.first >> e.second;
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) {
X[mp.size()] = r;
Y[mp.size()] = j;
mp.insert(make_pair(pair<int, long long>(r, j), mp.size()));
}
}
memset(mark, false, sizeof mark);
int t = mp[s], end = mp[e];
queue<pair<int, long long> > q;
q.push(pair<int, long long>(t, 0));
mark[t] = true;
long long ans = -1;
while (!q.empty()) {
t = q.front().first;
long long dis = q.front().second;
q.pop();
if (t == end) ans = dis;
for (int i = 0; i < 8; ++i) {
int nx = X[t] + d[i][0], ny = Y[t] + d[i][1];
pair<int, long long> nn = pair<int, long long>(nx, ny);
if (mp.find(nn) != mp.end()) {
int idx = mp[nn];
if (mark[idx] == false) {
q.push(pair<int, long long>(idx, dis + 1));
mark[idx] = true;
}
}
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int INFI = (1 << 30);
const long long INFL = (1LL << 62);
const long long md = 1000000007;
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
map<int, map<int, bool> > m;
for (int i = 1; i <= n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) m[r][j] = 1;
}
queue<int> q1, q2, q3;
q1.push(x0);
q2.push(y0);
q3.push(0);
m[x0][y0] = 0;
int ve[8][2] = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1},
{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
while (!q1.empty()) {
int x = q1.front();
int y = q2.front();
int s = q3.front();
q1.pop();
q2.pop();
q3.pop();
for (int q = 0; q < 8; q++) {
if (x + ve[q][0] > 0 && y + ve[q][1] > 0 &&
m[x + ve[q][0]][y + ve[q][1]] == 1) {
m[x + ve[q][0]][y + ve[q][1]] = 0;
if (x + ve[q][0] == x1 && y + ve[q][1] == y1) {
cout << s + 1;
return 0;
}
q1.push(x + ve[q][0]);
q2.push(y + ve[q][1]);
q3.push(s + 1);
}
}
}
cout << -1;
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;
bool valid(map<int, vector<pair<int, bool>>>& sm, int r, int c) {
const vector<pair<int, bool>>& v = sm[r];
int N = v.size();
if (N == 0) {
return false;
}
int pos = -1;
for (int 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;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(0);
int x0, y0, x1, y1, N;
cin >> x0 >> y0 >> x1 >> y1 >> N;
map<int, vector<pair<int, bool>>> fm;
for (int i = 1; i <= N; ++i) {
int r, a, b;
cin >> r >> a >> b;
fm[r].push_back({a, true});
fm[r].push_back({b, false});
}
map<int, vector<pair<int, bool>>> sm;
for (auto& it : fm) {
int r = it.first;
vector<pair<int, bool>>& v = it.second;
auto cmp = [](const pair<int, bool>& a, const pair<int, bool>& b) {
if (a.first == b.first) {
return a.second > b.second;
}
return a.first < b.first;
};
sort(v.begin(), v.end(), cmp);
int start = -1, num = 0;
for (auto p : v) {
if (p.second == true) {
++num;
if (num == 1) {
start = p.first;
}
} else {
--num;
if (num == 0) {
sm[r].push_back({start, true});
sm[r].push_back({p.first, false});
}
}
}
}
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 (!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;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
map<pair<int, int>, bool> Allowed;
int n;
cin >> n;
while (n--) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) Allowed[{r, i}] = true;
}
Allowed[{x1, y1}] = true;
Allowed[{x2, y2}] = true;
map<pair<int, int>, int> ans;
queue<pair<int, int>> que;
que.push({x1, y1});
ans[{x1, y1}] = 0;
while (!que.empty()) {
int x = que.front().first, y = que.front().second;
que.pop();
for (int i = 0; i < 8; i++) {
int newX = x + dx[i], newY = y + dy[i];
if (Allowed[{newX, newY}] && !ans[{newX, newY}]) {
que.push({newX, newY});
ans[{newX, newY}] = ans[{x, y}] + 1;
}
}
}
if (!ans[{x2, y2}])
cout << -1;
else
cout << ans[{x2, y2}];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, int> m1;
vector<int> v[100010];
int fx[] = {-1, 0, -1, -1};
int fy[] = {0, -1, 1, -1};
struct ss {
int r;
int c1;
int c2;
};
ss st[100010];
bool cmp(ss a, ss b) {
if (a.r != b.r)
return a.r < b.r;
else {
if (a.c1 != b.c1)
return a.c1 < b.c1;
else
return a.c2 < b.c2;
}
}
bool vis[100010];
int dis[100010];
int bfs(int s, int d) {
memset(vis, 0, sizeof(vis));
memset(dis, -1, sizeof(dis));
vis[s] = 1;
dis[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = 0; i < v[u].size(); i++) {
int j = v[u][i];
if (!vis[j]) {
vis[j] = 1;
dis[j] = dis[u] + 1;
if (j == d) return dis[j];
q.push(j);
}
}
}
return -1;
}
int main() {
int sx, sy, dx, dy;
scanf("%d%d", &sx, &sy);
scanf("%d%d", &dx, &dy);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
int r, c1, c2;
scanf("%d%d%d", &r, &c1, &c2);
st[i].r = r;
st[i].c1 = c1;
st[i].c2 = c2;
}
sort(st, st + n, cmp);
int ct = 1;
for (int i = 0; i < n; i++) {
int row = st[i].r;
for (int j = st[i].c1; j <= st[i].c2; j++) {
int col = j;
if (m1[make_pair(row, col)] > 0) continue;
m1[make_pair(row, col)] = ct;
for (int k = 0; k < 4; k++) {
int x = row + fx[k];
int y = col + fy[k];
int cnt = m1[make_pair(x, y)];
if (cnt > 0) {
v[ct].push_back(cnt);
v[cnt].push_back(ct);
}
}
ct++;
}
}
int s = m1[make_pair(sx, sy)];
int d = m1[make_pair(dx, dy)];
int ans = bfs(s, d);
printf("%d", ans);
}
|
#include <bits/stdc++.h>
using namespace std;
int b1, b2, s1, s2, n, yol[10][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1},
{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
map<pair<int, int>, int> mp;
queue<pair<int, int>> q;
int main() {
scanf("%d %d %d %d", &b1, &b2, &s1, &s2);
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int a, b, r;
scanf("%d %d %d", &r, &a, &b);
for (int j = a; j <= b; j++) {
mp[{r, j}] = -1;
}
}
mp[{b1, b2}] = 0;
q.push({b1, b2});
while (!q.empty()) {
pair<int, int> pt = q.front();
q.pop();
if (pt.first == s1 && pt.second == s2) {
printf("%d", mp[pt]);
return 0;
}
for (int i = 0; i < 8; i++) {
if (mp[{pt.first + yol[i][0], pt.second + yol[i][1]}] == -1) {
mp[{pt.first + yol[i][0], pt.second + yol[i][1]}] = mp[pt] + 1;
q.push({pt.first + yol[i][0], pt.second + yol[i][1]});
}
}
}
printf("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int sx, sy, dx, dy, n, dis[100004];
map<pair<int, int>, int> mp;
int main() {
cin >> sx >> sy >> dx >> dy;
cin >> n;
int i, j, cnt = 1;
for (i = 0; i < n; i++) {
int r, c1, c2;
cin >> r >> c1 >> c2;
for (j = c1; j <= c2; j++) {
mp[{r, j}] = cnt;
dis[cnt] = 1000000000;
cnt++;
}
}
mp[{dx, dy}] = cnt;
dis[cnt] = 1000000000;
queue<pair<int, int> > q;
q.push({sx, sy});
dis[mp[{sx, sy}]] = 0;
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
if (mp.find({x + 1, y}) != mp.end() &&
dis[mp[{x + 1, y}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x + 1, y}]] = dis[mp[{x, y}]] + 1;
q.push({x + 1, y});
}
if (mp.find({x - 1, y}) != mp.end() &&
dis[mp[{x - 1, y}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x - 1, y}]] = dis[mp[{x, y}]] + 1;
q.push({x - 1, y});
}
if (mp.find({x, y + 1}) != mp.end() &&
dis[mp[{x, y + 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x, y + 1}]] = dis[mp[{x, y}]] + 1;
q.push({x, y + 1});
}
if (mp.find({x, y - 1}) != mp.end() &&
dis[mp[{x, y - 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x, y - 1}]] = dis[mp[{x, y}]] + 1;
q.push({x, y - 1});
}
if (mp.find({x + 1, y + 1}) != mp.end() &&
dis[mp[{x + 1, y + 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x + 1, y + 1}]] = dis[mp[{x, y}]] + 1;
q.push({x + 1, y + 1});
}
if (mp.find({x - 1, y - 1}) != mp.end() &&
dis[mp[{x - 1, y - 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x - 1, y - 1}]] = dis[mp[{x, y}]] + 1;
q.push({x - 1, y - 1});
}
if (mp.find({x + 1, y - 1}) != mp.end() &&
dis[mp[{x + 1, y - 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x + 1, y - 1}]] = dis[mp[{x, y}]] + 1;
q.push({x + 1, y - 1});
}
if (mp.find({x - 1, y + 1}) != mp.end() &&
dis[mp[{x - 1, y + 1}]] > dis[mp[{x, y}]] + 1) {
dis[mp[{x - 1, y + 1}]] = dis[mp[{x, y}]] + 1;
q.push({x - 1, y + 1});
}
}
if (dis[mp[{dx, dy}]] == 1000000000) {
cout << -1 << "\n";
} else {
cout << dis[mp[{dx, dy}]] << "\n";
}
}
|
#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 (newx == x1 && newy == y1) {
exist = true;
break;
}
}
}
if (exist) break;
}
if (exist)
cout << vis[{x1, y1}] - 1 << "\n";
else
cout << -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;
mt19937 rnd(time(0));
const double eps = 1e-10;
const long long inf = 0x3f3f3f3f3f3f3f3fLL;
const long long N = 2e5 + 5;
const long long MOD = 1e9 + 7;
long long pw(long long b, long long p) {
b %= MOD;
long long res = 1;
while (p > 0) {
if (p & 1) res = res * b % MOD;
b = b * b % MOD;
p >>= 1;
}
return res;
}
set<pair<long long, long long>> s;
map<pair<long long, long long>, long long> d;
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
long long n, x0, y0, x1, y1, r, p, q;
cin >> x0 >> y0 >> x1 >> y1 >> n;
for (long long i = 0; i < n; i++) {
cin >> r >> p >> q;
for (long long j = p; j <= q; j++) {
s.insert(make_pair(r, j));
}
}
queue<pair<long long, long long>> Q;
Q.push(make_pair(x0, y0));
d[make_pair(x0, y0)] = 0;
while (!Q.empty()) {
long long x = Q.front().first;
long long y = Q.front().second;
long long dist = d[make_pair(x, y)];
Q.pop();
for (long long i = -1; i <= 1; i++) {
for (long long j = -1; j <= 1; j++) {
pair<long long, long long> cur = make_pair(x + i, y + j);
if (d.count(cur) || !s.count(cur)) continue;
Q.push(cur);
d[cur] = dist + 1;
}
}
}
if (!d.count(make_pair(x1, y1)))
cout << -1;
else
cout << d[make_pair(x1, y1)];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
bool isPrime(long long n) {
if (n < 2) return 0;
if (n < 4) return 1;
if (n % 2 == 0 or n % 3 == 0) return 0;
for (long long i = 5; i * i <= n; i += 6)
if (n % i == 0 or n % (i + 2) == 0) return 0;
return 1;
}
long long modexpo(long long x, long long p) {
long long res = 1;
x = x % 998244353;
while (p) {
if (p % 2) res = res * x;
p >>= 1;
x = x * x % 998244353;
res %= 998244353;
}
return res;
}
long long max(long long a, long long b) { return (a > b ? a : b); }
long long min(long long a, long long b) { return (a < b ? a : b); }
struct node {
long long u, v;
node(long long a, long long b) {
u = a;
v = b;
}
};
struct compare {
bool operator()(const node a, const node b) const {
if (a.u == b.u) {
return a.v < b.v;
}
return a.u < b.u;
}
};
map<node, long long, compare> mp, dist, vis;
long long row[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
long long col[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long a, b, c, d;
cin >> a >> b >> c >> d;
long long n;
cin >> n;
for (long long i = 0; i < n; i++) {
long long x, y, z;
cin >> x >> y >> z;
for (long long j = y; j <= z; j++) {
mp[node(x, j)]++;
dist[node(x, j)] = 2000000000000000000;
}
}
queue<node> q;
q.push(node(a, b));
dist[node(a, b)] = 0;
long long flag = 0;
while (!q.empty()) {
auto x = q.front();
q.pop();
long long len = dist[x];
if (x.u == c && x.v == d) {
flag = 1;
break;
}
for (long long i = 0; i < 8; i++) {
long long y = x.u + row[i];
long long z = x.v + col[i];
if (!vis.count(node(y, z)) && mp.count(node(y, z))) {
vis[node(y, z)] = 1;
q.push(node(y, z));
dist[node(y, z)] = len + 1;
}
}
}
if (flag == 0) {
cout << -1 << "\n";
} else {
cout << dist[node(c, d)] << "\n";
}
}
|
#include <bits/stdc++.h>
using namespace std;
int cm[] = {-1, 1, -1, 1, 0, -1, 0, 1};
int rm[] = {-1, 1, 1, -1, -1, 0, 1, 0};
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
map<pair<int, int>, int> m;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
m[{r, j}] = -1;
}
}
queue<pair<int, int>> q;
m[{x0, y0}] = 0;
q.push({x0, y0});
while (not q.empty()) {
auto p = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
pair<int, int> np = {p.first + rm[i], p.second + cm[i]};
if (m.count(np) != 0 && m[np] == -1) {
m[np] = m[p] + 1;
q.push(np);
}
}
}
cout << m[{x1, y1}] << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
int n;
map<pair<int, int>, int> mp;
vector<int> adj[100001];
bool vis[100001];
int dis[100001];
void bfs(int i) {
queue<int> q;
q.push(i);
dis[i] = 0;
vis[i] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto it : adj[v]) {
if (!vis[it]) {
vis[it] = true;
dis[it] = dis[v] + 1;
q.push(it);
}
}
}
}
int main() {
pair<int, int> st, en;
cin >> st.first >> st.second >> en.first >> en.second;
cin >> n;
int j = 0;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int h = a; h <= b; h++) {
if (mp.find(make_pair(r, h)) == mp.end()) {
mp[make_pair(r, h)] = j;
j++;
for (int dx = -1; dx <= 1; dx++)
for (int dy = -1; dy <= 1; dy++) {
if (!(dx == 0 && dy == 0)) {
if (mp.find(make_pair(r + dx, h + dy)) != mp.end()) {
adj[mp[make_pair(r, h)]].push_back(
mp[make_pair(r + dx, h + dy)]);
adj[mp[make_pair(r + dx, h + dy)]].push_back(
mp[make_pair(r, h)]);
}
}
}
}
}
}
bfs(mp[st]);
if (dis[mp[en]] == 0) dis[mp[en]] = -1;
cout << dis[mp[en]];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int MX = 100010;
const int oo = (1 << 31) - 1;
struct cell {
int x, y;
cell(int a, int b) {
x = a;
y = b;
}
bool operator<(const cell &arg) const {
if (x == arg.x) return y < arg.y;
return x < arg.x;
}
cell() { x = y = 0; }
} Cell[MX], way[8];
int dist[MX], nmap;
map<cell, int> mapa;
queue<int> cola;
int main() {
way[0].x = 0, way[0].y = 1;
way[1].x = 1, way[1].y = 0;
way[2].x = 0, way[2].y = -1;
way[3].x = -1, way[3].y = 0;
way[4].x = 1, way[4].y = 1;
way[5].x = -1, way[5].y = -1;
way[6].x = 1, way[6].y = -1;
way[7].x = -1, way[7].y = 1;
int r, a, b, xo, yo, x1, y1, N;
cell aux, next;
scanf("%d %d %d %d", &xo, &yo, &x1, &y1);
scanf("%d", &N);
nmap = 0;
for (int i = 0; i < N; i++) {
scanf("%d %d %d", &r, &a, &b);
for (int j = a; j <= b; j++) {
aux = cell(r, j);
if (mapa.find(aux) == mapa.end()) {
mapa[aux] = nmap;
Cell[nmap] = aux;
dist[nmap] = oo;
nmap++;
}
}
}
dist[mapa[cell(xo, yo)]] = 0;
cola.push(mapa[cell(xo, yo)]);
int ia, in;
while (!cola.empty()) {
ia = cola.front();
aux = Cell[ia];
cola.pop();
for (int i = 0; i < 8; i++) {
next.x = aux.x + way[i].x;
next.y = aux.y + way[i].y;
if (mapa.find(next) != mapa.end()) {
in = mapa[next];
if (dist[in] > dist[ia] + 1) {
dist[in] = dist[ia] + 1;
cola.push(in);
}
}
}
}
r = mapa[cell(x1, y1)];
if (dist[r] == oo)
printf("-1\n");
else
printf("%d\n", dist[r]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int alowed[3] = {0, 1, -1};
int ans = 0;
string getS(int r, int c) {
stringstream stringstream;
stringstream << r;
stringstream << "-";
stringstream << c;
return stringstream.str();
}
pair<int, int> getCoordinate(string str) {
pair<int, int> co;
int i;
for (i = 0; str[i] != '-'; i++) {
co.first *= 10;
co.first += str[i] - '0';
}
i++;
for (; i < str.length(); i++) {
co.second *= 10;
co.second += str[i] - '0';
}
return co;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
unordered_set<string> 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.insert(getS(r, i));
}
}
queue<string> bfs;
string be = getS(x1, y1);
bfs.push(be);
if (!s.count(be)) return cout << -1, 0;
s.erase(be);
while (!bfs.empty()) {
int x = bfs.size();
while (x--) {
string str = bfs.front();
bfs.pop();
pair<int, int> cor = getCoordinate(str);
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];
string s2 = getS(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 MOD = 1e9 + 7;
const long double PI = acos((long double)-1);
int dr[] = {0, 0, -1, -1, -1, 1, 1, 1};
int dc[] = {-1, 1, -1, 0, 1, -1, 0, 1};
void solve() {
int x1, y1, x2, y2, n;
cin >> x1 >> y1 >> x2 >> y2 >> n;
map<pair<int, int>, bool> isValid;
for (int i = 1; i <= n; ++i) {
int r, c1, c2;
cin >> r >> c1 >> c2;
for (int j = c1; j <= c2; ++j) {
isValid[{r, j}] = true;
}
}
map<pair<int, int>, int> dist;
dist[{x1, y1}] = 0;
queue<pair<int, int> > q;
q.push({x1, y1});
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
int currR = p.first, currC = p.second;
for (int i = 0; i < 8; ++i) {
int newR = currR + dr[i];
int newC = currC + dc[i];
if (isValid.count({newR, newC}) && !dist.count({newR, newC})) {
dist[{newR, newC}] = dist[{currR, currC}] + 1;
q.push({newR, newC});
}
}
}
if (dist.count({x2, y2})) {
cout << dist[{x2, y2}] << '\n';
} else {
cout << -1 << '\n';
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int xs, ys, xe, ye;
map<int, map<int, int> > ada, dist, visited;
scanf("%d%d%d%d", &xs, &ys, &xe, &ye);
int n;
scanf("%d", &n);
while (n--) {
int x, dari, ke;
scanf("%d%d%d", &x, &dari, &ke);
for (int(i) = (dari); (i) <= (ke); (i)++) {
ada[x][i] = 1;
dist[x][i] = INT_MAX;
}
}
ada[xe][ye] = 1;
queue<pair<int, int> > pq;
dist[xs][ys] = 0;
pq.push(make_pair(xs, ys));
while (!pq.empty()) {
pair<int, int> now = pq.front();
pq.pop();
int x = now.first, y = now.second;
for (int(i) = (-1); (i) <= (1); (i)++)
for (int(j) = (-1); (j) <= (1); (j)++) {
int xx = x + i, yy = y + j;
if (ada[xx][yy]) {
if (dist[xx][yy] > dist[x][y] + 1) {
dist[xx][yy] = dist[x][y] + 1;
pq.push(make_pair(xx, yy));
}
}
}
}
if (dist[xe][ye] != INT_MAX)
printf("%d", dist[xe][ye]);
else
printf("-1");
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> mp;
map<pair<int, int>, int> mp2;
queue<pair<int, int> > q;
pair<int, int> pi;
pair<int, int> pe;
void bfs() {
q.push(pi);
int length = 0;
while (q.size()) {
length++;
int sizee = q.size();
while (sizee--) {
pair<int, int> i;
i = q.front();
q.pop();
if (mp[{i.first - 1, i.second - 1}] == 1) {
mp[{i.first - 1, i.second - 1}] = 0;
q.push({i.first - 1, i.second - 1});
mp2[{i.first - 1, i.second - 1}] = length;
}
if (mp[{i.first - 1, i.second}] == 1) {
mp[{i.first - 1, i.second}] = 0;
q.push({i.first - 1, i.second});
mp2[{i.first - 1, i.second}] = length;
}
if (mp[{i.first - 1, i.second + 1}] == 1) {
mp[{i.first - 1, i.second + 1}] = 0;
q.push({i.first - 1, i.second + 1});
mp2[{i.first - 1, i.second + 1}] = length;
}
if (mp[{i.first, i.second - 1}] == 1) {
mp[{i.first, i.second - 1}] = 0;
q.push({i.first, i.second - 1});
mp2[{i.first, i.second - 1}] = length;
}
if (mp[{i.first, i.second + 1}] == 1) {
mp[{i.first, i.second + 1}] = 0;
q.push({i.first, i.second + 1});
mp2[{i.first, i.second + 1}] = length;
}
if (mp[{i.first + 1, i.second - 1}] == 1) {
mp[{i.first + 1, i.second - 1}] = 0;
q.push({i.first + 1, i.second - 1});
mp2[{i.first + 1, i.second - 1}] = length;
}
if (mp[{i.first + 1, i.second + 1}] == 1) {
mp[{i.first + 1, i.second + 1}] = 0;
q.push({i.first + 1, i.second + 1});
mp2[{i.first + 1, i.second + 1}] = length;
}
if (mp[{i.first + 1, i.second}] == 1) {
mp[{i.first + 1, i.second}] = 0;
q.push({i.first + 1, i.second});
mp2[{i.first + 1, i.second}] = length;
}
}
}
}
int main() {
cin >> pi.first >> pi.second;
cin >> pe.first >> pe.second;
int x;
cin >> x;
while (x--) {
int a, b, c;
cin >> a >> b >> c;
for (int w = b; w <= c; w++) mp[{a, w}] = 1;
}
bfs();
if (mp2[pe] == 0) {
cout << "-1" << endl;
} else {
cout << mp2[pe] << endl;
}
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() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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;
pair<int, int> st, en;
int n;
set<pair<int, int> > vis;
set<pair<int, int> > ok;
queue<pair<int, int> > q;
map<pair<int, int>, int> ans;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> st.first >> st.second >> en.first >> en.second;
cin >> n;
int r, a, b;
for (int i = (0); i < (n); ++i) {
cin >> r >> a >> b;
for (int j = (a); j < (b + 1); ++j) ok.insert({r, j});
}
q.push(st);
pair<int, int> cur;
bool done = 0;
while (!q.empty()) {
cur = q.front();
q.pop();
if (cur == en) {
cout << ans[cur] << '\n';
done = 1;
break;
}
for (int i = (-1); i < (2); ++i) {
for (int j = (-1); j < (2); ++j) {
pair<int, int> chi = {cur.first + i, cur.second + j};
if (vis.find(chi) != vis.end()) continue;
if (ok.find(chi) == ok.end()) continue;
ans.insert({chi, ans[cur] + 1});
vis.insert(chi);
q.push(chi);
}
}
}
if (!done) cout << -1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
unordered_set<long long> pos;
long long conv(int r, int c) { return r * 1000000000ll + c; }
queue<pair<pair<int, int>, int>> q;
unordered_map<long long, bool> inq;
void chk(int r, int c, int lvl) {
if (pos.count(conv(r, c)) > 0 && !inq[conv(r, c)])
q.push({{r, c}, lvl + 1}), inq[conv(r, c)] = true;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x0, y0, x1, y1, n;
scanf("%d%d%d%d%d", &x0, &y0, &x1, &y1, &n);
x0--, y0--, x1--, y1--;
for (int i = 0; i < n; i++) {
int r, a, b;
scanf("%d%d%d", &r, &a, &b);
r--, a--, b--;
for (int j = a; j <= b; j++) pos.insert(conv(r, j));
}
q.push({{x0, y0}, 0});
while (!q.empty()) {
pair<int, int> cur = q.front().first;
int lvl = q.front().second;
q.pop();
inq[conv(cur.first, cur.second)] = false;
if (cur.first == x1 && cur.second == y1) {
printf("%d\n", lvl);
return 0;
}
if (pos.count(conv(cur.first, cur.second)) == 0) continue;
pos.erase(conv(cur.first, cur.second));
chk(cur.first + 1, cur.second, lvl);
chk(cur.first - 1, cur.second, lvl);
chk(cur.first, cur.second + 1, lvl);
chk(cur.first, cur.second - 1, lvl);
chk(cur.first - 1, cur.second - 1, lvl);
chk(cur.first - 1, cur.second + 1, lvl);
chk(cur.first + 1, cur.second - 1, lvl);
chk(cur.first + 1, cur.second + 1, lvl);
}
printf("-1\n");
}
|
#include <bits/stdc++.h>
using namespace std;
int dx[] = {-1, -1, 1, 1, 0, 1, 0, -1};
int dy[] = {-1, 1, -1, 1, 1, 0, -1, 0};
const int N = 2e5 + 10, M = 1e6 + 10, OOm = 0x3f3f3f3f;
string blank = "";
int xO, yO, xN, yN, n;
map<int, set<int>> segments;
map<pair<int, int>, bool> vis;
bool valid(int x, int y) {
if (segments.find(x) != segments.end())
if (segments[x].count(y)) return true;
return false;
}
int bfs() {
queue<pair<int, pair<int, int>>> q;
q.push({0, {xO, yO}});
while (!q.empty()) {
auto cell = q.front();
q.pop();
for (int i = 0; i < 8; i++) {
int nx = cell.second.first + dx[i];
int ny = cell.second.second + dy[i];
if (valid(nx, ny) && !vis[{nx, ny}]) {
vis[{nx, ny}] = true;
if (nx == xN && ny == yN) return cell.first + 1;
q.push({cell.first + 1, {nx, ny}});
}
}
}
return -1;
}
void solve() {
cin >> xO >> yO >> xN >> yN >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
segments[r].insert(i);
}
}
cout << bfs();
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
cerr << "Time elapsed: " << clock() / 1000 << " ms" << endl;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> st;
set<pair<int, int>> visi;
const int dx[] = {1, 1, 1, 0, -1, -1, -1, 0},
dy[] = {0, 1, -1, 1, 1, 0, -1, -1};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int x0, y0, x1, y1, n, r, a, b, f = 1;
cin >> x0 >> y0 >> x1 >> y1 >> n;
for (auto i = (0); i < (n); i++) {
cin >> r >> a >> b;
for (auto j = (a); j < (b + 1); j++) st.insert({r, j});
}
queue<tuple<int, int, int>> qu;
qu.push({x0, y0, 0});
while (!qu.empty()) {
int t1, t2, t3;
tie(t1, t2, t3) = qu.front();
qu.pop();
if (st.find({t1, t2}) == st.end() || visi.find({t1, t2}) != visi.end())
continue;
visi.insert({t1, t2});
if (t1 == x1 && t2 == y1) {
cout << t3 << '\n';
f = 0;
break;
} else
for (auto i = (0); i < (8); i++)
qu.push({t1 + dx[i], t2 + dy[i], t3 + 1});
}
if (f) cout << "-1\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int x[3], y[3], T, res;
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
map<pair<int, int>, int> Ok, dis, vis;
bool isValid(int x, int y) {
return (x >= 0 && y >= 0 && Ok[make_pair(x, y)] && !vis[make_pair(x, y)]);
}
void bfs(pair<int, int> v) {
queue<pair<int, int>> q;
q.push(v), vis[v] = 1;
while (q.size()) {
int x = q.front().first, y = q.front().second;
q.pop();
for (int i = 0; i < 8; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (isValid(nx, ny))
q.push({nx, ny}), dis[make_pair(nx, ny)] = dis[make_pair(x, y)] + 1,
vis[make_pair(nx, ny)] = 1;
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> x[0] >> y[0] >> x[1] >> y[1] >> T;
while (T--) {
int r, x, y;
cin >> r >> x >> y;
for (int i = x; i <= y; i++) Ok[make_pair(r, i)] = 1;
}
bfs({x[0], y[0]});
res = dis[make_pair(x[1], y[1])];
return cout << (!res ? -1 : res) << endl, 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};
void fast() {
std::ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
}
set<pair<int, int> > Vis;
map<int, vector<pair<int, int> > > allowed;
bool Allow(int r, int Col) {
for (int i = 0; i < allowed[r].size(); i++)
if (Col >= allowed[r][i].first && Col <= allowed[r][i].second) return true;
return false;
}
int main() {
fast();
int startX, startY, endX, endY, N;
cin >> startX >> startY >> endX >> endY;
cin >> N;
while (N--) {
int Row, left, right;
cin >> Row >> left >> right;
allowed[Row].push_back(make_pair(left, right));
}
queue<pair<int, int> > q;
q.push(make_pair(startX, startY));
Vis.insert(make_pair(startX, startY));
int siz = 0, dep = 0;
while (!q.empty()) {
siz = q.size();
while (siz--) {
pair<int, int> Top = q.front();
q.pop();
int X = Top.first;
int Y = Top.second;
if (X == endX && Y == endY) {
cout << dep;
return 0;
}
for (int i = 0; i < 8; i++) {
int newX = X + dx[i];
int newY = Y + dy[i];
if (newX < 1 || newY < 1) continue;
if (Vis.find(make_pair(newX, newY)) == Vis.end() && Allow(newX, newY)) {
Vis.insert(make_pair(newX, newY));
q.push(make_pair(newX, newY));
}
}
}
dep++;
}
cout << -1;
}
|
#include <bits/stdc++.h>
using namespace std;
set<pair<int, int>> pts;
int main() {
std::ios_base::sync_with_stdio(false);
int x1, y1, x2, y2, n;
cin >> x1 >> y1 >> x2 >> y2 >> n;
for (int i = 0; i < (int)(n); ++i) {
int r, a, b;
cin >> r >> a >> b;
for (int j = (a); j < (int)(b + 1); ++j) pts.insert({r, j});
}
map<pair<int, int>, int> len;
queue<pair<int, int>> q;
pair<int, int> s = {x1, y1};
pair<int, int> cur;
q.push(s), len[s] = 0;
enum dir { d, r, u, l, ul, dr, ur, dl };
int dx[8] = {1, 0, -1, 0, -1, 1, -1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
int dep = 0, sz = 1;
for (; !q.empty(); ++dep, sz = q.size()) {
while (sz--) {
cur = q.front(), q.pop();
for (int d = 0; d < 8; ++d) {
int nx = cur.first + dx[d];
int ny = cur.second + dy[d];
pair<int, int> newp = {nx, ny};
if (pts.count(newp) == 0 || len.count(newp) != 0) continue;
q.push(newp), len[newp] = dep + 1;
if (newp.first == x2 && newp.second == y2) {
cout << dep + 1;
return 0;
}
}
}
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
void push(queue<pair<int, int> >& q, int x, int y, int d,
map<int, vector<pair<int, int> > >& v,
map<int, map<int, int> >& dist) {
if (dist[x][y] && dist[x][y] != -1) return;
vector<pair<int, int> >& vv = v[x];
bool flg = false;
for (int i = 0; i < vv.size(); i++)
flg |= vv[i].first <= y && y <= vv[i].second;
if (!flg) return;
dist[x][y] = d;
q.push(make_pair(x, y));
}
int main() {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
map<int, vector<pair<int, int> > > v;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
v[r].push_back(make_pair(a, b));
}
map<int, map<int, int> > dist;
dist[x0][y0] = 0;
dist[x1][y1] = -1;
queue<pair<int, int> > q;
q.push(make_pair(x0, y0));
while (q.size()) {
pair<int, int> c = q.front();
q.pop();
int d = dist[c.first][c.second];
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
if (i || j)
push(q, c.first + i, c.second + j, dist[c.first][c.second] + 1, v,
dist);
}
cout << dist[x1][y1];
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, 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;
const int MOD = 1000000007;
const int N = 1000005;
const double PI = 4 * atan(1);
long long m, x, n, p, k, q, l, r;
long long a, b;
long long x0, y, x2, y2;
map<pair<long long, long long>, bool> isbehy;
map<pair<long long, long long>, bool> vis;
map<pair<long long, long long>, long long> dist;
int dx[] = {1, -1, 1, -1, 0, 0, 1, -1};
int dy[] = {1, -1, -1, 1, 1, -1, 0, 0};
bool ingrid(long long x, long long y) {
return isbehy[make_pair(x, y)] == 1 && vis[make_pair(x, y)] == 0 && x >= 1 &&
x <= 1e9 && y >= 1 && y <= 1e9 && dist[make_pair(x, y)] == 0;
}
long long bfs() {
queue<pair<long long, long long> > q;
q.push(make_pair(x0, y));
pair<long long, long long> ras;
while (!q.empty()) {
ras = q.front();
q.pop();
if (vis[ras]) continue;
vis[ras] = 1;
for (int i = 0; i < 8; i++) {
if (ingrid(ras.first + dx[i], ras.second + dy[i])) {
dist[make_pair(ras.first + dx[i], ras.second + dy[i])] = dist[ras] + 1;
q.push(make_pair(ras.first + dx[i], ras.second + dy[i]));
}
}
}
if (dist[make_pair(x2, y2)] == 0) return -1;
return dist[make_pair(x2, y2)];
}
int main() {
ios::sync_with_stdio(0);
cin >> x0 >> y >> x2 >> y2;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
isbehy[make_pair(r, j)] = 1;
}
}
long long ans = bfs();
cout << ans;
return 0;
}
|
#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};
unordered_map<pair<long long, long long>, long long, hash_pair> mp;
long long srcx, srcy, desx, desy;
unordered_map<pair<long long, long long>, long long, hash_pair> cost;
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;
}
}
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}) && !cost.count({x, y})) {
cost[{x, y}] = cost[cn] + 1;
q.push({x, y});
}
}
}
if (!cost.count({desx, desy}))
cout << -1 << "\n";
else
cout << cost[{desx, desy}] << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
map<long, vector<pair<long, long> > > mp;
int main() {
long x0, y0, x1, y1;
scanf("%ld %ld %ld %ld", &x0, &y0, &x1, &y1);
long n;
scanf("%ld", &n);
long r, a, b;
while (n--) {
scanf("%ld %ld %ld", &r, &a, &b);
mp[r].push_back(make_pair(a, b));
}
queue<pair<long, long> > q;
map<pair<long, long>, bool> v;
map<pair<long, long>, bool> p;
map<pair<long, long>, long> l;
l[make_pair(x0, y0)] = 0;
l[make_pair(x1, y1)] = -1;
q.push(make_pair(x0, y0));
while (!q.empty()) {
long ux = q.front().first;
long uy = q.front().second;
q.pop();
if (v[make_pair(ux, uy)]) {
continue;
}
v[make_pair(ux, uy)] = 1;
long x[8] = {1, -1, 0, 0, 1, 1, -1, -1};
long y[8] = {0, 0, 1, -1, 1, -1, -1, 1};
for (int i = 0; i < 8; i++) {
long adx = ux + x[i];
long ady = uy + y[i];
if (mp.find(adx) != mp.end()) {
long n = mp[adx].size();
for (long j = 0; j < n; j++) {
long a = mp[adx][j].first;
long b = mp[adx][j].second;
if (ady >= a && ady <= b) {
q.push(make_pair(adx, ady));
if (l[make_pair(ux, uy)] + 1 < l[make_pair(adx, ady)] ||
p[make_pair(adx, ady)] == 0) {
l[make_pair(adx, ady)] = l[make_pair(ux, uy)] + 1;
p[make_pair(adx, ady)] = 1;
break;
}
}
}
}
}
}
printf("%ld", l[make_pair(x1, y1)]);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
struct state {
int x, y, cost;
};
int sx, sy, fs, fy, n, r, a, b;
map<pair<int, int>, int> mp, vis;
queue<state> q;
int dxx[] = {1, 0, -1, 0, -1, 1, 1, -1};
int dyy[] = {0, 1, 0, -1, -1, 1, -1, 1};
bool check(int x, int y) { return x > 0 && y > 0; }
int bfs() {
state s;
s.x = sx;
s.y = sy;
s.cost = 0;
q.push(s);
while (!q.empty()) {
state cur = q.front();
int ox = cur.x, oy = cur.y, cc = cur.cost;
q.pop();
if (cur.x == fs && cur.y == fy) return cur.cost;
if (vis.count(make_pair(cur.x, cur.y))) continue;
vis[make_pair(cur.x, cur.y)] = 1;
for (int i = 0; i < 8; i++) {
int nx = ox + dxx[i], ny = oy + dyy[i];
if (check(nx, ny) && vis.count(make_pair(nx, ny)) == 0 &&
mp.count(make_pair(nx, ny))) {
state next;
next.x = nx;
next.y = ny;
next.cost = cur.cost + 1;
q.push(next);
}
}
}
return -1;
}
int main() {
cin >> sx >> sy >> fs >> fy >> n;
for (int i = 0; i < n; i++) {
cin >> r >> a >> b;
for (int j = a; j <= b; j++) mp[make_pair(r, j)] = 1;
}
printf("%d\n", bfs());
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, long long> allowed;
queue<pair<long long, long long> > q;
long long dx[] = {1, 1, 1, -1, -1, -1, 0, 0};
long long dy[] = {1, 0, -1, 1, 0, -1, 1, -1};
int main() {
long long n, x0, y0, x1, y1, r, a, b;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) {
allowed[make_pair(r, j)] = -1;
}
}
allowed[make_pair(x0, y0)] = 0;
q.push(make_pair(x0, y0));
while (!q.empty()) {
pair<long long, long long> u = q.front();
q.pop();
for (long long i = 0; i < 8; i++) {
pair<long long, long long> v =
pair<long long, long long>(u.first + dx[i], u.second + dy[i]);
if (allowed.count(v) && allowed[v] == -1) {
allowed[v] = allowed[u] + 1;
q.push(v);
}
}
}
cout << allowed[pair<long long, long long>(x1, y1)];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 2e18;
const int N = 521234;
int n, m;
map<pair<int, int>, int> mp;
int r[N], a[N], b[N];
struct Edge {
int v, next;
} edge[N * 2];
int tot;
int head[N];
void init() {
tot = 0;
memset(head, -1, sizeof(head));
}
int mx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
int my[8] = {1, 0, 0, -1, 1, -1, 1, -1};
void addedge(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void add(int x, int y) {
int u = mp[pair<int, int>(x, y)];
for (int i = 0; i < 8; ++i) {
int tx = x + mx[i];
int ty = y + my[i];
int v = mp[pair<int, int>(tx, ty)];
if (v) addedge(u, v);
}
}
int vis[N];
int bfs(int x) {
memset(vis, 0, sizeof(vis));
queue<pair<int, int> > q;
while (!q.empty()) q.pop();
q.push(pair<int, int>(x, 0));
vis[x] = 1;
int ans = 0;
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
if (p.first == 2) return p.second;
for (int i = head[p.first]; i != -1; i = edge[i].next) {
if (vis[edge[i].v]) continue;
vis[edge[i].v] = 1;
q.push(pair<int, int>(edge[i].v, p.second + 1));
}
}
return -1;
}
int main() {
int x1, y1, x2, y2;
while (scanf("%d%d%d%d", &x1, &y1, &x2, &y2) != EOF) {
int i, j;
scanf("%d", &n);
mp.clear();
init();
m = 0;
mp[pair<int, int>(x1, y1)] = ++m;
mp[pair<int, int>(x2, y2)] = ++m;
for (i = 0; i < n; ++i) {
scanf("%d%d%d", &r[i], &a[i], &b[i]);
for (j = a[i]; j <= b[i]; ++j)
if (!mp[pair<int, int>(r[i], j)]) mp[pair<int, int>(r[i], j)] = ++m;
}
add(x1, y1);
add(x2, y2);
for (i = 0; i < n; ++i) {
for (j = a[i]; j <= b[i]; ++j) {
add(r[i], j);
}
}
int ans = bfs(1);
printf("%d\n", ans);
}
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<int, int>, bool> allowed;
map<pair<int, int>, bool> visitado;
bool valid(int i, int j) {
return i >= 1 && i <= 1e9 && j >= 1 && j <= 1e9 &&
allowed.find({i, j}) != allowed.end() && !visitado[{i, j}];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
while (n--) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) {
allowed[{r, i}] = true;
visitado[{r, i}] = false;
}
}
queue<pair<pair<int, int>, int> > q;
q.push({{x0, y0}, 0});
while (!q.empty()) {
auto p = q.front();
q.pop();
vector<pair<int, int> > mov = {{1, 0}, {-1, 0}, {0, -1}, {0, 1},
{1, 1}, {-1, 1}, {-1, -1}, {1, -1}};
for (int i = 0; i < 8; i++) {
int dx = p.first.first + mov[i].first;
int dy = p.first.second + mov[i].second;
int d = p.second;
if (valid(dx, dy)) {
visitado[{dx, dy}] = true;
if (dx == x1 && dy == y1) {
cout << d + 1 << '\n';
return 0;
}
q.push({{dx, dy}, d + 1});
}
}
}
cout << -1 << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
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;
}
};
int r1, c1, r2, c2, n, ri, ai, bi, dx[8] = {1, -1, 0, 0, 1, 1, -1, -1},
dy[8] = {0, 0, 1, -1, 1, -1, 1, -1};
map<pair<int, int>, int> p;
map<pair<int, int>, bool> vis, a;
bool valid(int rr, int cc) {
pair<int, int> pp = make_pair(rr, cc);
if (min(rr, cc) > 0 && max(rr, cc) <= (int)1e9 &&
(vis.find(pp) == vis.end()) && (a.find(pp) != a.end()))
return 1;
return 0;
}
int Bfs(int r, int c) {
queue<pair<int, int>> q;
q.push(make_pair(r, c));
while (!q.empty()) {
pair<int, int> v = q.front();
q.pop();
if (v.first == r2 && v.second == c2) return p[v];
for (int i = 0; i < 8; ++i) {
int ni = v.first + dx[i], nj = v.second + dy[i];
if (valid(ni, nj)) {
vis[make_pair(ni, nj)] = 1;
p[make_pair(ni, nj)] = p[v] + 1;
q.push(make_pair(ni, nj));
}
}
}
return -1;
}
int main() {
scanf("%d%d%d%d%d", &r1, &c1, &r2, &c2, &n);
for (int i = 0; i < n; ++i) {
scanf("%d%d%d", &ri, &ai, &bi);
for (int ci = ai; ci <= bi; ++ci) a[make_pair(ri, ci)] = 1;
}
p[make_pair(r1, c1)] = 0;
int ans = Bfs(r1, c1);
printf("%d\n", ans);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
queue<pair<int, int> > bfs;
queue<int> cekori;
pair<int, int> start, kraj;
map<pair<int, int>, int> mapa;
map<pair<int, int>, int>::iterator it;
int n, i, j, one, two, three;
cin >> start.first >> start.second >> kraj.first >> kraj.second;
cin >> n;
for (i = 0; i < n; i++) {
cin >> one >> two >> three;
for (j = two; j <= three; j++) {
pair<int, int> temp = make_pair(one, j);
mapa[temp] = 1000000000;
}
}
mapa[start] = 0;
bfs.push(start);
cekori.push(0);
while (!bfs.empty()) {
pair<int, int> temp = bfs.front();
int cek = cekori.front();
bfs.pop();
cekori.pop();
pair<int, int> poteg = make_pair(temp.first - 1, temp.second);
if ((temp.first - 1 >= 0) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first + 1, temp.second);
if ((temp.first + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first, temp.second - 1);
if ((temp.second - 1 >= 0) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first, temp.second + 1);
if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first - 1, temp.second + 1);
if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first + 1, temp.second + 1);
if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first + 1, temp.second - 1);
if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
poteg = make_pair(temp.first - 1, temp.second - 1);
if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
(mapa[poteg] > cek + 1)) {
mapa[poteg] = cek + 1;
bfs.push(poteg);
cekori.push(cek + 1);
}
if (mapa[kraj] < 1000000000) {
cout << mapa[kraj];
return 0;
}
}
cout << "-1";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
double squareRoot(double n) { return pow(2, 0.5 * log2(n)); }
ll gcd(ll a, ll b) {
if (b == 0) return a;
return gcd(b, a % b);
}
bool pal(string& s) {
for (int i = 0; i < s.size() / 2; i++) {
if (s[i] != s[s.size() - i - 1]) return 0;
}
return 1;
}
ll MOD = 1000000007;
ll powmod(ll a, ll b, ll m) {
if (b == 0) return 1;
if (b == 1) return a;
ll p1 = powmod(a, b / 2ll, m);
ll p2 = p1 * p1;
ll p3 = powmod(a, b % 2ll, m);
return ((p2 % m) * (p3 % m)) % m;
}
ll modpow(ll a, ll b, ll x) {
ll res = 1;
while (b > 0) {
if (b & 1) res = (res * a) % x;
a = (a * a) % x;
b >>= 1;
}
return res;
}
ll ncr(ll n, ll r) {
if (n == 0 || r == 0) return 1;
if (r > n) return 0;
if (n - r < r) r = n - r;
ll ans = 1;
for (ll i = 1; i <= r; i++) {
ans = ans * (n - i + 1) / i;
}
return ans;
}
bool bruteprime(ll n) {
if (n == 1) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
vector<ll> primefactors(ll n) {
vector<ll> ans;
for (ll i = 2; i * i <= n; i++) {
while (n % i == 0) {
ans.push_back(i);
n /= i;
}
}
if (n > 1) ans.push_back(n);
return ans;
}
vector<ll> factors(ll n) {
vector<ll> ans;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
ans.push_back(i);
if (n / i != i) {
ans.push_back(n / i);
}
}
}
return ans;
}
void computeLPSArray(string pat, int M, int* lps);
int KMPSearch(string pat, string txt) {
int M = pat.size();
int N = txt.size();
int lps[M];
computeLPSArray(pat, M, lps);
int i = 0;
int j = 0;
int ans = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
ans++;
j = lps[j - 1];
} else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return ans;
}
void computeLPSArray(string pat, int M, int* lps) {
int len = 0;
lps[0] = 0;
int i = 1;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
} else {
if (len != 0) {
len = lps[len - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}
ll nCrModp(ll n, ll r, ll p) {
if (r > n - r) r = n - r;
ll C[r + 1];
memset(C, 0, sizeof(C));
C[0] = 1;
for (ll i = 1; i <= n; i++) {
for (ll j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p;
}
return C[r];
}
bool cmp(vector<double>& a, vector<double>& b) {
if (a[3] > b[3])
return true;
else if (a[3] < b[3])
return false;
else
return a[2] < b[2];
}
ll cnt = 0;
void dfsutil(vector<bool>& vis, ll src, ll n, ll k) {
vis[src] = 1;
cnt++;
for (ll i = 0; i < (k); ++i) {
if ((src - i) >= 0 && (src + k - i - 1) < n && !vis[src - i - i + k - 1])
dfsutil(vis, src - i - i + k - 1, n, k);
}
}
void dfs(vector<bool>& vis, ll n, ll m, ll k) {
ll c = 0;
for (ll i = 0; i < (n); ++i) {
if (vis[i] == false) {
dfsutil(vis, i, n, k);
c++;
}
}
ll ans = 1;
for (ll i = 0; i < (c); ++i) {
ans = (ans * m) % MOD;
}
cout << ans;
}
ll dx[] = {0, 0, 1, 1, 1, -1, -1, -1};
ll dy[] = {1, -1, -1, 0, 1, -1, 0, 1};
void solve() {
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ll n;
cin >> n;
map<pair<ll, ll>, ll> m1, m2;
for (ll i = 0; i < (n); ++i) {
ll x, y, z;
cin >> x >> y >> z;
for (ll j = y; j <= (z); ++j) m1[{x, j}] = 1;
}
queue<pair<pair<ll, ll>, ll>> q;
m2[{x1, y1}] = 1;
q.push({{x1, y1}, 0});
while (!q.empty()) {
auto ele = q.front();
q.pop();
if (ele.first.first == x2 && ele.first.second == y2) {
cout << ele.second << "\n";
return;
}
for (ll i = 0; i < (8); ++i) {
ll x = ele.first.first + dx[i], y = ele.first.second + dy[i];
if (m1.find({x, y}) != m1.end() && m2.find({x, y}) == m2.end()) {
m2[{x, y}] = 1;
q.push({{x, y}, ele.second + 1});
}
}
}
cout << "-1";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
ll t = 1;
while (t--) solve();
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
multimap<long long, pair<long long, long long> > allowed;
bool inBound(int x, int y) {
multimap<long long, pair<long long, long long> >::iterator it, itlow, itup;
itlow = allowed.lower_bound(x);
itup = allowed.upper_bound(x);
for (it = itlow; it != itup; it++) {
if (y >= (*it).second.first && y <= (*it).second.second) return true;
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
set<pair<long long, long long> > vis;
long long x0, y0, x1, y1, n;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
while (n--) {
long long r, a, b;
cin >> r >> a >> b;
allowed.insert(make_pair(r, make_pair(a, b)));
}
long long m = 0;
queue<pair<long long, long long> > q;
q.push(make_pair(x0, y0));
while (!q.empty()) {
long long sz = q.size();
while (sz--) {
pair<long long, long long> p(q.front().first, q.front().second);
q.pop();
vis.insert(make_pair(p.first, p.second));
if (p == make_pair(x1, y1)) {
cout << m;
return 0;
}
if (inBound(p.first - 1, p.second) &&
vis.count(make_pair(p.first - 1, p.second)) == 0) {
q.push(make_pair(p.first - 1, p.second));
vis.insert(make_pair(p.first - 1, p.second));
}
if (inBound(p.first - 1, p.second - 1) &&
vis.count(make_pair(p.first - 1, p.second - 1)) == 0) {
q.push(make_pair(p.first - 1, p.second - 1));
vis.insert(make_pair(p.first - 1, p.second - 1));
}
if (inBound(p.first - 1, p.second + 1) &&
vis.count(make_pair(p.first - 1, p.second + 1)) == 0) {
q.push(make_pair(p.first - 1, p.second + 1));
vis.insert(make_pair(p.first - 1, p.second + 1));
}
if (inBound(p.first + 1, p.second) &&
vis.count(make_pair(p.first + 1, p.second)) == 0) {
q.push(make_pair(p.first + 1, p.second));
vis.insert(make_pair(p.first + 1, p.second));
}
if (inBound(p.first + 1, p.second - 1) &&
vis.count(make_pair(p.first + 1, p.second - 1)) == 0) {
q.push(make_pair(p.first + 1, p.second - 1));
vis.insert(make_pair(p.first + 1, p.second - 1));
}
if (inBound(p.first + 1, p.second + 1) &&
vis.count(make_pair(p.first + 1, p.second + 1)) == 0) {
q.push(make_pair(p.first + 1, p.second + 1));
vis.insert(make_pair(p.first + 1, p.second + 1));
}
if (inBound(p.first, p.second + 1) &&
vis.count(make_pair(p.first, p.second + 1)) == 0) {
q.push(make_pair(p.first, p.second + 1));
vis.insert(make_pair(p.first, p.second + 1));
}
if (inBound(p.first, p.second - 1) &&
vis.count(make_pair(p.first, p.second - 1)) == 0) {
q.push(make_pair(p.first, p.second - 1));
vis.insert(make_pair(p.first, p.second - 1));
}
}
m++;
}
cout << -1;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 5;
const int INF = (int)1e9 + 7;
const double EPS = 1e-9;
const int hh[] = {1, -1, 0, 0, -1, -1, 1, 1};
const int hc[] = {0, 0, 1, -1, -1, 1, -1, 1};
void nhap();
void process();
int n, k, y;
set<pair<int, int> > th;
queue<pair<int, pair<int, int> > > hd;
int s1, s2, e1, e2;
int res;
int main() {
int nTest = 1;
for (int test = 1; test <= nTest; ++test) {
nhap();
process();
}
}
void nhap() {
scanf("%d%d%d%d", &s1, &s2, &e1, &e2);
scanf("%d", &n);
int r, ai, bi;
for (int i = 1; i <= n; ++i) {
scanf("%d%d%d", &r, &ai, &bi);
for (int j = ai; j <= bi; ++j) th.insert(make_pair(r, j));
}
}
void process() {
hd.push(make_pair(0, make_pair(s1, s2)));
th.erase(pair<int, int>(s1, s2));
int y, x, p, q, ts;
while (!hd.empty()) {
pair<int, pair<int, int> > u = hd.front();
hd.pop();
ts = u.first;
x = u.second.first;
y = u.second.second;
for (int i = 0; i < 8; ++i) {
p = x + hh[i];
q = y + hc[i];
pair<int, int> ii = make_pair(p, q);
if (th.count(ii) > 0) {
if (p == e1 && q == e2) {
printf("%d", ts + 1);
return;
}
th.erase(ii);
hd.push(make_pair(ts + 1, ii));
}
}
}
puts("-1");
}
|
#include <bits/stdc++.h>
using namespace std;
map<pair<long long, long long>, long long> allowed;
map<pair<long long, long long>, long long> marked;
long long dr[] = {1, 1, 1, 0, 0, -1, -1, -1};
long long dc[] = {1, -1, 0, 1, -1, 0, -1, 1};
long long bfs(long long sr, long long sc, long long er, long long ec) {
queue<pair<long long, long long> > q;
q.push(make_pair(sr, sc));
marked[make_pair(sr, sc)] = 0;
pair<long long, long long> p;
while (!q.empty()) {
p = q.front();
q.pop();
for (long long i = 0; i < 8; ++i) {
long long tr = p.first + dr[i];
long long tc = p.second + dc[i];
if (tr > 1e9 || tc > 1e9 || tr < 1 || tc < 1 ||
!(allowed[make_pair(tr, tc)] > 0) || marked[make_pair(tr, tc)] > 0)
continue;
marked[make_pair(tr, tc)] = marked[p] + 1;
if (tr == er && tc == ec) {
return marked[p] + 1;
}
q.push(make_pair(tr, tc));
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
long long sr, se, er, ee;
cin >> sr >> se >> er >> ee;
long long n;
cin >> n;
long long row, col1, col2;
for (long long i = 0; i < n; ++i) {
cin >> row >> col1 >> col2;
for (long long j = col1; j < col2 + 1; ++j) {
allowed[make_pair(row, j)] = 1;
}
}
long long ans = bfs(sr, se, er, ee);
cout << ans << '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
map<int, map<int, bool> > board;
int main(void) {
ios::sync_with_stdio(0);
int xo, yo, xf, yf;
cin >> xo >> yo >> xf >> yf;
queue<int> Q;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int i = a; i <= b; i++) board[r][i] = true;
}
int ans = -1;
Q.push(xo);
Q.push(yo);
Q.push(0);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
int y = Q.front();
Q.pop();
int d = Q.front();
Q.pop();
if (x == xf && y == yf) {
ans = d;
break;
}
if (board[x - 1][y - 1]) {
Q.push((x - 1));
Q.push((y - 1));
Q.push((d + 1));
board[x - 1][y - 1] = false;
}
if (board[x - 1][y]) {
Q.push((x - 1));
Q.push((y));
Q.push((d + 1));
board[x - 1][y] = false;
}
if (board[x - 1][y + 1]) {
Q.push((x - 1));
Q.push((y + 1));
Q.push((d + 1));
board[x - 1][y + 1] = false;
}
if (board[x][y - 1]) {
Q.push((x));
Q.push((y - 1));
Q.push((d + 1));
board[x][y - 1] = false;
}
if (board[x][y + 1]) {
Q.push((x));
Q.push((y + 1));
Q.push((d + 1));
board[x][y + 1] = false;
}
if (board[x + 1][y - 1]) {
Q.push((x + 1));
Q.push((y - 1));
Q.push((d + 1));
board[x + 1][y - 1] = false;
}
if (board[x + 1][y]) {
Q.push((x + 1));
Q.push((y));
Q.push((d + 1));
board[x + 1][y] = false;
}
if (board[x + 1][y + 1]) {
Q.push((x + 1));
Q.push((y + 1));
Q.push((d + 1));
board[x + 1][y + 1] = false;
}
}
cout << ans << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
long long int advceil(long long int num, long long int den) {
return (num % den == 0 ? num / den : num / den + 1);
}
long long int lstbt(long long int val) {
long long int msk = val & (val - 1);
return log2(val ^ msk);
}
long long int modmul(long long int a, long long int b) {
if (a < 1e9 and b < 1e9) return (a * b) % 1000000007;
a %= 1000000007;
b %= 1000000007;
long long int res = 0;
while (b > 0) {
if (b & 1) res = (res + a) % 1000000007;
a = (a * 2) % 1000000007;
b /= 2;
}
return res;
}
long long int modexpo(long long int a, long long int b) {
long long int res = 1;
while (b > 0) {
if (b & 1) res = modmul(res, a);
a = modmul(a, a);
b /= 2;
}
return res;
}
long long int gcd(long long int a, long long int b) {
return a == 0 ? b : gcd(b % a, a);
}
vector<long long int> CALCfactor(long long int n) {
vector<long long int> factor(n + 2, 0);
for (long long int i = 4; i <= n; i += 2) factor[i] = 2;
for (long long int j = 3; j <= n; j += 2) {
if (factor[j]) continue;
for (long long int i = 2 * j; i <= n; i += j) factor[i] = j;
}
return factor;
}
vector<long long int> CALCprimeNUM(long long int n) {
vector<long long int> factor = CALCfactor(n);
vector<long long int> primeNUM;
primeNUM.reserve(n + 2);
for (long long int i = 2; i <= n; i++)
if (!factor[i]) primeNUM.push_back(i);
return primeNUM;
}
vector<long long int> CALCprimeFACTOR(long long int n) {
vector<long long int> factor = CALCfactor(n);
vector<long long int> ans;
while (factor[n] != 0) {
ans.push_back(factor[n]);
n /= factor[n];
}
if (n > 1) ans.push_back(n);
return ans;
}
vector<long long int> unique(vector<long long int> x) {
sort(x.begin(), x.end());
set<long long int> s;
vector<long long int> ans;
ans.reserve(x.size());
for (auto elem : x) s.insert(elem);
for (auto elem : s) ans.push_back(elem);
return ans;
}
pair<vector<long long int>, vector<long long int> > getFact(long long int n) {
vector<long long int> fact(n + 1, 1), invfact(n + 1, 1);
for (long long int i = 1; i <= n; i++)
fact[i] = (i * (fact[i - 1])) % 1000000007;
for (long long int i = 1; i <= n; i++)
invfact[i] = (modexpo(i, 1000000007 - 2) * invfact[i - 1]) % 1000000007;
return {fact, invfact};
}
void compress(vector<long long int>& arr, long long int n) {
map<long long int, vector<long long int> > pos;
for (long long int i = 1; i <= n; i++) pos[arr[i]].push_back(i);
long long int cnt = 1;
for (auto itr : pos) {
for (auto elem : itr.second) arr[elem] = cnt;
cnt++;
}
}
bool rcomp(long long int a, long long int b) { return a > b; }
map<long long int, vector<long long int> > edges;
map<pair<long long int, long long int>, long long int> dp;
long long int dx[] = {-1, 1, 0, 0, 1, -1, 1, -1};
long long int dy[] = {0, 0, -1, 1, -1, 1, 1, -1};
long long int Dfn(long long int xs, long long int ys, long long int xa,
long long int ya) {
queue<pair<long long int, pair<long long int, long long int> > > q;
q.push({0, {xs, ys}});
dp[{xs, ys}] = 0;
while (!q.empty()) {
auto elem = q.front();
q.pop();
for (long long int i = 0; i < 8; i++) {
long long int newx = elem.second.first + dx[i];
long long int newy = elem.second.second + dy[i];
if (edges.count(newx) == 0) continue;
if (!binary_search(edges[newx].begin(), edges[newx].end(), newy))
continue;
if (dp[{newx, newy}] > elem.first + 1) {
dp[{newx, newy}] = elem.first + 1;
q.push({elem.first + 1, {newx, newy}});
}
}
}
if (dp[{xa, ya}] < 1e8) return dp[{xa, ya}];
return -1;
}
void solve() {
long long int xs, ys, xa, ya;
cin >> xs >> ys >> xa >> ya;
long long int n;
cin >> n;
for (long long int i = 0; i < n; i++) {
long long int x, y, z;
cin >> x >> y >> z;
for (long long int j = y; j <= z; j++)
edges[x].push_back(j), dp[{x, j}] = 1e9;
}
for (auto elem : edges)
sort(edges[elem.first].begin(), edges[elem.first].end());
long long int ans = Dfn(xs, ys, xa, ya);
cout << ans << "\n";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
auto start1 = high_resolution_clock::now();
solve();
auto stop1 = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop1 - start1);
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
clock_t clk = clock();
cerr << "ACE THE GAME ...\n";
map<pair<long long, long long>, long long> allowed;
queue<pair<long long, long long> > q;
long long dx[] = {1, 1, 1, -1, -1, -1, 0, 0};
long long dy[] = {1, 0, -1, 1, 0, -1, 1, -1};
long long n, x0, y0, x1, y1, r, a, b;
cin >> x0 >> y0 >> x1 >> y1;
cin >> n;
for (long long i = 0; i < n; i++) {
cin >> r >> a >> b;
for (long long j = a; j <= b; j++) {
allowed[make_pair(r, j)] = -1;
}
}
allowed[pair<long long, long long>(x0, y0)] = 0;
q.push(make_pair(x0, y0));
while (!q.empty()) {
pair<long long, long long> u = q.front();
q.pop();
for (long long i = 0; i < 8; i++) {
pair<long long, long long> v =
pair<long long, long long>(u.first + dx[i], u.second + dy[i]);
if (allowed[v] == -1) {
allowed[v] = allowed[u] + 1;
q.push(v);
}
}
}
cout << allowed[pair<long long, long long>(x1, y1)];
cerr << "...and TAKE IT EASY.\n";
cerr << "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC
<< '\n';
}
|
#include <bits/stdc++.h>
using namespace std;
unordered_map<int, unordered_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;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, 1, -1, -1};
int main() {
int x1, x2, y1, y2, i, j, k, l, m, n, r, a, b, x, y;
map<pair<int, int>, int> mp, mp1;
cin >> x1 >> y1 >> x2 >> y2 >> n;
mp[{x1, y1}] = mp[{x2, y2}] = 1;
for (i = 0; i < n; i++) {
cin >> r >> a >> b;
for (j = a; j <= b; j++) mp[{r, j}] = 1;
}
queue<pair<pair<int, int>, int> > q;
q.push({{x1, y1}, 0}), mp1[{x1, y1}] = 1;
while (!q.empty()) {
a = q.front().first.first, b = q.front().first.second, r = q.front().second,
q.pop();
for (i = 0; i < 8; i++) {
x = a + dx[i], y = b + dy[i];
if (x == x2 && y == y2) {
printf("%d\n", r + 1);
return 0;
}
if (mp1[{x, y}] == 0 && mp[{x, y}] == 1) {
mp1[{x, y}] = 1, q.push({{x, y}, r + 1});
}
}
}
printf("-1\n");
return 0;
}
|
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC optimize("no-stack-protector,fast-math")
using namespace std;
int dx[8] = {1, 0, -1, 0, -1, 1, -1, 1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
int a, b, c, d, t;
map<pair<int, int>, int> dist, s;
queue<pair<int, int>> q;
int bfs(pair<int, int> src, pair<int, int> dest) {
q.push(src);
dist[src] = 1;
while (!q.empty()) {
auto front = q.front();
q.pop();
for (int k = 0; k < 8; k++) {
pair<int, int> v;
v.first = front.first + dx[k];
v.second = front.second + dy[k];
if (s[v] == 0 || dist.count(v) != 0) continue;
dist[v] = dist[front] + 1;
q.push(v);
if (v.first == c && v.second == d) return dist[v] - 1;
}
}
return -1;
}
int main() {
scanf("%d %d %d %d", &a, &b, &c, &d);
scanf("%d", &t);
for (int i = 1; i <= t; i++) {
int r, from, to;
scanf("%d %d %d", &r, &from, &to);
for (int j = from; j <= to; j++) {
s[make_pair(r, j)] = 1;
}
}
pair<int, int> source, destination;
source.first = a, source.second = b;
destination.first = c, destination.second = d;
printf("%d\n", bfs(source, destination));
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const int n = 1e5 + 500;
int dx[]{0, 1, 0, -1, -1, 1, 1, -1};
int dy[]{1, 0, -1, 0, 1, 1, -1, -1};
int Q;
int Xs, Ys, Xe, Ye;
map<pair<int, int>, bool> ava, visit;
int main() {
scanf("%d %d %d %d %d", &Xs, &Ys, &Xe, &Ye, &Q);
while (Q--) {
int row, l, r;
scanf("%d %d %d", &row, &l, &r);
for (int i = l; i <= r; i++) {
ava[{row, i}] = 1;
}
}
queue<pair<pair<int, int>, int> > q;
q.push({{Xs, Ys}, 0});
while (!q.empty()) {
int x = q.front().first.first;
int y = q.front().first.second;
int dist = q.front().second;
if (x == Xe && y == Ye) {
printf("%d\n", dist);
return 0;
}
q.pop();
if (visit[{x, y}]) continue;
visit[{x, y}] = 1;
for (int d = 0; d < 8; d++) {
if (ava[{x + dx[d], y + dy[d]}])
q.push({{x + dx[d], y + dy[d]}, dist + 1});
}
}
puts("-1");
}
|
#include <bits/stdc++.h>
using namespace std;
int movx[] = {0, -1, -1, -1, 0, 1, 1, 1};
int movy[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int main(void) {
int x0, y0, x1, y1;
cin >> x0 >> y0 >> x1 >> y1;
int n;
cin >> n;
map<pair<int, int>, bool> available;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
for (int j = a; j <= b; j++) {
available[{r, j}] = true;
}
}
map<pair<int, int>, int> dis;
for (auto i : available) {
dis[{i.first.first, i.first.second}] = -1;
}
queue<pair<int, int>> q;
q.push({x0, y0});
dis[{x0, y0}] = 0;
dis[{x1, y1}] = -1;
while (!q.empty()) {
pair<int, int> x = q.front();
q.pop();
if (x.first == x1 && x.second == y1) {
break;
}
for (int i = 0; i < 8; i++) {
int nx = x.first + movx[i];
int ny = x.second + movy[i];
if (available[{nx, ny}] == true) {
dis[{nx, ny}] = 1 + dis[x];
available[{nx, ny}] = false;
q.push({nx, ny});
}
}
}
cout << dis[{x1, y1}];
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1e9 + 5;
const double eps = 1e-7;
const double PI = acos(-1.0);
inline void debug_vi(vector<int> a) {
for (long long i = (long long)(0); i < (long long)(a.size()); i++)
cout << a[i] << " ";
}
inline void debug_vll(vector<long long> a) {
for (long long i = (long long)(0); i < (long long)(a.size()); i++)
cout << a[i] << " ";
}
inline void print_case(int tn) { cout << "Case #" << tn << ": "; }
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
template <typename T>
using maxpq = priority_queue<T>;
map<int, vector<pair<int, int>>> segs, s;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
bool valid(int nu, int nv) {
return (nu >= 1 && nu <= (1e9) && nv >= 1 && nv <= (1e9));
}
bool checkSegs(int nu, int nv) {
vector<pair<int, int>> &sg = s[nu];
int lo = 0, hi = sg.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (sg[mid].second >= nv) {
if (sg[mid].first <= nv) return true;
hi = mid - 1;
} else
lo = mid + 1;
}
return false;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
clock_t clk = clock();
int x, y, a, b, n, r, p, q;
cin >> x >> y >> a >> b >> n;
for (long long i = (long long)(0); i < (long long)(n); i++) {
cin >> r >> p >> q;
segs[r].push_back({p, q});
}
for (auto &itr : segs) {
sort((itr.second).begin(), (itr.second).end());
vector<pair<int, int>> ints;
for (int i = 0; i < (int)itr.second.size();) {
int rr = i, hg = itr.second[i].second;
while (rr < (int)itr.second.size()) {
if (hg >= itr.second[rr].first) {
hg = max(hg, itr.second[rr].second);
rr++;
} else
break;
}
rr--;
ints.push_back({itr.second[i].first, itr.second[rr].second});
i = rr + 1;
}
s[itr.first] = ints;
}
set<pair<int, int>> visited;
map<pair<int, int>, int> dist;
queue<pair<int, int>> qu({{x, y}});
dist[{x, y}] = 0;
visited.insert({x, y});
int u, v, res = -1;
while (!qu.empty()) {
if (res >= 0) break;
tie(u, v) = qu.front();
qu.pop();
for (long long d = (long long)(0); d < (long long)(8); d++) {
int nu = u + dx[d];
int nv = v + dy[d];
if (valid(nu, nv) && checkSegs(nu, nv) && visited.count({nu, nv}) == 0) {
visited.insert({nu, nv});
dist[{nu, nv}] = dist[{u, v}] + 1;
qu.push({nu, nv});
if (nu == a && nv == b) {
res = dist[{nu, nv}];
break;
}
}
}
}
cout << res;
cerr << "\n"
<< "Time (in ms): " << double(clock() - clk) * 1000.0 / CLOCKS_PER_SEC
<< "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int ans = -1;
map<pair<int, int>, bool> visited, mp;
void bfs(int x1, int y1, int x2, int y2) {
queue<pair<pair<int, int>, int>> q;
q.push({{x1, y1}, 0});
while (!q.empty()) {
auto z = q.front();
q.pop();
visited[z.first] = 1;
if (z.first.first == x2 && z.first.second == y2) {
ans = z.second;
break;
}
int x = z.first.first, y = z.first.second;
if (x - 1 >= 1 && !visited[{x - 1, y}] && mp[{x - 1, y}]) {
visited[{x - 1, y}] = true;
q.push({{x - 1, y}, z.second + 1});
}
if (x + 1 <= 1e9 && !visited[{x + 1, y}] && mp[{x + 1, y}]) {
visited[{x + 1, y}] = true;
q.push({{x + 1, y}, z.second + 1});
}
if (y - 1 >= 1 && !visited[{x, y - 1}] && mp[{x, y - 1}]) {
visited[{x, y - 1}] = true;
q.push({{x, y - 1}, z.second + 1});
}
if (y + 1 <= 1e9 && !visited[{x, y + 1}] && mp[{x, y + 1}]) {
visited[{x, y + 1}] = true;
q.push({{x, y + 1}, z.second + 1});
}
if (x + 1 <= 1e9 && y + 1 <= 1e9 && !visited[{x + 1, y + 1}] &&
mp[{x + 1, y + 1}]) {
visited[{x + 1, y + 1}] = true;
q.push({{x + 1, y + 1}, z.second + 1});
}
if (x + 1 <= 1e9 && y - 1 >= 1 && !visited[{x + 1, y - 1}] &&
mp[{x + 1, y - 1}]) {
visited[{x + 1, y - 1}] = true;
q.push({{x + 1, y - 1}, z.second + 1});
}
if (x - 1 >= 1 && y + 1 <= 1e9 && !visited[{x - 1, y + 1}] &&
mp[{x - 1, y + 1}]) {
visited[{x - 1, y + 1}] = true;
q.push({{x - 1, y + 1}, z.second + 1});
}
if (x - 1 >= 1 && y - 1 >= 1 && !visited[{x - 1, y - 1}] &&
mp[{x - 1, y - 1}]) {
visited[{x - 1, y - 1}] = true;
q.push({{x - 1, y - 1}, z.second + 1});
}
}
}
int main() {
;
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
;
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int n;
cin >> n;
set<pair<int, pair<int, int>>> s;
for (int i = 0; i < n; i++) {
int r, a, b;
cin >> r >> a >> b;
s.insert({r, {a, b}});
}
for (auto it : s) {
for (int i = it.second.first; i <= it.second.second; i++) {
mp[{it.first, i}] = true;
}
}
mp[{x1, y1}] = true;
mp[{x2, y2}] = true;
bfs(x1, y1, x2, y2);
cout << ans;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int home[30];
int away[30];
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> home[i];
cin >> away[i];
}
int cnt = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) continue;
if (home[i] == away[j]) cnt++;
}
}
cout << cnt << '\n';
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x3f3f3f3f;
template <class T>
void show(T a, int n) {
for (int i = 0; i < n; ++i) cout << a[i] << ' ';
cout << endl;
}
template <class T>
void show(T a, int r, int l) {
for (int i = 0; i < r; ++i) show(a[i], l);
cout << endl;
}
const int N = 1e6 + 111;
map<int, int> k;
int x[31], y[31];
int main() {
int n, a, b, i, j;
cin >> n;
int ans = 0;
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
k[y[i]]++;
}
for (i = 0; i < n; i++) {
ans += k[x[i]];
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int inf = 0x3f3f3f3f;
template <class T>
void show(T a, int n) {
for (int i = 0; i < n; ++i) cout << a[i] << ' ';
cout << endl;
}
template <class T>
void show(T a, int r, int l) {
for (int i = 0; i < r; ++i) show(a[i], l);
cout << endl;
}
const int N = 1e6 + 111;
map<int, int> k;
int x[31], y[31];
const int maxnode = 2001100;
const int sigma_size = 26;
int ch[maxnode][sigma_size];
int main() {
int n, a, b, i, j;
cin >> n;
int ans = 0;
for (i = 0; i < n; i++) {
cin >> x[i] >> y[i];
k[y[i]]++;
}
for (i = 0; i < n; i++) {
ans += k[x[i]];
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, h, g;
vector<int> home, guest;
cin >> n;
while (n--) {
cin >> h >> g;
home.push_back(h);
guest.push_back(g);
}
n = home.size();
h = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (home[i] == guest[j]) h++;
}
}
cout << h << "\n";
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int count, home[30], guest[30], final = 0;
cin >> count;
for (int i = 0; i < count; i++) {
cin >> home[i] >> guest[i];
}
for (int i = 0; i < count; i++) {
for (int a = 0; a < count; a++) {
if (home[i] == guest[a]) {
final++;
}
}
}
cout << final;
}
|
#include <bits/stdc++.h>
int color[2][100];
int N;
int couter;
int main() {
scanf("%d", &N);
couter = 0;
for (int i = 0; i < N; i++) {
scanf("%d %d", &color[0][i], &color[1][i]);
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (j != i) {
if (color[0][i] == color[1][j]) couter++;
}
}
}
printf("%d\n", couter);
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int a, b[1111], c[1111], d;
int cnt;
int main() {
cin >> a;
for (int i = 1; i <= a; i++) {
cin >> b[i] >> c[i];
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= a; j++) {
if (i != j) {
if (b[i] == c[j]) {
cnt++;
}
if (c[i] == b[j]) {
cnt++;
}
}
}
}
cout << cnt / 2;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int a[101] = {0}, b[101];
int ans = 0;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
b[i] = x;
a[y]++;
}
for (int i = 0; i < n; i++) ans += a[b[i]];
cout << ans;
}
|
#include <bits/stdc++.h>
using namespace std;
int n, x;
pair<int, int> a[35];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (a[i].first == a[j].second) {
x++;
}
}
}
cout << x;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
int c = 0;
cin >> n;
int u[2][n];
for (int i = 0; i < n; i++) {
cin >> u[0][i] >> u[1][i];
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && u[0][i] == u[1][j]) {
c++;
}
}
}
cout << c;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a[30][2], k;
cin >> n;
k = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++) cin >> a[i][j];
for (int i = 0; i < n; i++) {
for (int z = 0; z < n; z++)
if (a[i][0] == a[z][1]) k++;
}
cout << k;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
typedef struct {
int home;
int guest;
} k;
int main() {
int m, n, ans = 0, w;
cin >> n;
k *v;
v = new k[n];
for (int i = 0; i < n; i++) {
cin >> v[i].home >> v[i].guest;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (j != i && v[i].home == v[j].guest) ans++;
}
}
cout << ans << endl;
return 0;
}
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, a, b, first, sec, c = 0;
cin >> n;
vector<int> v;
vector<int> u;
while (n--) {
cin >> a >> b;
v.push_back(a);
u.push_back(b);
}
for (int i = 0; i < v.size(); i++) {
for (int j = 0; j < u.size(); j++) {
if (v[i] == u[j]) {
c += 1;
}
}
}
cout << c;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.