Advertisement
MatveyL

chodkonya

Feb 20th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <math.h>
  5.  
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int main () {
  11.  
  12. vector<pair<int,int>>step = {{2,-1},{2,1},{1,2},{1,-2},{-2,-1},{-2,1},{-1,2},{-1,-2}};
  13.  
  14.  
  15. int n,x1,x2,y1,y2,sd = 0;
  16. cin>>n;
  17. char a;
  18.  
  19. vector<pair<int,int>>Ass;
  20. vector<vector<int>> dist(n +1, vector<int>(n + 1, n * n +1));
  21. vector<vector<pair<int,int>>> parent(n + 1, vector<pair<int, int>>(n + 1, {-1, -1}));
  22.  
  23. for(int i = 0 ;i < n ; i++){
  24. for(int j = 0 ; j < n ; j++){
  25. cin>>a;
  26. if (a == '@'){
  27. if(sd == 0){
  28. dist[i][j] = 0;
  29. sd+=1;
  30. }
  31. Ass.push_back({i,j});
  32. }
  33. }
  34.  
  35. }
  36. cout<<endl<<endl<<endl;
  37. x1 = Ass[0].first;
  38. y1 = Ass[0].second;
  39. x2 = Ass[1].first;
  40. y2 = Ass[1].second;
  41.  
  42. for(int i = 0 ;i < n+1 ; i++){
  43. for(int j = 0 ; j < n+1 ; j++){
  44. cout<<dist[i][j]<<" ";
  45. }
  46. cout<<endl;
  47. }
  48. queue<pair<int,int>>q;
  49. q.push({x1,y1});
  50.  
  51.  
  52.  
  53. dist[x1][y1] =0;
  54.  
  55.  
  56.  
  57. while(q.size() != 0) {
  58. for(int i = 0 ; i < 8 ; i++){
  59. int curectx = q.front().first, curecty = q.front().second;
  60. int newx = curectx + step[i].first, newy = curecty + step[i].second;
  61. cout<<q.front().first<<" "<<q.front().second<<endl;
  62. if ((newx <= n && newx > 0) && (newy <= n && newy > 0)) {
  63. if (dist[newx][newy] == n * n + 1 || dist[newx][newy] > dist[curectx][curecty] + 1) {
  64. dist[newx][newy] = dist[curectx][curecty] + 1;
  65. parent[newx][newy] = {curectx,curecty};
  66. q.push({newx,newy});
  67. }
  68. }
  69. }
  70.  
  71.  
  72. q.pop();
  73.  
  74. }
  75.  
  76. cout<<endl<<endl<<endl;
  77. for(int i = 0 ;i < n +1 ; i++){
  78. for(int j = 0 ; j < n+1 ; j++){
  79. cout<<dist[i][j]<<" ";
  80. }
  81. cout<<endl;
  82. }
  83. cout<<endl<<endl<<endl;
  84. //if(dist[x2][y2] == n*n+1){
  85. // cout<<"Impossible"<<endl;
  86. // return 0;
  87. //}
  88.  
  89. for(int i = n ;i > 0 ; i= i - 1){
  90. for(int j = n ; j > 0; j = j -1 ){
  91. //cout<<i<<" "<<j<<endl;
  92. if(dist[i][j] != n*n+1){
  93. cout<<"@"<<" ";
  94. }
  95. else{
  96. cout<<"."<<" ";
  97. }
  98. }
  99. cout<<endl;
  100. }
  101.  
  102.  
  103. return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement