寒假养成计划——Day4

A题

题目链接

There is a grid with n rows and m columns. Some cells are colored black, and the rest of the cells are colored white.

In one operation, you can select some black cell and do exactly one of the following:

  • color all cells in its row black, or
  • color all cells in its column black.

You are given two integers r and c. Find the minimum number of operations required to make the cell in row r and column c black, or determine that it is impossible.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains four integers n, m, r, and c (1≤n,m≤50; 1≤r≤n; 1≤c≤m) — the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively.

Then n lines follow, each containing m characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.

Output

For each test case, if it is impossible to make the cell in row r and column c black, output −1.

Otherwise, output a single integer — the minimum number of operations required to make the cell in row r and column c black.

Example

input

9
3 5 1 4
WBWWW
BBBWB
WWBBB
4 3 2 1
BWW
BBW
WBB
WWB
2 3 2 2
WWW
WWW
2 2 1 1
WW
WB
5 9 5 9
WWWWWWWWW
WBWBWBBBW
WBBBWWBWW
WBWBWBBBW
WWWWWWWWW
1 1 1 1
B
1 1 1 1
W
1 2 1 1
WB
2 1 1 1
W
B

output

1
0
-1
2
2
0
-1
1
1

Note

The first test case is pictured below.

We can take the black cell in row 1 and column 2, and make all cells in its row black. Therefore, the cell in row 1 and column 4 will become black.

In the second test case, the cell in row 2 and column 1 is already black.

In the third test case, it is impossible to make the cell in row 2 and column 2 black.

The fourth test case is pictured below.

We can take the black cell in row 2 and column 2 and make its column black.

Then, we can take the black cell in row 1 and column 2 and make its row black.

Therefore, the cell in row 1 and column 1 will become black.


题解:

        这道题目理解起来不是很困难,就是说每次操作只能在有黑色块上进行整行或整列涂黑,问至少需要多少次才能使目标为(r, c)的位置上变黑。首先明确一点,这道题的答案只有4个:-1,0,1,2。分情况讨论即可(这个部分过于简单就不做过多的描述了)。

AC代码:

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

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int m,n,r,c;
		char cc[55][55];
		int cnt=0,zero=0,one=0;
		scanf("%d%d%d%d",&n,&m,&r,&c);
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<=m;j++)
			{
				cin>>cc[i][j];
				if (cc[i][j]=='B')
					cnt++;
				if (i==r&&j==c&&cc[i][j]=='B')
					zero=1;
			}	
		}
		if (cnt==0)
			puts("-1");
		else if (zero)
			puts("0");
		else
		{
			for (int i=1;i<=n;i++)
			{
				if (cc[i][c]=='B')
				{
					one=1;
					break;
				}
			}
			for (int i=1;i<=m;i++)
			{
				if (cc[r][i]=='B')
				{
					one=1;
					break;
				}
			}
			if (one)
				puts("1");
			else
				puts("2");
		}
	}
	return 0;
}

B题

题目链接

Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a n×m grid. The seat in row r and column c is denoted by (r,c), and the distance between two seats (a,b) and (c,d) is |a−c|+|b−d|.

As the class president, Tina has access to exactly k buckets of pink paint. The following process occurs.

  • First, Tina chooses exactly k seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat.
  • After Tina has painted k seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink.
  • After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul.

Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement!

Now, Rahul wonders for k=0,1,…,n⋅m−1, if Tina has kk buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity!

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤5⋅10^{4}) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n, m (2≤n⋅m≤10^{5}) — the number of rows and columns of seats in the classroom.

The sum of n⋅m across all test cases does not exceed 10^{5}.

Output

For each test case, output n⋅mn⋅m ordered integers — the distance between Rahul and Tina if both of them act optimally for every k∈[0,n⋅m−1].

Example

input

2
4 3
1 2

output

3 3 4 4 4 4 4 4 5 5 5 5 
1 1 

Note

One possible sequence of choices for the first testcase where Tina has k=3 buckets of paints is as follows.

Tina paints the seats at positions (1,2), (2,2), (3,2) with pink paint. Rahul chooses the seat at (3,1) after which Tina chooses to sit at (1,3).

Therefore, the distance between Tina and Rahul is |3−1|+|1−3|=4, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well.

For k=0 in the first test case, Rahul can decide to sit at (2,2) and Tina can decide to sit at (4,3) so the distance between them would be |2−4|+|2−3|=3.

Below are pictorial representations of the k=3 and k=0 cases for the first test case.

 

A possible seating arrangement for k=3.A possible seating arrangement for k=0.


题解:

        这道题目比较的偏思维,重点是要找到一个重要的关系:距离的计算公式。首先先明确一个事情:这个k从0增长到n×m-1的过程中,距离d是不会递减的(毕竟二人足够的聪明),所以最后得到的序列一定是一个非递减序列。二者的距离计算公式在上文也给出了,一个绝对值之和的关系,也就是说,两个人的距离是根据相对的坐标决定的。不难看出,这个距离的最大值就是max(i,n-i-1)+max(j,m-j-1)(这个画画图稍微算算就知道结果了),带入即可。

AC代码:

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

const int M=1e5+5;
int a[M];

int main()
{
	int t;
	scanf("%d",&t);
	while (t--)
	{
		int n,m;
		int x=0;
		scanf("%d%d",&n,&m);
		for (int i=0;i<n;i++)
		{
			for (int j=0;j<m;j++)
				a[x++]=max(i,n-i-1)+max(j,m-j-1);	
		}
		sort(a,a+x);
		for (int i=0;i<n*m;i++)
			printf("%d ",a[i]);
		printf("\n");
	}
	return 0;
}

ps:本人实力较菜,只能做出这些简单题,后面的题如果有哪位大神可以解决可以私信我,如果这里面有讲的不清楚或者有错误的,望及时指出!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值