Yet Another Crosses Problem_被教育场(B题)_思维/经验_Educational Codeforces Round 68 [Rated for Div. 2]

B. Yet Another Crosses Problem

题目限制:

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input outputstandard output

主干:
You are given a picture consisting of n rows and m columns. Rows are numbered from 1 to n from the top to the bottom, columns are numbered from 1 to 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, where 1≤x≤n and 1≤y≤m, such that all cells in row x and all cells in column y are painted black.

For examples, each of these pictures contain crosses:
在这里插入图片描述

The fourth picture contains 4 crosses: at (1,3), (1,5), (3,3) and (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 q (1≤q≤5⋅104) — the number of queries.

The first line of each query contains two integers n and m (1≤n,m≤5⋅104, n⋅m≤4⋅105) — 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 ∑n≤5⋅104 and ∑n⋅m≤4⋅105.

Output
Print q lines, 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.

Example
输入

9
5 5
..*..
..*..
*****
..*..
..*..
3 4
****
.*..
.*..
4 3
***
*..
*..
*..
5 5
*****
*.*.*
*****
..*.*
..***
1 4
****
5 5
.....
..*..
.***.
..*..
.....
5 3
...
.*.
.*.
***
.*.
3 3
.*.
*.*
.*.
4 4
*.**
....
*.**
*.**

输出

0
0
0
0
0
4
1
1
2

题目大意:
给你n*m大小的区域,满足填满区域的十字(就是说边上无空白),我们现在有刷子和一桶油漆,请问我们需要刷多少次才能够满足黑十字(上述条件),一次只能刷一格;

代码借鉴了,cf中的红名dalao,不得不说代码实在是太强了。
Mingrui Liu
想法:
记录每行/每列的空白数,在从中找出最小的,a[i] + b[j] - (s[i*m + j] == ‘.’),且用ans,与之前的不断对比,找出最小。

#include<iostream>
#include<math.h>
#include<cmath>
#include<algorithm>

#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

const int N = 4e5 + 5;
int T, n, m, a[N], b[N];
char s[N];

int main() {

	for (cin >> T; T--;) {
		cin >> n >> m;
		rep(i, n)scanf("%s", s + i * m);
		rep(i, n)a[i] = 0; rep(j, m)b[j] = 0;
		rep(i, n)rep(j, m)if (s[i*m + j] == '.')++a[i], ++b[j];
		//a[i]/b[j]分别用于记录行/列内的空白部分个数,在这其中找出最小的数。
		int ans = 1e9 + 7;
		rep(i, n)rep(j, m)ans = min(ans, a[i] + b[j] - (s[i*m + j] == '.'));
		//这里是最重要的地方,找出最小,
		//两点重合时(就是例如空白十字的中心)需要-1
		printf("%d\n", ans);
	}

	return 0;
}

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值