Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- char mat[n][m];
- int si, sj;
- int ei, ej;
- vector<pair<int, int>> dzidovi;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 'S') {
- si = i;
- sj = j;
- }
- if(mat[i][j] == 'E') {
- ei = i;
- ej = j;
- }
- if(mat[i][j] == '#') {
- dzidovi.push_back(make_pair(i, j));
- }
- }
- }
- int res = -1;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- for(int i = 0; i < dzidovi.size(); i++) {
- int qi = dzidovi[i].first, qj = dzidovi[i].second;
- mat[qi][qj] = '.';
- queue<int> q;
- q.push(si);
- q.push(sj);
- q.push(0);
- vector<vector<bool>> visited(n, vector<bool>(m, false));
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- int dist = q.front();
- q.pop();
- if(ci == ei and cj == ej) {
- if(dist > res) {
- res = dist;
- break;
- }
- }
- for(int k = 0; k < 4; k++) {
- int ti = ci + di[k];
- int tj = cj + dj[k];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj] and mat[ti][tj] != '#') {
- q.push(ti);
- q.push(tj);
- q.push(dist + 1);
- visited[ti][tj] = true;
- }
- }
- }
- mat[qi][qj] = '#';
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement