Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <random>
- #include <vector>
- #include <deque>
- using namespace std;
- const long long MOD = 1e9 + 7, N = 17;
- long long matr[52][52], col[52][52], vis[52][52], conn[20][20], d[52][52], n, m, cnt;
- bool fl;
- char z;
- long long dp[1 << 20][20];
- vector<pair<int, int>> q;
- deque<pair<int, int>> q1;
- int check(int mask, int u) {
- return mask & (1 << u);
- }
- int main()
- {
- cin >> n >> m;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> z;
- if (z == 'X') {
- matr[i][j] = 1;
- }
- if (z == 'S') {
- matr[i][j] = 0;
- }
- if (z == '.') {
- matr[i][j] = -1;
- }
- }
- }
- cnt = 1;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (matr[i][j] == 1 && col[i][j] == 0) {
- col[i][j] = cnt;
- q.clear();
- q.push_back({ i,j });
- int l = 0;
- while (l < q.size()) {
- int x = q[l].first;
- int y = q[l].second;
- col[x][y] = cnt;
- if (x > 0) {
- if (!vis[x - 1][y] && matr[x - 1][y] > 0) {
- q.push_back({ x - 1,y });
- vis[x - 1][y] = 1;
- }
- }
- if (y > 0) {
- if (!vis[x][y - 1] && matr[x][y - 1] > 0) {
- q.push_back({ x,y - 1 });
- vis[x][y - 1] = 1;
- }
- }
- if (y < m) {
- if (!vis[x][y + 1] && matr[x][y + 1] > 0) {
- q.push_back({ x,y + 1 });
- vis[x][y + 1] = 1;
- }
- }
- if (x < n) {
- if (!vis[x + 1][y] && matr[x + 1][y] > 0) {
- q.push_back({ x + 1,y });
- vis[x + 1][y] = 1;
- }
- }
- l++;
- }
- cnt++;
- }
- }
- }
- for (int i = 0; i < cnt; i++) {
- for (int j = 0; j < cnt; j++) {
- conn[i][j] = 1e15;
- }
- }
- for (int v = 0; v < cnt - 1; v++) {
- q1.clear();
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- if (col[i][j] - 1 == v) {
- d[i][j] = 0;
- q1.push_back({ i,j });
- vis[i][j] = 1;
- }
- else {
- d[i][j] = 122;
- vis[i][j] = 0;
- }
- }
- }
- while (!q1.empty()) {
- int x = q1.front().first;
- int y = q1.front().second;
- q1.pop_front();
- if (x >= n || y >= m) {
- continue;
- }
- if (x > 0) {
- if (matr[x - 1][y] == 0) {
- d[x - 1][y] = min(d[x - 1][y], d[x][y] + 1);
- if (!vis[x - 1][y]) {
- q1.push_back({ x - 1,y });
- vis[x - 1][y] = 1;
- }
- }
- if (matr[x - 1][y] == 1) {
- d[x - 1][y] = min(d[x - 1][y], d[x][y]);
- if (!vis[x - 1][y]) {
- q1.push_front({ x - 1,y });
- vis[x - 1][y] = 1;
- }
- }
- }
- if (y > 0) {
- if (matr[x][y - 1] == 0) {
- d[x][y - 1] = min(d[x][y - 1], d[x][y] + 1);
- if (!vis[x][y - 1]) {
- q1.push_back({ x, y - 1 });
- vis[x][y - 1] = 1;
- }
- }
- if (matr[x][y - 1] == 1) {
- d[x][y - 1] = min(d[x][y - 1], d[x][y]);
- if (!vis[x][y - 1]) {
- q1.push_front({ x,y - 1 });
- vis[x][y - 1] = 1;
- }
- }
- }
- if (y < m) {
- if (matr[x][y + 1] == 0) {
- d[x][y + 1] = min(d[x][y + 1], d[x][y] + 1);
- if (!vis[x][y + 1]) {
- q1.push_back({ x,y + 1 });
- vis[x][y + 1] = 1;
- }
- }
- if (matr[x][y + 1] == 1) {
- d[x][y + 1] = min(d[x][y + 1], d[x][y]);
- if (!vis[x][y + 1]) {
- q1.push_front({ x,y + 1 });
- vis[x][y + 1] = 1;
- }
- }
- }
- if (x < n) {
- if (matr[x + 1][y] == 0) {
- d[x + 1][y] = min(d[x + 1][y], d[x][y] + 1);
- if (!vis[x + 1][y]) {
- q1.push_back({ x + 1,y });
- vis[x + 1][y] = 1;
- }
- }
- if (matr[x + 1][y] == 1) {
- d[x + 1][y] = min(d[x + 1][y], d[x][y]);
- if (!vis[x + 1][y]) {
- q1.push_front({ x + 1,y });
- vis[x + 1][y] = 1;
- }
- }
- }
- }
- for (int i1 = 0; i1 < n; i1++) {
- for (int j1 = 0; j1 < m; j1++) {
- if (matr[i1][j1] == 1) {
- conn[v][col[i1][j1] - 1] = min(conn[v][col[i1][j1] - 1], d[i1][j1]);
- }
- }
- }
- }
- for (int i = 0; i <= (1 << (cnt)); i++) {
- for (int j = 0; j < cnt; j++) {
- dp[i][j] = 1e15;
- }
- }
- for (int i = 0; i < cnt - 1; i++) {
- dp[1 << i][i] = 0;
- }
- /*for (int mask = 1; mask < 1 << (cnt); mask++) {
- for (int v = 0; v < cnt-1; v++) {
- if (check(mask, v)) {
- for (int u = 0; u < cnt-1; u++) {
- if (!check(mask, u)) {
- dp[mask ^ (1 << u)][u] = min(dp[mask ^ (1 << u)][u], dp[mask][v] + conn[u][v]);
- }
- }
- }
- }
- }*/
- for (int mask = 1; mask < (1 << (cnt - 1)) - 1; mask++) {
- for (int v = 0; v < (cnt - 1); v++) {
- if (check(mask, v)) {
- for (int u = 0; u < cnt - 1; u++) {
- if (!check(mask, u)) {
- dp[mask ^ (1 << u)][u] = min(dp[mask ^ (1 << u)][u], dp[mask][v] + conn[v][u]);
- }
- }
- }
- }
- }
- long long ans = 1e15;
- for (int i = 0; i < cnt - 1; i++) {
- ans = min(ans, dp[(1 << (cnt - 1)) - 1][i]);
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment