其他分享
首页 > 其他分享> > Educational codeforces Round68 Div.2 Yet Another Crosses Problem

Educational codeforces Round68 Div.2 Yet Another Crosses Problem

作者:互联网

题目描述

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

标签:picture,Educational,Crosses,Round68,int,contains,number,++,black
来源: https://www.cnblogs.com/miaoshengyou/p/11190677.html