Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <queue>
- #include <map>
- #include <vector>
- using namespace std;
- int main() {
- int n, m;
- cin >> n >> m;
- vector<vector<char>> mat(n, vector<char>(m));
- bool visited[n][m];
- int si, sj;
- int ei, ej;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- visited[i][j] = false;
- if(mat[i][j] == 'S') {
- si = i;
- sj = j;
- }
- if(mat[i][j] == 'E') {
- ei = i;
- ej = j;
- }
- }
- }
- queue<int> q;
- q.push(si);
- q.push(sj);
- q.push(0);
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- int cekor = q.front();
- q.pop();
- if(ci == ei and cj == ej) {
- cout << cekor << endl;
- return 0;
- }
- if(ci + 1 < n and mat[ci + 1][cj] != '#' and !visited[ci + 1][cj]) {
- visited[ci + 1][cj] = true;
- q.push(ci + 1);
- q.push(cj);
- q.push(cekor + 1);
- }
- if(ci - 1 >= 0 and mat[ci - 1][cj] != '#' and !visited[ci - 1][cj]) {
- visited[ci - 1][cj] = true;
- q.push(ci - 1);
- q.push(cj);
- q.push(cekor + 1);
- }
- if(cj + 1 < m and mat[ci][cj + 1] != '#' and !visited[ci][cj + 1]) {
- visited[ci][cj + 1] = true;
- q.push(ci);
- q.push(cj + 1);
- q.push(cekor + 1);
- }
- if(cj - 1 >= 0 and mat[ci][cj - 1] != '#' and !visited[ci][cj - 1]) {
- visited[ci][cj - 1] = true;
- q.push(ci);
- q.push(cj - 1);
- q.push(cekor + 1);
- }
- }
- return 0;
- }
- /**
- ....S##...
- E....##..
- ##..##
- ...###...
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement