CF1512B Almost Rectangle

给定一个n*n的矩阵,其中有两个已标记的星号。任务是再标记两个点,使它们与已有的两个星号构成一个矩形的四个角。题目保证解决方案存在,且星号不会在同一行或同一列。程序通过遍历和条件判断找到解决方案并输出结果矩阵。
摘要由CSDN通过智能技术生成

题目描述

There is a square field of size n \times nn×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=4n=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.

输入格式

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

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

The following nn lines each contain nn characters '.' or '*' denoting empty and marked cells, respectively.

It is guaranteed that the sums of nn for all test cases do not exceed 400400 .

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.

输出格式

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

题意翻译

一句话题意:给出 TT 个矩阵,每个矩阵由 NN 行 NN 列个 . 和 * 组成,保证矩阵中有且只有两个 *,要求你再在矩阵上将两个 . 改成 * 使得 44 个 * 可以是一个矩阵的 44 个角(提示:两个 * 可能在同一行)。

输入输出样例

输入 #1复制

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

输出 #1复制

*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..
#include<bits/stdc++.h>
using namespace std;
int main()
{
	
	int t;
	cin>>t;//多组输入
	while(t--)
	{
		
		int n;
		cin>>n;
		char c[n+1][n+1]; 
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=n;++j)
			{
				cin>>c[i][j];//一个n*n的矩阵 用来存*
			}
		}
		//把每行每列的*用坐标(x1,y1)(x2,y2)来表示 
		int x1=-1,x2;//x1=-1作为一个标记 
		int y1,y2;
		for(int i=1;i<=n;++i)
		{
			for(int j=1;j<=n;++j)
			{
				if(c[i][j]=='*')
				{
					if(x1==-1)//
					{
						x1=i;
						y1=j;
					}
					else
					{
						x2=i;
						y2=j;
						break;//节省空间 
					}
				}
			}
		}
			//开始分组讨论
			if(x1!=x2&&y1!=y2)
			{
				c[x1][y2] = c[x2][y1] = '*';
			}
			else if(x1==x2)//在同一行 
			{
				if(x1==1)
				{
					c[2][y1]=c[2][y2]='*'; //就把第二行两列置称* 
				}
				else
				{
					c[1][y1]=c[1][y2]='*';//否则的话 就把第一行两列置成* 
				}	
			}
			else if(y1==1)//都在第一列 
			{
				c[x1][2]=c[x2][2]='*';  //就把第二列两行置称* 
			}
			else
			{
				c[x1][1]=c[x2][1]='*';//否则的话 就把第一列两行置成* 
			}
			for(int i=1;i<=n;++i)
			{
				for(int j=1;j<=n;++j)
				{
					cout<<c[i][j];
				}
				cout<<endl;
			 } 
		
		
		
	} 
	
	
	return 0;
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值