이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> baris[30005];
int n, m, tujuan, ans = LLONG_MAX;
void solve(int pos, int p, int count) {
// cout << pos << " " << p << " " << count << endl;
// return 0;
if (pos == tujuan) {
ans = min(count, ans);
return;
}
// while (pos >= 0 and pos < n) {
// if (pos < 0 or pos >= n) return;
// if (pos-p >= 0) solve(pos-p, p, count+1);
// if (pos+p < n) solve(pos+p, p, count+1);
int cur = pos;
int temp = 0;
while (cur-p >= 0) {
cur -= p;
temp += 1;
// cout << "cur " << cur << endl;
if (!baris[cur].empty()) {
// cout << "NOT EMPTY" << endl;
for (int i = 0; i < baris[cur].size(); i++) {
int save = baris[cur][i];
baris[cur].erase(baris[cur].begin()+i);
solve(cur, save, count+temp);
baris[cur].push_back(save);
}
}
}
cur = pos;
temp = 0;
while (cur+p < n) {
cur += p;
temp += 1;
// cout << "cur " << cur << endl;
if (!baris[cur].empty()) {
// cout << "NOT EMPTY" << endl;
for (int i = 0; i < baris[cur].size(); i++) {
int save = baris[cur][i];
baris[cur].erase(baris[cur].begin()+i);
solve(cur, save, count+temp);
baris[cur].push_back(save);
}
}
}
// }
}
signed main() {
int b, p, posstart, pstart;
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> b >> p;
if (i == 0) {
posstart = b;
pstart = p;
} else {
if (i == 1) tujuan = b;
// cout << b << endl;
baris[b].push_back(p);
}
}
solve(posstart, pstart, 0);
if (ans == LLONG_MAX) cout << -1 << endl;
else cout << ans << endl;
// vector<int> a;
// a.push_back(1);
// a.push_back(2);
// for (auto i: a) cout << i << " ";
// cout << endl;
// a.erase(a.begin()+1);
// for (auto i: a) cout << i << " ";
// cout << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'void solve(long long int, long long int, long long int)':
skyscraper.cpp:30:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < baris[cur].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
skyscraper.cpp:47:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for (int i = 0; i < baris[cur].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:74:8: warning: 'pstart' may be used uninitialized in this function [-Wmaybe-uninitialized]
74 | solve(posstart, pstart, 0);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:74:8: warning: 'posstart' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |