Educational codeforces Round68 Div.2 Yet Another Crosses Problem

博客围绕一个图片染色问题展开,给定由黑白单元格组成的图片,若图片中有十字(某行和某列全为黑色)则认为有趣。可将白色单元格染黑,需计算使图片包含至少一个十字的最少染色时间。这是模拟题,通过遍历找每行每列染色块数来求解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

You are given a picture consisting of n rows and mm columns. Rows are numbered from 11 to n from the top to the bottom, columns are numbered from 11 to mm from the left to the right. Each cell is painted either black or white.

You think that this picture is not interesting enough. You consider a picture to be interesting if there is at least one cross in it. A cross is represented by a pair of numbers xx and y, where 1≤x≤n1≤x≤n and 1≤y≤m1≤y≤m, such that all cells in row xx and all cells in column y are painted black.

For examples, each of these pictures contain crosses:

img

The fourth picture contains 4 crosses: at (1,3), (1,5), (3,3)and(3,5).

Following images don't contain crosses:

img

You have a brush and a can of black paint, so you can make this picture interesting. Each minute you may choose a white cell and paint it black.

What is the minimum number of minutes you have to spend so the resulting picture contains at least one cross?

You are also asked to answer multiple independent queries.

Input

The first line contains an integer q (1≤q≤5⋅1041≤q≤5⋅104) — the number of queries.

The first line of each query contains two integers n and mm (1≤n,m≤5⋅1041≤n,m≤5⋅104, n⋅m≤4⋅105n⋅m≤4⋅105) — the number of rows and the number of columns in the picture.

Each of the next n lines contains mm characters — '.' if the cell is painted white and '*' if the cell is painted black.

It is guaranteed that ∑n≤5⋅104∑n≤5⋅104 and ∑n⋅m≤4⋅105∑n⋅m≤4⋅105.

Output

Print qlines, the i-th line should contain a single integer — the answer to the i-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.

题目分析

这是一道模拟题(implementation),直接按照要求做就是了。不断遍历,找到每行每列有的染色的块数,这样就可以得到任意以(x, y)为节需要染色的时间,找到最小的就是所求答案。

源代码

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int times;

    cin>>times;

    while(times--){
        int n, m; //行数、列数和输出结果
        cin >> n >> m;

        vector<string> map(n); //待输入的图片地图
        vector<int> row(n, 0); //保存每行存在的*的个数
        vector<int> column(m, 0); //保存每列存在的*的个数

        //输入地图
        for(int i = 0; i < n; i++){
            cin >> map[i];
        }

        //计算每行每列的*号数
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                if(map[i][j] == '*'){
                    row[i]++;
                    column[j]++;
                }
            }
        }

        //找出指定一行一列中*号数最多的搭配
        int res = 2e9;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                int toAdd = (m - row[i]) + (n - column[j]) - 1; //各个点产生交叉需要的最小时间
                if(map[i][j] == '*'){ //如果节上涂了
                    toAdd++;
                }
                res = min(res, toAdd);
            }
        }

        //输出结果
        cout << res << endl;
    }
}

转载于:https://www.cnblogs.com/miaoshengyou/p/11190677.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值