Codeforces Round #713 (Div. 3)-B. Almost Rectangle-题解

Codeforces Round #713 (Div. 3)-B. Almost Rectangle

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

There is a square field of size n × n n \times n n×n in which two cells are marked. These cells can be in the same row or column.

You are to mark two more cells so that they are the corners of a rectangle with sides parallel to the coordinate axes.

For example, if n = 4 n=4 n=4 and a rectangular field looks like this (there are asterisks in the marked cells):

. . ∗ . . . . . ∗ . . . . . . . \begin{matrix} . & . & * & . \\ . & . & . & . \\ * & . & . & . \\ . & . & . & . \\ \end{matrix} ..............

Then you can mark two more cells as follows

∗ . ∗ . . . . . ∗ . ∗ . . . . . \begin{matrix} * & . & * & . \\ . & . & . & . \\ * & . & * & . \\ . & . & . & . \\ \end{matrix} ............

If there are several possible solutions, then print any of them.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 400 1 \le t \le 400 1t400). Then t t t test cases follow.

The first row of each test case contains a single integer n n n ( 2 ≤ n ≤ 400 2 \le n \le 400 2n400) — the number of rows and columns in the table.

The following n n n lines each contain n n n characters ‘.’ or ‘*****’ denoting empty and marked cells, respectively.

It is guaranteed that the sums of n n n for all test cases do not exceed 400 400 400.

It is guaranteed that there are exactly two asterisks on the field. They can be in the same row/column.

It is guaranteed that the solution exists.

Output

For each test case, output n n n rows of n n n characters — a field with four asterisks marked corresponding to the statements. If there multiple correct answers, print any of them.

Sample Input

6
4
..*.
....
*...
....
2
*.
.*
2
.*
.*
3
*.*
...
...
5
.....
..*..
.....
.*...
.....
4
....
....
*...
*...

Sample Onput

*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..

题目大意

给你一个 n ∗ n n*n nn的矩阵,里面又两个*,其他都是.
现在让你把两个.变成*,使得四个*能够组成一个矩形。


解题思路

首先题目保证了一定有解,那就考虑几种情况:

  • 两个*不在同一行且不在同一列:

    那么两个*就一定在对角,直接把一个*的横坐标和另一个*的纵坐标相组合就得到了两个新的坐标,即为所求。

  • 两个*在同一行或同一列

    那么两个*就是矩阵的一条边。可以看这两个*是不是在第一行(或第一列):

    • 两个*在第一行(或第一列)

      那么就把另一条边放到第二行(或第二列)

    • 两个*不在第一行(或第一列)

      那么就把另一条边放到第一行(或第一列)


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;

int main()
{
    int N;
    cin>>N;
    while(N--)//N组测试样例
    {
        int n;
        cd(n);
        int xy[2][2];//记录已有的两个*的横纵坐标xy
        int sum=0;//出现的*的个数
        for(int i=0;i<n;i++)
        {
            string line;
            cin>>line;//输入一行
            for(int j=0;j<n;j++)
            {
                if(line[j]=='*')//如果这个是*就记录
                {
                    xy[sum][0]=i;
                    xy[sum][1]=j;
                    sum++;
                }
            }
        }
        int ans[2][2];//要变成*的两个.的横纵坐标
        if(xy[0][0]==xy[1][0])//已有两个*在同一行
        {
            if(xy[0][0]==0)//如果在第一行
            {
                ans[0][0]=1;//另外两个要变成的*就放在第二行
                ans[0][1]=xy[0][1];//纵坐标与已有的*的纵坐标相同
                ans[1][0]=1;
                ans[1][1]=xy[1][1];
            }
            else//不在第一行
            {
                ans[0][0]=0;//另外两个要变成的*就放在第一行
                ans[0][1]=xy[0][1];
                ans[1][0]=0;
                ans[1][1]=xy[1][1];
            }
            
        }
        else if(xy[0][1]==xy[1][1])//已有的两个*在同一列
        {
            if(xy[0][1]==0)//在第一列
            {
                ans[0][0]=xy[0][0];
                ans[0][1]=1;
                ans[1][0]=xy[1][0];
                ans[1][1]=1;
            }
            else//不在第一列
            {
                ans[0][0]=xy[0][0];
                ans[0][1]=0;
                ans[1][0]=xy[1][0];
                ans[1][1]=0;
            }
        }
        else//已有的两个*在对角
        {
            ans[0][0]=xy[0][0];
            ans[0][1]=xy[1][1];
            ans[1][0]=xy[1][0];
            ans[1][1]=xy[0][1];
        }
        // printf("%d %d\n%d %d\n%d %d\n%d %d\n",ans[0][0],ans[0][1],ans[1][0],ans[1][1],xy[0][0],xy[0][1],xy[1][0],xy[1][1]);//*******
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if((i==ans[0][0]||i==ans[1][0]||i==xy[0][0]||i==xy[1][0])&&(j==ans[0][1]||j==ans[1][1]||j==xy[0][1]||j==xy[1][1]))//如果横坐标是*的其中一个横坐标  且  纵坐标是*的其中一个纵坐标
                {
                    putchar('*');//输出*
                }
                else//否则
                {
                    putchar('.');//输出.
                }
                
            }
            puts("");
        }
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/115597106

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tisfy

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

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

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

打赏作者

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

抵扣说明:

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

余额充值