AcWing 2060. 奶牛选美
原题链接
中等
作者:
贩卖日落_02
,
2025-04-09 22:37:47
· 河南
,
所有人可见
,
阅读 2
#include <iostream>
#include <cstring>
#include <algorithm>
#define x first
#define y second
using namespace std;
typedef pair<int, int> PII;
const int N = 55;
int n, m;
char g[N][N];
vector<PII> points[2];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int a, int b, vector<PII>& q)
{
q.push_back({a, b});
g[a][b] = '.';
for (int i = 0; i < 4; i ++ )
{
int x = a + dx[i], y = b + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] == 'X')
dfs(x, y, q);
}
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++ ) scanf("%s", g[i]);
int k = 0;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < m; j ++ )
if (g[i][j] == 'X')
dfs(i, j, points[k ++ ]);
int res = 110;
for (auto& a: points[0])
for (auto& b: points[1])
res = min(res, abs(a.x - b.x) + abs(a.y - b.y));
printf("%d\n", res - 1);
return 0;
}