time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a picture consisting of n

rows and m

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 x

and y

are painted black.

For examples, each of these pictures contain crosses:

B. Yet Another Crosses Problem

The fourth picture contains 4 crosses: at )

, )

.

Following images don't contain crosses:

B. Yet Another Crosses Problem

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

(4

) — the number of queries.

The first line of each query contains two integers n

and 5

) — the number of rows and the number of columns in the picture.

Each of the next n

lines contains m

characters — '.' if the cell is painted white and '*' if the cell is painted black.

It is guaranteed that 4

and 5

.

Output

Print q

lines, the i

-th query, which is the minimum number of minutes you have to spend so the resulting picture contains at least one cross.

Example
Input
Copy
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
Copy
0
0
0
0
0
4
1
1
2
Note

The example contains all the pictures from above in the same order.

The first 5 pictures already contain a cross, thus you don't have to paint anything.

You can paint )

, 4

minutes.

You can paint )

on the )

.

You can paint )

on the 1

.

There are 9 possible crosses you can get in minimum time on the 9

).

 

题意:找补充次数最少的十字架

题解:暴力模拟,分别把每行每列的  *  个数存进数组,找出含有最长 * 的行列以及行列所在坐标i,j,还要判断a[i][j]是否为 .(特判),是的话次数还要减一

#include<iostream>
#include<math.h>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
vector<int>a[500004];
int xx[500005],yy[500005];
char s;
int main()
{
    int t, n, m;
    cin >> t;
    while (t--)
    {
        int x = -1, y = -1, cnt = 0,x_pos,y_pos;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                cin >> s;
                if (s == '.')
                    a[i].push_back(0);
                else
                    a[i].push_back(1);
            }
        }
        for (int i = 0; i < n; i++)
        {
            cnt = 0;
            for (int j = 0; j < m; j++)
            {
                cnt = cnt + a[i][j];
            }
            xx[i]=cnt;
            if(cnt>x)
                x=cnt;
        }
        for (int i = 0; i < m; i++)
        {
            cnt = 0;
            for (int j = 0; j < n; j++)
            {
                cnt = cnt + a[j][i];
            }
            yy[i]=cnt;
            if(cnt>y)
                y = cnt;
                
        }
        int ans=999999,p=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(xx[i]==x&&yy[j]==y&&a[i][j]==0)
                    p=1;
                ans=min(m+n-xx[i]-yy[j],ans);
            }
        }
        cout<<ans-p<<endl;
        
        for (int i = 0; i < n; i++)//清空
            a[i].clear();
    }
    // system("pause");
    return 0;
}

 

相关文章:

  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
  • 2022-01-20
  • 2021-12-01
  • 2021-11-05
  • 2022-01-26
猜你喜欢
  • 2022-02-15
  • 2021-08-15
  • 2022-02-01
  • 2021-06-27
  • 2021-07-13
相关资源
相似解决方案