Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <queue>
- #include <iostream>
- #include <vector>
- #include <cstring>
- #include <iostream>
- #include <set>
- #include <map>
- #include <cstring>
- //#include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- const int maxn = 55;
- int n, m;
- char mat[maxn][maxn];
- int bfs(vector<pair<int, int>> S, vector<pair<int, int>> E, char c) {
- queue<vector<pair<int, int>>> q;
- q.push(S);
- queue<int> q_dist;
- q_dist.push(0);
- map<vector<pair<int, int>>, bool> visited;
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- while(!q.empty()) {
- vector<pair<int, int>> piano = q.front();
- q.pop();
- int dist = q_dist.front();
- q_dist.pop();
- if(piano == E) {
- return dist;
- }
- for(int i = 0; i < 4; i++) {
- vector<pair<int, int>> t_piano = piano;
- bool ok = true;
- for(int j = 0; j < (int) t_piano.size(); j++) {
- t_piano[j].first += di[i];
- t_piano[j].second += dj[i];
- if(t_piano[j].first < 0 or t_piano[j].first >= n or t_piano[j].second < 0 or t_piano[j].second >= m) {
- ok = false;
- break;
- }
- if(mat[t_piano[j].first][t_piano[j].second] == '#') {
- ok = false;
- break;
- }
- if(isdigit(mat[t_piano[j].first][t_piano[j].second]) and mat[t_piano[j].first][t_piano[j].second] != c) {
- ok = false;
- break;
- }
- }
- if(ok and !visited[t_piano]) {
- q.push(t_piano);
- q_dist.push(dist + 1);
- visited[t_piano] = true;
- }
- }
- }
- return 2e9;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- vector<pair<int, int>> F, one, two, three;
- cin >> m >> n;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- if(mat[i][j] == 'F') {
- F.push_back({i, j});
- }
- if(mat[i][j] == '1') {
- one.push_back({i, j});
- }
- if(mat[i][j] == '2') {
- two.push_back({i, j});
- }
- if(mat[i][j] == '3') {
- three.push_back({i, j});
- }
- }
- }
- int eden = bfs(one, F, '1');
- int dva = bfs(two, F, '2');
- int tri = bfs(three, F, '3');
- int res = min(eden, min(dva, tri));
- if(res == eden) {
- cout << "1 ";
- }
- else if(res == dva) {
- cout << "2 ";
- }
- else {
- cout << "3 ";
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment