Advertisement
Guest User

Cavall

a guest
Dec 21st, 2014
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. typedef vector<vector<char> > Table;
  9. typedef pair<int,pair<int,int> > info;
  10. typedef priority_queue<info, vector<info>, greater<info> > PQ;
  11.  
  12. const int X[8] = {1, 1, 2, 2, -1, -1, -2, -2};
  13. const int Y[8] = {2, -2, 1, -1, 2, -2, -1, 1};
  14. const int INFINITE = 1e9;
  15.  
  16. bool posicio_correcte (int n, int m, int x, int y) {
  17.     return 0 <= x and x < n and 0 <= y and y < m;
  18. }
  19. bool backtracking (const Table& T, pair<int,int> Cav, int& D)
  20. {
  21.     int n = T.size(), m = T[0].size();
  22.     vector<vector<bool> > vis (n, vector<bool> (m, false));
  23.     vector<vector<int> > distancies (n, vector<int> (m,INFINITE));
  24.     distancies[Cav.first][Cav.second] = 0;
  25.     PQ Q;
  26.     Q.push(make_pair(0,Cav));
  27.     while (not Q.empty()) {
  28.         int dist = Q.top().first;
  29.         pair<int,int> Pos = Q.top().second;
  30.         Q.pop();
  31.         //cout << Pos.first << " " << Pos.second << endl;
  32.         if (T[Pos.first][Pos.second] == 'p') {
  33.             D = dist;
  34.             return true;
  35.         }
  36.         if (not vis[Pos.first][Pos.second]) {
  37.             vis[Pos.first][Pos.second] = true;
  38.             for (int i = 0; i < 8; ++i) {
  39.                 if (posicio_correcte(n, m, Pos.first + X[i], Pos.second + Y[i])
  40.                     and T[Pos.first + X[i]][Pos.second + Y[i]] != 'X')
  41.                     {
  42.                         //cout << "Ha entrat" << endl;
  43.                         info aux;
  44.                         aux.first = dist + 1;
  45.                         aux.second = make_pair(Pos.first + X[i],Pos.second + Y[i]);
  46.                         if (distancies[aux.second.first][aux.second.second] > aux.first) {
  47.                                 distancies[aux.second.first][aux.second.second] = aux.first;
  48.                             Q.push(aux);
  49.                         }
  50.                         else {
  51.                             distancies[aux.second.first][aux.second.second] = aux.first;
  52.                             Q.push(aux);
  53.                         }
  54.                     }
  55.             }
  56.         }
  57.     }
  58.     return false;
  59. }
  60.                
  61. void read_table (Table& T, int f, int c)
  62. {
  63.     for (int i = 0; i < f; ++i) {
  64.         for (int j = 0; j < c; ++j) {
  65.             cin >> T[i][j];
  66.         }
  67.     }
  68. }
  69. int main () {
  70.     int f, c;
  71.     while (cin >> f >> c) {
  72.         /* f i c estan entre 3 i 200*/
  73.         Table T(f, vector<char> (c));
  74.         read_table(T,f,c);
  75.         pair<int, int> Cav;
  76.         cin >> Cav.first >> Cav.second;
  77.         Cav.first -= 1;
  78.         Cav.second -= 1;       
  79.         int dist;
  80.         bool Pot_arribar = backtracking(T, Cav, dist);
  81.         if (Pot_arribar) cout << dist;
  82.         else cout << "no";
  83.         cout << endl;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement