AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 校园
  • 应用
  • 文章
    • 题解
    • 分享
    • 问答
  • 吐槽
  • 登录/注册

AcWing 2060. 奶牛选美 0.07 AC币

作者: 作者的头像   Yipxx ,  2022-01-12 21:56:20 ,  所有人可见 ,  阅读 5


0


#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;
}

0 评论

你确定删除吗?
1024
x

© 2018-2022 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息