Advertisement
Guest User

Tesoros2.cpp

a guest
Dec 21st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1.  
  2. #include <iostream>
  3. #include <vector>
  4. #include <queue>
  5. using namespace std;
  6.  
  7. typedef vector<vector<char> > Mapa;
  8. typedef vector<vector<int> >  Dapa;
  9. typedef pair<int,int> P;
  10.  
  11. const int X[4] = {1, 0, -1, 0};
  12. const int Y[4] = {0, 1, 0, -1};
  13.  
  14. int main(){
  15.     int n,m,x,y;
  16.     cin >> n >> m;
  17.     Mapa map(n,vector<char>(m,'.'));
  18.     Dapa dap(n,vector<int> (m,0));
  19.     for(int i = 0; i < n; ++i){
  20.         for(int j = 0; j < m; ++j){
  21.             cin >> map[i][j];
  22.         }
  23.     }
  24.     cin >> x >> y;
  25.     --x; --y;
  26.     // Distancia inicial
  27.     dap[x][y] == 0;
  28.     // Posicion inicial
  29.     P inicial;
  30.     inicial.first = x;
  31.     inicial.second = y;
  32.     queue<P> Q;
  33.     Q.push(inicial);
  34.     int res = 0;
  35.     bool trobat = false;
  36.     // mientras no hayamos encontrado tesoros y la cola no esté vacía...
  37.     while(not trobat and not Q.empty()){
  38.         P act = Q.front();
  39.         Q.pop();
  40.         if(map[act.first][act.second] == 't'){
  41.             trobat = true;
  42.             res = dap[act.first][act.second];
  43.         }else if(map[act.first][act.second] == '.'){
  44.             map[act.first][act.second] = 'v';
  45.             // Cuatro direcciones;
  46.             for(int i = 0; i < 4; ++i){
  47.                 int xx = act.first + X[i];
  48.                 int yy = act.second + Y[i];
  49.                 // si la posicion que miramos es válida...
  50.                 if(xx >= 0 and xx < map.size() and yy >= 0 and yy < map[0].size()){
  51.                     if(map[xx][yy] == '.' or map[xx][yy] == 't'){
  52.                         Q.push(P(xx,yy));
  53.                         dap[xx][yy] = dap[act.first][act.second] + 1;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     if(trobat) cout << "distancia minima: " << res << endl;
  60.     else cout << "no es pot arribar a cap tresor" << endl;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement