本文已参与「新人创作礼」活动,一起开启掘金创作之路。
@TOC
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 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 and a rectangular field looks like this (there are asterisks in the marked cells):
Then you can mark two more cells as follows
If there are several possible solutions, then print any of them.
Input
The first line contains a single integer (). Then test cases follow.
The first row of each test case contains a single integer () — the number of rows and columns in the table.
The following lines each contain characters '.' or '*****' denoting empty and marked cells, respectively.
It is guaranteed that the sums of for all test cases do not exceed .
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 rows of 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
*.*.
....
*.*.
....
**
**
**
**
*.*
*.*
...
.....
.**..
.....
.**..
.....
....
....
**..
**..
题目大意
给你一个的矩阵,里面又两个
*,其他都是.。 现在让你把两个.变成*,使得四个*能够组成一个矩形。
解题思路
首先题目保证了一定有解,那就考虑几种情况:
- 两个
*不在同一行且不在同一列:那么两个
*就一定在对角,直接把一个*的横坐标和另一个*的纵坐标相组合就得到了两个新的坐标,即为所求。- 两个
*在同一行或同一列那么两个
*就是矩阵的一条边。可以看这两个*是不是在第一行(或第一列):
- 两个
*在第一行(或第一列)那么就把另一条边放到第二行(或第二列)
- 两个
*不在第一行(或第一列)那么就把另一条边放到第一行(或第一列)
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;
}
同步发文于我的CSDN,原创不易,转载请附上原文链接哦~
Tisfy:letmefly.blog.csdn.net/article/det…