Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <queue>
- using namespace std;
- int inf = 2000000;
- vector<int> dx = { 1, 0, -1, 0 };
- vector<int> dy = { 0, 1, 0, -1 };
- queue<pair<int, int>> q;
- void bfs(vector<vector<int>>& map) {
- while (!q.empty()) {
- pair<int, int> v, u;
- v = q.front();
- u = v;
- q.pop();
- for (int i = 0; i < 4; ++i) {
- u.first += dx[i];
- u.second += dy[i];
- if (u.first > 0 && u.first < map.size() - 1 && u.second > 0 && u.second < map[0].size() - 1 && map[u.first][u.second] != -1) {
- if (map[u.first][u.second] > map[v.first][v.second] + 1) {
- map[u.first][u.second] = min(map[u.first][u.second], map[v.first][v.second] + 1);
- q.push(u);
- }
- }
- u.first -= dx[i];
- u.second -= dy[i];
- }
- }
- }
- int main() {
- std::ifstream fin("input.txt");
- std::ofstream fout("output.txt");
- int n, m;
- fin >> n >> m;
- vector<vector<int>> map(n, vector<int>(m, 0)), pmap(n, vector<int>(m, 0));
- pair<int, int> tiger;
- for (int i = 0; i < n; ++i) {
- for (int j = 0; j < m; ++j) {
- char c;
- fin >> c;
- if (c == '#') {
- map[i][j] = -1;
- pmap[i][j] = -1;
- }
- else {
- map[i][j] = inf;
- pmap[i][j] = inf;
- }
- if (c == 'T') {
- tiger.first = i; tiger.second = j;
- map[i][j] = 0;
- pmap[i][j] = inf;
- }
- }
- }
- q.push(tiger);
- bfs(map);
- tiger.first = 1; tiger.second = 1, pmap[1][1] = 0;
- q.push(tiger);
- bfs(pmap);
- fout << pmap[n - 2][m - 2] << endl;
- if (pmap[n - 2][m - 2] >= map[n - 2][m - 2]) {
- fout << "No" << endl;
- }
- else {
- fout << "Yes" << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment