CF1799E.City Union

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given n×mn \times m grid. Some cells are filled and some are empty.

A city is a maximal (by inclusion) set of filled cells such that it is possible to get from any cell in the set to any other cell in the set by moving to adjacent (by side) cells, without moving into any cells not in the set. In other words, a city is a connected component of filled cells with edges between adjacent (by side) cells.

Initially, there are two cities on the grid. You want to change some empty cells into filled cells so that both of the following are satisfied:

  • There is one city on the resulting grid.
  • The shortest path between any two filled cells, achievable only by moving onto filled cells, is equal to the Manhattan distance between them.

The Manhattan distance between two cells (a,b)(a, b) and (c,d)(c, d) is equal to ac+bd|a - c| + |b - d| .

Find a way to add filled cells that satisfies these conditions and minimizes the total number of filled cells.

输入格式

Input consists of multiple test cases. The first line contains a single integer tt , the number of test cases ( 1t50001 \le t \le 5000 ).

The first line of each test case contains two integers nn and mm ( 1n,m501 \le n, m \le 50 , nm3nm \geq 3 ).

The next nn lines describe the grid. The ii -th line contains a string sis_i of length mm . si,js_{i,j} is '#' if the cell in position (i,j)(i, j) is filled, and '.' if it is empty.

It is guaranteed that there are exactly two cities in the initial grid.

It is guaranteed that the sum of nmn\cdot m over all test cases does not exceed 2500025\,000 .

输出格式

For each test case, output nn lines, each containing a string of length mm , describing the grid you create in the same format as the input.

If there are multiple possible answers with the minimum number of filled cells print any.

输入输出样例

  • 输入#1

    11
    1 3
    #.#
    2 2
    .#
    #.
    4 4
    ..##
    ...#
    #...
    ##..
    6 6
    .##...
    ##....
    ......
    ....##
    .....#
    ...###
    6 5
    .#..#
    .#..#
    .#..#
    .#.##
    .#...
    ##...
    5 5
    #####
    #...#
    #.#.#
    #...#
    #####
    4 4
    .##.
    ##.#
    #.##
    .##.
    5 5
    ..###
    ....#
    .....
    #....
    #....
    5 6
    .##...
    ##....
    #....#
    ....##
    ...##.
    6 5
    ..##.
    ...##
    ....#
    #....
    ##...
    .##..
    5 4
    ..##
    ..#.
    ..#.
    #...
    #...

    输出#1

    ###
    
    .#
    ##
    
    ..##
    ..##
    ###.
    ##..
    
    .##...
    ###...
    ..#...
    ..####
    ...###
    ...###
    
    .####
    .####
    .####
    .####
    .#...
    ##...
    
    #####
    #####
    #####
    #####
    #####
    
    .##.
    ####
    ####
    .##.
    
    ..###
    ..###
    ..#..
    ###..
    #....
    
    .##...
    ###...
    ######
    ...###
    ...##.
    
    ..##.
    ..###
    ..###
    ###..
    ###..
    .##..
    
    ..##
    ..#.
    ..#.
    ###.
    #...

说明/提示

In the first test case, we can add a single filled cell between the two cities to connect them. We can verify that the second condition is satisfied.

In the second test case, we can also connect the cities with a single filled cell, while satisfying the second condition.

In the third test case, note that if we filled the 3 cells in the top left, the cities would be connected, but the second condition would not be satisfied for cells (4,2)(4, 2) and (2,4)(2, 4) .

首页