Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// https://informatics.msk.ru/mod/statements/view3.php?chapterid=161#1
- #include <bits/stdc++.h>
- using namespace std;
- const int DIM = 21;
- int n, fx, fy, sx, sy, used[DIM][DIM], dist[DIM][DIM];
- pair < int, int > from[DIM][DIM]; /// Масив пар цілих чисел для відновлення шляху
- queue < pair < int, int > > q; /// Черга, кожен елемент якої - пара цілих чисел
- void check(int fromx, int fromy, int x, int y){
- if (x > 0 && y > 0 && x <= n && y <= n && used[x][y] == 0){ /// Якщо (х, у) не виходить за межі дошки і якщо вона ще не була відвідана
- used[x][y] = 1;
- dist[x][y] = dist[fromx][fromy] + 1;
- from[x][y] = {fromx, fromy}; /// Попередником точки (х, у) стає точка (fromx, fromy)
- q.push({x, y}); /// Додаємо (х, у) в чергу для подальшого опрацювання
- }
- }
- int main(){
- cin >> n >> fx >> fy >> sx >> sy;
- /**
- Щоб не реверсити відповідь в кінці, можна починати не зі стартової кітинки, а з останньої.
- **/
- q.push({sx, sy}); /// Додаємо в чергу стартову вершину (х2, у2)
- used[sx][sy] = 1;
- while(!q.empty()){
- pair<int,int> v = q.front();
- q.pop();
- check(v.first, v.second, v.first+2, v.second+1); /// v.first - координата точки з черги по х, v.second - координата по у
- check(v.first, v.second, v.first+2, v.second-1);
- check(v.first, v.second, v.first-2, v.second+1);
- check(v.first, v.second, v.first-2, v.second-1);
- check(v.first, v.second, v.first+1, v.second+2);
- check(v.first, v.second, v.first+1, v.second-2);
- check(v.first, v.second, v.first-1, v.second+2);
- check(v.first, v.second, v.first-1, v.second-2);
- }
- cout << dist[fx][fy] << endl;
- while(fx != 0 && fy != 0){
- cout << fx << ' ' << fy << endl;
- pair<int,int> v = from[fx][fy];
- fx = v.first;
- fy = v.second;
- }
- return 0;
- }
- /*
- Breadth First Search
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement