Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <math.h>
- using namespace std;
- int main () {
- vector<pair<int,int>>step = {{2,-1},{2,1},{1,2},{1,-2},{-2,-1},{-2,1},{-1,2},{-1,-2}};
- int n,x1,x2,y1,y2,sd = 0;
- cin>>n;
- char a;
- vector<pair<int,int>>Ass;
- vector<vector<int>> dist(n +1, vector<int>(n + 1, n * n +1));
- vector<vector<pair<int,int>>> parent(n + 1, vector<pair<int, int>>(n + 1, {-1, -1}));
- for(int i = 0 ;i < n ; i++){
- for(int j = 0 ; j < n ; j++){
- cin>>a;
- if (a == '@'){
- if(sd == 0){
- dist[i][j] = 0;
- sd+=1;
- }
- Ass.push_back({i,j});
- }
- }
- }
- cout<<endl<<endl<<endl;
- x1 = Ass[0].first;
- y1 = Ass[0].second;
- x2 = Ass[1].first;
- y2 = Ass[1].second;
- for(int i = 0 ;i < n+1 ; i++){
- for(int j = 0 ; j < n+1 ; j++){
- cout<<dist[i][j]<<" ";
- }
- cout<<endl;
- }
- queue<pair<int,int>>q;
- q.push({x1,y1});
- dist[x1][y1] =0;
- while(q.size() != 0) {
- for(int i = 0 ; i < 8 ; i++){
- int curectx = q.front().first, curecty = q.front().second;
- int newx = curectx + step[i].first, newy = curecty + step[i].second;
- cout<<q.front().first<<" "<<q.front().second<<endl;
- if ((newx <= n && newx > 0) && (newy <= n && newy > 0)) {
- if (dist[newx][newy] == n * n + 1 || dist[newx][newy] > dist[curectx][curecty] + 1) {
- dist[newx][newy] = dist[curectx][curecty] + 1;
- parent[newx][newy] = {curectx,curecty};
- q.push({newx,newy});
- }
- }
- }
- q.pop();
- }
- cout<<endl<<endl<<endl;
- for(int i = 0 ;i < n +1 ; i++){
- for(int j = 0 ; j < n+1 ; j++){
- cout<<dist[i][j]<<" ";
- }
- cout<<endl;
- }
- cout<<endl<<endl<<endl;
- //if(dist[x2][y2] == n*n+1){
- // cout<<"Impossible"<<endl;
- // return 0;
- //}
- for(int i = n ;i > 0 ; i= i - 1){
- for(int j = n ; j > 0; j = j -1 ){
- //cout<<i<<" "<<j<<endl;
- if(dist[i][j] != n*n+1){
- cout<<"@"<<" ";
- }
- else{
- cout<<"."<<" ";
- }
- }
- cout<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement