作者:
Yipxx
,
2022-01-12 21:56:20
,
所有人可见
,
阅读 5
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 55;
typedef pair<int, int> PII;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int n, m;
char g[N][N];
vector<PII> points[2];
void dfs(int x, int y, vector<PII>& ps)
{
g[x][y] = '.';
ps.push_back({x, y});
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a >= 0 && a < n && b >= 0 && b < m && g[a][b] == 'X')
dfs(a, b, ps);
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i ++ )
cin >> g[i];
for (int i = 0, k = 0; i < n; i ++ )
{
for (int j = 0; j < m; j ++ )
{
if (g[i][j] == 'X')
dfs(i, j, points[k ++]);
}
}
int res = 1e9;
for (auto x: points[0])
{
for (auto y: points[1])
res = min(res, abs(x.first - y.first) + abs(x.second - y.second) - 1);
}
cout << res << endl;
return 0;
}