Last active
March 3, 2019 20:11
-
-
Save vkopichenko/0648a7bfa7a6d6a8d3b35dd5949b0265 to your computer and use it in GitHub Desktop.
Простейшая олимпиадная очередь, отрицательные индексы в двумерном массиве
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "bits/stdc++.h" | |
using namespace std; | |
const int N = 500; | |
const int N2 = 2 * N + 1; | |
typedef bool A[N2]; | |
A _a_[N2]; | |
bool* _a[N2]; | |
bool** used; | |
struct P { | |
int x, y; | |
int steps; | |
P(int ax, int ay, int asteps) { | |
x = ax; y = ay; steps = asteps; | |
} | |
}; | |
queue<P> q; | |
P pop(queue<P>& q) { | |
P res = q.front(); | |
q.pop(); | |
return res; | |
} | |
void push(queue<P>& q, int x, int y, int steps) { | |
if (x >= -N && x <= N && y >= -N && y <= N && !used[x][y]) { | |
q.push(P(x, y, steps)); | |
used[x][y] = true; | |
} | |
} | |
int main() { | |
for (int i=N2; i--;) _a[i] = &_a_[i][N]; | |
used = &_a[N]; | |
ifstream cin("mud.in"); | |
ofstream cout("mud.out"); | |
int n,X,Y; | |
cin >> X >> Y >> n; | |
for (int i=0; i < n; ++i) { | |
int x, y; | |
cin >> x >> y; | |
used[x][y] = true; | |
} | |
push(q, 0, 0, 0); | |
while (!q.empty()) { | |
P p = q.front(); q.pop(); | |
if (p.x == X && p.y == Y) { | |
cout << p.steps << endl; | |
break; | |
} | |
push(q, p.x, p.y + 1, p.steps + 1); | |
push(q, p.x + 1, p.y, p.steps + 1); | |
push(q, p.x, p.y - 1, p.steps + 1); | |
push(q, p.x - 1, p.y, p.steps + 1); | |
} | |
return 0; | |
} |
Для запоминания позиций можно вместо большого двумерного массива использовать множество (std::set):
#include "bits/stdc++.h"
using namespace std;
const int N = 500;
typedef pair<int, int> P;
struct PQ : P {
int steps;
PQ(int ax, int ay, int asteps) : P(ax, ay) {
steps = asteps;
}
};
PQ pop(queue<PQ>& q) {
PQ res = q.front();
q.pop();
return res;
}
set<P> used;
void push(queue<PQ>& q, int x, int y, int steps) {
const PQ& pq = PQ(x, y, steps);
if (x >= -N && x <= N && y >= -N && y <= N && !used.count(pq)) {
q.push(pq);
used.insert(pq);
}
}
int main() {
ifstream cin("mud.in");
ofstream cout("mud.out");
int n,X,Y;
cin >> X >> Y >> n;
for (int i=0; i < n; ++i) {
int x, y;
cin >> x >> y;
used.insert(P(x, y));
}
queue<PQ> q;
push(q, 0, 0, 0);
while (!q.empty()) {
PQ p = q.front(); q.pop();
if (p.first == X && p.second == Y) {
cout << p.steps << endl;
break;
}
push(q, p.first, p.second + 1, p.steps + 1);
push(q, p.first + 1, p.second, p.steps + 1);
push(q, p.first, p.second - 1, p.steps + 1);
push(q, p.first - 1, p.second, p.steps + 1);
}
return 0;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Задача: http://dl.gsu.by/task.jsp?nid=1756228&cid=1105
Программирование - профессионалы (лич. 2018-2019) (P/O)
К области 5-9 (весна)\Область, 20 апреля 2017, 5-9 кл\10 - "Попасть в точку - 2" 201290