No.13 Not Shading

题目来源:https://acs.jxnu.edu.cn/contest/23/board/challenge/Ahttps://acs.jxnu.edu.cn/contest/23/board/challenge/A

原文:

Not Shading

描述:

There is a grid with nn rows and mm 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 rr and cc. Find the minimum number of operations required to make the cell in row rr and column cc black, or determine that it is impossible.

输入:

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

The first line of each test case contains four integers nn, mm, rr, and cc (1≤n,m≤501≤n,m≤50; 1≤r≤n1≤r≤n; 1≤c≤m1≤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 nn lines follow, each containing mm characters. Each of these characters is either 'B' or 'W' — a black and a white cell, respectively.

输出:

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

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

样例输入:

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

复制

样例输出:

1
0
-1
2
2
0
-1
1
1

译文:

着色

描述:

现在有一个n行m列的网格,其中一些格子被涂上了黑色,其余的涂上了白色。

在一步操作中你可以选取一个黑色方格并以下规则对白色格子进行涂色:

  1. 将该黑色方格所在行中全部白色方格涂成黑色
  2. 将该黑色方格所在列中全部白色方格涂成黑色

问题开始前会给你两个整数r和c,你需要找出让第r行和第c列中的所有方格变成黑色的最小操作数,或者做出判断,这是不可能实现的。

输入:

测试数据包含多组,第一行为一个整数t,用于表示测试的组数,每组测试输入情况如下:

第一行包含四个整数n,m,r,c,其中n,m表示网格的长和宽,r和c表示需要被全部着成黑色的行列标志数。

接下来n行,每行包含m个字符,每两个字符之间用一个空格隔开,用字符“B”表示该方格的颜色为黑色,字符“W”则为白色。

输出:

每组测试对应一行输出结果,如果无法完成上述要求则输出-1,否则输出所需的最小操作数。

样例输入:

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

复制

样例输出:

1
0
-1
2
2
0
-1
1
1

参考代码:

#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int t;
	cin >> t;
	for(int i = 0;i < t;++i){
		int m,n,r,c;
		cin >> n >> m >> r >> c;
		char old[n+1][m+1];	
		for(int j = 1;j <= n;++j){
			for(int k = 1;k <= m;++k){
				cin >> old[j][k];
			}
		}
		int C = 0,D = 0,E = 0;
		for(int j = 1;j <= n;++j){
			for(int k = 1;k <= m;++k){
				if(old[j][k] == 'B'){
					C = 1;
				}
				if(j == r && k != c && old[j][k] == 'B'){
					D = 1;
				}
				else if(j != r && k == c && old[j][k] == 'B'){
					D = 1;
				}
				else if(old[r][c] == 'B'){
					E = 1;
				}
			}
		}
		if(C){
			if(E){
				cout << 0 << endl;
			}
			else{
				if(D){
					cout << 1 << endl;
				}
				else{
					cout << 2 << endl;
				}
			}
		}
		else{
			cout << -1 << endl;
		}
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌凌丢了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值