Codeforces Round #766 (Div. 2) A. Not Shading 翻译 题解

原题地址:https://codeforces.com/contest/1627/problem/A
A. Not Shading

time limit per test: 1 second
memory limit per test: 256 megabytes
input: standard input
output: standard output

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

有一个nm列的网格。一些格子是黑色的,其余的格子是白色的。

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.

给定两个整数rc,找出使第rc列的单元格变成黑色所需的最小操作数,或者确定这是不可能的。

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.

输入由多个测试用例组成。第一行包含一个整数t (1≤t≤100)表示测试用例的数量。每组测试用例的描述如下。

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.

每组测试用例的第一行包含四个整数n,m, r,c (1≤n,m≤50; 1≤r≤n; 1≤c≤m),表示网格的行数和列数,以及需要变成黑色的单元格所在行和列。

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

接下来n行,每一行包含m个字符。这些字符中的每一个都是BW,表示一个黑色或白色的单元格。

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

对于每个测试用例,如果不可能将第rc列的单元格变成黑色,输出−1

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

否则,输出一个整数——使第rc列的单元格变为黑色所需的最小操作数。

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.
我们可以取黑色单元格(1,2),并使其所在行变为黑色。这样,单元格(1,4)变成黑色。
在这里插入图片描述

In the second test case, the cell in row 2 and column 1 is already black.
在第二个测试用例中,单元格(2,1)已经是黑色的。

In the third test case, it is impossible to make the cell in row 2 and column 2 black.
在第三个测试用例中,不可能使单元格(2,2)变成黑色。

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.
这样,单元格(1,1)变成黑色。

思路:
①如果单元格全为白色,输出-1;
②如果(r,c)已经是黑色,输出0;
③如果(r,c)所在的行或列有黑色单元格,只需要一步,输出1;
④对于③,如果没有同行或同列的黑色单元格,可以取任意一个黑色单元格,将其所在行或所在列变为黑色,然后符合③的情况,需要两步,输出2;

AC 代码

#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define mem(a) memset(a,0,sizeof(a));
using namespace std;
char matx[60][60]; 
int main(){
	int T;
	cin>>T;
	int n,m,r,c;
	bool posb;
	while(T--){
		mem(matx);
		posb=false;
		cin>>n>>m>>r>>c;
		r--;c--;
		FOR(i,0,n-1){
			FOR(j,0,m-1){
				cin>>matx[i][j];
				if(!posb and matx[i][j]=='B'){
					posb=true;
				}
			}
		}
		
		if(!posb){
			cout<<-1<<endl;
			continue;
		}
		
		if(matx[r][c]=='B'){
			cout<<0<<endl;
			continue;
		}
		
		{
			bool same=false;
			FOR(i,0,n-1){
				if(matx[i][c]=='B'){
					same=true;
					break;
				}
			}
			FOR(i,0,m-1){
				if(matx[r][i]=='B'){
					same=true;
					break;
				}
			}
			if(same){
				cout<<1<<endl;
				continue;
			}
		}
		
		cout<<2<<endl;
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值