Advertisement
Guest User

Searching for the telecos

a guest
Jan 16th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4.  
  5. using namespace std;
  6.  
  7. const int x0 = 0;
  8. const int y0 = 0;
  9.  
  10. typedef vector<char> VC;
  11. typedef vector<VC> VVC;
  12. typedef vector<int> VI;
  13. typedef vector<VI> VVI;
  14. typedef pair<int, int> II;
  15.  
  16. int n, m, Ty, Tx;
  17. VVC t;
  18. VVI d, p;
  19.  
  20. bool valid(int y, int x){
  21.     if (y == Ty and x == Tx) return true;
  22.     return (y >= 0 and x >= 0 and y < n and x < m and t[y][x] != '#');
  23. }
  24.  
  25. int main(){
  26.     while (cin >> n >> m){
  27.         Ty = Tx = -1;
  28.         t = VVC(n, VC(m));
  29.         for (int y = 0; y < n; y++) for (int x = 0; x < m; x++){
  30.             cin >> t[y][x];
  31.             if (t[y][x] == 'T'){
  32.                 Ty = y; Tx = x;
  33.             }
  34.         }
  35.         if (Ty == -1 and Tx == -1) cout << "The telecos ran away." << endl;
  36.         else {
  37.             queue<II> q;
  38.             d = VVI(n, VI(m, -1));
  39.             p = VVI(n, VI(m, -1));
  40.  
  41.             d[y0][x0] = 0;
  42.             p[y0][x0] = (t[y0][x0] == 'P' ? 1 : 0);
  43.             q.push(II(y0, x0));
  44.  
  45.             while (not q.empty()){
  46.                 II pp = q.front();
  47.                 q.pop();
  48.                 int y = pp.first, x = pp.second;
  49.                 int td = d[y][x] + 1, pers = p[y][x];
  50.  
  51.                 y--;if(valid(y, x)){
  52.                     int tp = pers + (t[y][x] == 'P' ? 1 : 0);
  53.                     if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
  54.                         d[y][x] = td;
  55.                         p[y][x] = tp;
  56.                         q.push(II(y, x));
  57.                     }
  58.                 }y++;
  59.                 x--;if(valid(y, x)){
  60.                     int tp = pers + (t[y][x] == 'P' ? 1 : 0);
  61.                     if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
  62.                         d[y][x] = td;
  63.                         p[y][x] = tp;
  64.                         q.push(II(y, x));
  65.                     }
  66.                 }x++;
  67.                 y++;if(valid(y, x)){
  68.                     int tp = pers + (t[y][x] == 'P' ? 1 : 0);
  69.                     if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
  70.                         d[y][x] = td;
  71.                         p[y][x] = tp;
  72.                         q.push(II(y, x));
  73.                     }
  74.                 }y--;
  75.                 x++;if(valid(y, x)){
  76.                     int tp = pers + (t[y][x] == 'P' ? 1 : 0);
  77.                     if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
  78.                         d[y][x] = td;
  79.                         p[y][x] = tp;
  80.                         q.push(II(y, x));
  81.                     }
  82.                 }x--;
  83.  
  84.             }
  85.             if (d[Ty][Tx] == -1) cout << "The telecos is hidden." << endl;
  86.             else cout << d[Ty][Tx] << " " << p[Ty][Tx] << endl;
  87.         }
  88.     }
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement