Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://codeforces.com/gym/103269/problem/I
- #include <bits/stdc++.h>
- using namespace std;
- int n, m;
- vector<string> v;
- vector<vector<vector<int>>> l;
- void bfs(int index, int x, int y, int dep = 0) {
- l[index][x][y] = dep;
- if(x + 1 < n and l[index][x + 1][y] > dep + 1 and v[x + 1][y] == '.') {
- bfs(index, x + 1, y, dep + 1);
- }
- if(x - 1 >= 0 and l[index][x - 1][y] > dep + 1 and v[x - 1][y] == '.') {
- bfs(index, x - 1, y, dep + 1);
- }
- if(y + 1 < m and l[index][x][y + 1] > dep + 1 and v[x][y + 1] == '.') {
- bfs(index, x, y + 1, dep + 1);
- }
- if(y - 1 >= 0 and l[index][x][y - 1] > dep + 1 and v[x][y - 1] == '.') {
- bfs(index, x, y - 1, dep + 1);
- }
- return;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cin >> n >> m;
- v = vector<string>(n);
- for(int i = 0; i < n; i++) {
- cin >> v[i];
- }
- l = vector<vector<vector<int>>>(4, vector<vector<int>>(n, vector<int>(m, INT_MAX)));
- pair<int, int> a1, a2, b1, b2;
- cin >> a1.first >> a1.second;
- cin >> a2.first >> a2.second;
- cin >> b1.first >> b1.second;
- cin >> b2.first >> b2.second;
- bfs(0, a1.first, a1.second);
- bfs(1, a2.first, a2.second);
- bfs(2, b1.first, b1.second);
- bfs(3, b2.first, b2.second);
- int ans = INT_MAX;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- if(l[0][i][j] == INT_MAX or l[1][i][j] == INT_MAX or l[2][i][j] == INT_MAX or l[3][i][j] == INT_MAX) {
- continue;
- }
- //cout << i << " " << j << "\n";
- ans = min(ans, l[0][i][j] + l[1][i][j] + l[2][i][j] + l[3][i][j]);
- }
- }
- if(ans == INT_MAX) {
- cout << "IMPOSSIBLE\n";
- } else {
- cout << ans << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement