CF1689D.Lena and Matrix

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Lena is a beautiful girl who likes logical puzzles.

As a gift for her birthday, Lena got a matrix puzzle!

The matrix consists of nn rows and mm columns, and each cell is either black or white. The coordinates (i,j)(i,j) denote the cell which belongs to the ii-th row and jj-th column for every 1≤i≤n1\leq i \leq n and 1≤j≤m1\leq j \leq m. To solve the puzzle, Lena has to choose a cell that minimizes the Manhattan distance to the farthest black cell from the chosen cell.

More formally, let there be k≥1k \ge 1 black cells in the matrix with coordinates (xi,yi)(x_i,y_i) for every 1≤i≤k1\leq i \leq k. Lena should choose a cell (a,b)(a,b) that minimizes $$\max_{i=1}^{k}(|a-x_i|+|b-y_i|).$$

As Lena has no skill, she asked you for help. Will you tell her the optimal cell to choose?

莉娜是一位喜欢逻辑谜题的漂亮女孩。

作为生日礼物,莉娜收到了一个矩阵谜题!

该矩阵包含 nn 行和 mm 列,每个格子为黑色或白色。坐标 (i,j)(i,j) 表示第 ii 行、第 jj 列的格子(其中 1≤i≤n1\leq i \leq n,1≤j≤m1\leq j \leq m)。要解开这个谜题,莉娜需选择一个格子,使得它到最远的黑色格子的曼哈顿距离最小。

更准确地说,设矩阵中一共有 k≥1k \ge 1 个黑色格子,其坐标为 (xi,yi)(x_i,y_i)(1≤i≤k1\leq i \leq k)。莉娜需选择一个格子 (a,b)(a,b),使得下式取最小值:

max⁡i=1k(∣a−xi∣+∣b−yi∣).\max_{i=1}^{k}(|a-x_i|+|b-y_i|).

由于莉娜不擅长解题,她向你求助。你能告诉她应选择哪个最优格子吗?

输入格式

There are several test cases in the input data. The first line contains a single integer tt (1≤t≤10 0001\leq t\leq 10\,000) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains two integers nn and mm (2≤n,m≤10002\leq n,m \leq 1000) — the dimensions of the matrix.

The following nn lines contain mm characters each, each character is either 'W' or 'B'. The jj-th character in the ii-th of these lines is 'W' if the cell (i,j)(i,j) is white, and 'B' if the cell (i,j)(i,j) is black.

It is guaranteed that at least one black cell exists.

It is guaranteed that the sum of n⋅mn\cdot m does not exceed 10610^6.

输入数据包含多个测试用例。第一行包含一个整数 tt(1≤t≤10 0001\leq t\leq 10\,000),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n,m≤10002\leq n,m \leq 1000),表示矩阵的维度。

接下来的 nn 行,每行包含 mm 个字符,每个字符为 'W' 或 'B'。在这些行中,第 ii 行的第 jj 个字符为 'W' 表示单元格 (i,j)(i,j) 是白色的,为 'B' 表示单元格 (i,j)(i,j) 是黑色的。

保证至少存在一个黑色单元格。

保证所有测试用例中 n⋅mn\cdot m 的总和不超过 10610^6。

输出格式

For each test case, output the optimal cell (a,b)(a,b) to choose. If multiple answers exist, output any.

对于每个测试用例,输出应选择的最优单元格 (a,b)(a,b)。如果存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    5
    3 2
    BW
    WW
    WB
    3 3
    WWB
    WBW
    BWW
    2 3
    BBB
    BBB
    5 5
    BWBWB
    WBWBW
    BWBWB
    WBWBW
    BWBWB
    9 9
    WWWWWWWWW
    WWWWWWWWW
    BWWWWWWWW
    WWWWWWWWW
    WWWWBWWWW
    WWWWWWWWW
    WWWWWWWWW
    WWWWWWWWW
    WWWWWWWWB

    输出#1

    2 1
    2 2
    1 2
    3 3
    6 5

说明/提示

In the first test case the two black cells have coordinates (1,1)(1,1) and (3,2)(3,2). The four optimal cells are (1,2)(1,2), (2,1)(2,1), (2,2)(2,2) and (3,1)(3,1). It can be shown that no other cell minimizes the maximum Manhattan distance to every black cell.

In the second test case it is optimal to choose the black cell (2,2)(2,2) with maximum Manhattan distance being 22.

在第一个测试用例中,两个黑格的坐标分别为 (1,1)(1,1) 和 (3,2)(3,2)。四个最优格子为 (1,2)(1,2)、(2,1)(2,1)、(2,2)(2,2) 和 (3,1)(3,1)。可以证明,不存在其他格子能使到所有黑格的最大曼哈顿距离更小。

在第二个测试用例中,选择黑格 (2,2)(2,2) 是最优的,此时最大曼哈顿距离为 22。

输入解题思路,AI测评打分。不知道怎么写?

首页