Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- const int x0 = 0;
- const int y0 = 0;
- typedef vector<char> VC;
- typedef vector<VC> VVC;
- typedef vector<int> VI;
- typedef vector<VI> VVI;
- typedef pair<int, int> II;
- int n, m, Ty, Tx;
- VVC t;
- VVI d, p;
- bool valid(int y, int x){
- if (y == Ty and x == Tx) return true;
- return (y >= 0 and x >= 0 and y < n and x < m and t[y][x] != '#');
- }
- int main(){
- while (cin >> n >> m){
- Ty = Tx = -1;
- t = VVC(n, VC(m));
- for (int y = 0; y < n; y++) for (int x = 0; x < m; x++){
- cin >> t[y][x];
- if (t[y][x] == 'T'){
- Ty = y; Tx = x;
- }
- }
- if (Ty == -1 and Tx == -1) cout << "The telecos ran away." << endl;
- else {
- queue<II> q;
- d = VVI(n, VI(m, -1));
- p = VVI(n, VI(m, -1));
- d[y0][x0] = 0;
- p[y0][x0] = (t[y0][x0] == 'P' ? 1 : 0);
- q.push(II(y0, x0));
- while (not q.empty()){
- II pp = q.front();
- q.pop();
- int y = pp.first, x = pp.second;
- int td = d[y][x] + 1, pers = p[y][x];
- y--;if(valid(y, x)){
- int tp = pers + (t[y][x] == 'P' ? 1 : 0);
- if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
- d[y][x] = td;
- p[y][x] = tp;
- q.push(II(y, x));
- }
- }y++;
- x--;if(valid(y, x)){
- int tp = pers + (t[y][x] == 'P' ? 1 : 0);
- if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
- d[y][x] = td;
- p[y][x] = tp;
- q.push(II(y, x));
- }
- }x++;
- y++;if(valid(y, x)){
- int tp = pers + (t[y][x] == 'P' ? 1 : 0);
- if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
- d[y][x] = td;
- p[y][x] = tp;
- q.push(II(y, x));
- }
- }y--;
- x++;if(valid(y, x)){
- int tp = pers + (t[y][x] == 'P' ? 1 : 0);
- if (d[y][x] == -1 or d[y][x] > td or (d[y][x] == td and p[y][x] < tp)){
- d[y][x] = td;
- p[y][x] = tp;
- q.push(II(y, x));
- }
- }x--;
- }
- if (d[Ty][Tx] == -1) cout << "The telecos is hidden." << endl;
- else cout << d[Ty][Tx] << " " << p[Ty][Tx] << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement