Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <set>
- #include <string>
- #include <algorithm>
- #include <queue>
- using namespace std;
- using ll = long long;
- #define pii pair <int, int>
- void cv(vector <int> v){
- for (auto x: v) cout<<x<<' ';
- cout<<'\n';
- }
- int N;
- bool in(int x, int y){
- bool a = x < N && x >=0;
- bool b = y < N && y >= 0;
- return a&&b;
- }
- vector <int> dx = {-2, -2, -1, 1, 2, 2, 1, -1};
- vector <int> dy = {-1, 1, 2, 2, 1, -1, -2, -2};
- vector <vector <vector <pii> > > dsk(8, vector <vector <pii> > (8));
- queue <pii> go1;
- pii strt, fin;
- vector < vector <pii> > pred;
- vector <vector<int>> dst(8, vector <int> (8, -1));
- int cnt = 0;
- void bfs(){
- dst[strt.first][strt.second] = 0;
- go1.push(strt);
- cout<<"BFS\n";
- while (!go1.empty()){
- auto v = go1.front();
- //cout<<"v= \n";
- //cout<<v.first<<' '<<v.second<<'\n';
- for (auto u: dsk[v.first][v.second]){
- if (dst[u.first][u.second] = -1){
- dst[u.first][u.second] = dst[v.first][v.second] + 1;
- go1.push(u);
- pred[u.first][u.second] = v;
- }
- if (u == fin){
- return;
- }
- }
- go1.pop();
- cnt++;
- //if (cnt == 200) exit(0);
- }
- }
- int main()
- {
- /*ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);*/
- cin>>N;
- dsk.resize(N);
- dst.resize(N);
- for (int i=0;i<N;++i){
- dsk[i].resize(N);
- dst[i].resize(N, -1);
- }
- for (int i = 0; i < N;++i){
- for (int j=0;j<N;++j){
- //cout<<"IJ= "<<i<<" "<<j<<"\n";
- for (int ngb = 0; ngb<8; ++ngb){
- //cout<<"ngb= "<<ngb<<'\n';
- int I = i + dx[ngb];
- int J = j + dy[ngb];
- //cout<<"dx dy = "<<dx[ngb]<<' '<<dy[ngb]<<'\n';
- if (in(I, J)){
- dsk[i][j].push_back({I, J});
- }
- }//cout<<'\n';
- }
- }
- /*cout<<"\nDESK\n";
- for (int i = 0; i < N;++i){
- for (int j=0;j<N;++j){
- cout<<"IJ= "<<i<<" "<<j<<"\n";
- for (auto ngb: dsk[i][j]){
- cout<<ngb.first<<' '<<ngb.second<<"\n";
- }cout<<"\n";
- }
- }*/
- cin>>strt.first>>strt.second>>fin.first>>fin.second;
- strt.first--;
- strt.second--;
- fin.first--;
- fin.second--;
- bfs();
- cout<<"ans= \n";
- cout<<dst[fin.first][fin.second]<<'\n';
- cout<<"predki\n";
- pii pr = fin;
- vector <pii> ans_pred;
- while (true){
- ans_pred.push_back(pr);
- pr = pred[pr.first][pr.second];
- if (pr == strt) break;
- }
- for (auto x: ans_pred) cout<<x.first<<' '<<x.second<<'\n';
- cout<<'\n';
- }
Add Comment
Please, Sign In to add comment