#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
//#pragma GCC optimize("O3")
//#pragma GCC optimization("Ofast,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ll long long
//#define smid (nl + nr) / 2
//#define lc id * 2
//#define rc id * 2 + 1
#define migmig std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);std::cout.tie(NULL);
template <class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
const int N = 30005;
bitset<N>mark[N];
vector<int> doge[N];
void solve() {
int n, m; scanf("%d%d", &n, &m);
int b0, p0, b1, p1;
for (int i = 0; i < m; i++) {
int b, p; scanf("%d%d", &b, &p);
doge[b].push_back(p);
if (i == 1) {
b1 = b;
p1 = p;
}
if (i == 0) {
b0 = b;
p0 = p;
}
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= 30000; j++) {
mark[i][j] = 0;
}
}
queue<array<int, 3>> q;
q.push({ 0, b0, p0 });
mark[b0][p0] = 1;
while (!q.empty()) {
int d = q.front()[0];
int v = q.front()[1];
int p = q.front()[2];
q.pop();
if (v == b1) {
printf("%d", d); return;
}
if (v - p >= 0) {
if (!mark[v - p][p]) {
if (v - p == b1) {
printf("%d", d + 1); return;
}
q.push({ d + 1, v - p, p });
mark[v - p][p] = 1;
}
}
if (v + p < n) {
if (!mark[v + p][p]) {
if (v + p == b1) {
printf("%d", d + 1); return;
}
q.push({ d + 1, v + p, p });
mark[v + p][p] = 1;
}
}
for (int pp : doge[v]) {
if (!mark[v][pp]) {
mark[v][pp] = 1;
if (v - pp >= 0 && !mark[v - pp][pp]) {
q.push({ d + 1, v - pp, pp });
mark[v - pp][pp] = 1;
}
if (v + pp < n && !mark[v + pp][pp]) {
q.push({ d + 1, v + pp, pp });
mark[v + pp][pp] = 1;
}
}
}
doge[v].clear();
}
printf("-1");
}
int main() {
migmig;
int T = 1;
//cin >> T;
while (T--) {
solve();
}
}
Compilation message (stderr)
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:25:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | int n, m; scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
skyscraper.cpp:29:24: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | int b, p; scanf("%d%d", &b, &p);
| ~~~~~^~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |