Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <map>
- using namespace std;
- const vector<int> DI = {1,0,-1,0};
- const vector<int> DJ = {0,1,0,-1};
- typedef vector<vector<char>> GRAPH;
- typedef pair<pair<int,int>,int> VL;
- bool in_range(int i, int j, int n, int m) {
- return 0 <= i and 0 <= j and i < n and j < m;
- }
- int main() {
- int n, m; cin >> n >> m;
- GRAPH g(n,vector<char>(m));
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- cin >> g[i][j];
- }
- }
- int counter = 0;
- int a, b; cin >> a >> b; --a; --b;
- map<int,int> s;
- VL aux; aux.first.first = a; aux.first.second = b; aux.second = 0; bool r = in_range(a,b,n,m);
- queue<VL> q; q.push(aux);
- while (not q.empty() and r) {
- int ui = q.front().first.first; int uj = q.front().first.second;
- int d = q.front().second;
- q.pop();
- if (g[ui][uj] != 'v' and g[ui][uj] != 'X') {
- if (g[ui][uj] == 't') {
- if (s.find(d) == s.end()) {
- s.insert(make_pair(d,1));
- } else {
- map<int,int>::iterator it = s.find(d);
- it -> second = it->second+1;
- }
- ++counter;
- }
- g[ui][uj] = 'v';
- for (int i = 0; i < 4; ++i) {
- VL v;
- v.first.first = ui + DI[i];
- v.first.second = uj + DJ[i];
- if (in_range(v.first.first, v.first.second, n, m)) {
- v.second = d + 1;
- q.push(v);
- }
- }
- }
- }
- map<int,int>::const_iterator it = s.end();
- if (counter < 2) cout << "we cannot reach two or more treasures" << endl;
- else {
- --it;
- if (it -> second >= 2) cout << "second maximum distance: " << it -> first << endl;
- else {
- --it;
- cout << "second maximum distance: " << it -> first << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement