暴力出奇迹(第二周限时训练)

题目链接:

You are given a picture consisting of nn rows and mm columns. Rows are numbered from 11 to nn 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 yy, where 1xn1≤x≤n and 1ym1≤y≤m, such that all cells in row xx and all cells in column yy are painted black.

For examples, each of these pictures contain crosses:

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

Following images don't contain crosses:

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 qq (1q51041≤q≤5⋅104) — the number of queries.

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

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

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

Output

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

Example

Input
9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**
Output
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 (1,3)(1,3), (3,1)(3,1), (5,3)(5,3) and (3,5)(3,5) on the 66-th picture to get a cross in (3,3)(3,3). That'll take you 44 minutes.

You can paint (1,2)(1,2) on the 77-th picture to get a cross in (4,2)(4,2).

You can paint (2,2)(2,2) on the 88-th picture to get a cross in (2,2)(2,2). You can, for example, paint (1,3)(1,3), (3,1)(3,1) and (3,3)(3,3) to get a cross in (3,3)(3,3) but that will take you 33 minutes instead of 11.

There are 9 possible crosses you can get in minimum time on the 99-th picture. One of them is in (1,1)(1,1): paint (1,2)(1,2) and (2,1)(2,1).

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;

string g[50001];

ll cnt[50001];

int main(){
	ll q;
	cin>>q;
	while(q--){
		ll n,m;
		cin>>n>>m;
		for(int i=1;i<=n;i++){
			cin>>g[i];
			g[i]='@'+g[i];
		}
		memset(cnt,0,sizeof(cnt));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				if(g[i][j]=='.'){
					cnt[i]++;//存储每行需要涂的个数 
				}
			}
		}
		ll min_s=INF;
		for(int i=1;i<=m;i++){//用每一列去遍历每一行 
			ll cn=0;
			for(int j=1;j<=n;j++){
				if(g[j][i]=='.'){
					cn++;
			}
			}
			for(int k=1;k<=n;k++){
				if(g[k][i]=='.'){
					min_s=min(min_s,cn+cnt[k]-1);
				}
				else min_s=min(min_s,cn+cnt[k]);
			}
		}
		cout<<min_s<<endl;
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/akpower/p/11285235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值