ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【atcoder】beginner218-shapes<图形旋转平移>

2021-09-12 21:03:10  阅读:213  来源: 互联网

标签:atcoder Sample SS TT shapes ..... beginner218 Input Copy


一、题目大意

题目链接:https://atcoder.jp/contests/abc218/tasks/abc218_c

Problem Statement

We have two figures SS and TT on a two-dimensional grid with square cells.

SS lies within a grid with NN rows and NN columns, and consists of the cells where S_{i,j}Si,j​ is #.
TT lies within the same grid with NN rows and NN columns, and consists of the cells where T_{i,j}Ti,j​ is #.

Determine whether it is possible to exactly match SS and TT by 9090-degree rotations and translations.

Constraints

  • 1 \leq N \leq 2001≤N≤200
  • Each of SS and TT consists of # and ..
  • Each of SS and TT contains at least one #.

Input

Input is given from Standard Input in the following format:

NN
S_{1,1}S_{1,2}\ldots S_{1,N}S1,1​S1,2​…S1,N​
\vdots⋮
S_{N,1}S_{N,2}\ldots S_{N,N}SN,1​SN,2​…SN,N​
T_{1,1}T_{1,2}\ldots T_{1,N}T1,1​T1,2​…T1,N​
\vdots⋮
T_{N,1}T_{N,2}\ldots T_{N,N}TN,1​TN,2​…TN,N​

Output

Print Yes if it is possible to exactly match SS and TT by 9090-degree rotations and translations, and No otherwise.


Sample Input 1 Copy

Copy
5
.....
..#..
.###.
.....
.....
.....
.....
....#
...##
....#

Sample Output 1 Copy

Copy
Yes

We can match SS to TT by rotating it 9090-degrees counter-clockwise and translating it.


Sample Input 2 Copy

Copy
5
#####
##..#
#..##
#####
.....
#####
#..##
##..#
#####
.....

Sample Output 2 Copy

Copy
No

It is impossible to match them by 9090-degree rotations and translations.


Sample Input 3 Copy

Copy
4
#...
..#.
..#.
....
#...
#...
..#.
....

Sample Output 3 Copy

Copy
Yes

Each of SS and TT may not be connected.


Sample Input 4 Copy

Copy
4
#...
.##.
..#.
....
##..
#...
..#.
....

Sample Output 4 Copy

Copy
No

Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.

大意就是给两个矩阵,看里面的'#'能否通过旋转、平移重合

二、ac代码

#include<bits/stdc++.h>
using namespace std;

struct node{
int x,y;
};
map<int,node>mp;
bool comp(const node &a,const node &b)
{
    if (a.x==b.x) return a.y<b.y;
    else return a.x<b.x;
}
int main(void)
{
    string s;
    vector<node>vec;
    ios::sync_with_stdio(0);
    int n,cnt=0,flag=1;
    cin>>n;
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        for (int j=0;j<n;j++)
        {
            if (s[j]=='#')
            {
                node t;
                t.x=i,t.y=j+1;
                mp[cnt++]=t;  //序号与坐标
            }
        }
    }
    for (int i=1;i<=n;i++)
    {
        cin>>s;
        for (int j=0;j<n;j++)
        {
            if (s[j]=='#')
            {
                node t;
                t.x=i,t.y=j+1;
                vec.push_back(t);
            }
        }
    }
    if (cnt!=vec.size())  //个数不相等,肯定不可能重合
    {
        cout<<"No";
        return 0;
    }
    for (int i=1;i<=4;i++)
    {
        flag=1;
        for (int j=0;j<vec.size();j++)  //更新旋转后的坐标
        {
            swap(vec[j].x,vec[j].y);
            vec[j].y=n-vec[j].y+1;
        }
        sort(vec.begin(),vec.end(),comp);  //按照从左到右,从上到下的顺序排序
        int row=vec[0].x-mp[0].x; //判断旋转后的第一个坐标和要比较的第一个坐标的行的关系
        int column=vec[0].y-mp[0].y; //判断旋转后的第一个坐标和要比较的第一个坐标的列的关系
        for (int j=1;j<cnt;j++)
        {
            if (mp[j].x+row==vec[j].x&&mp[j].y+column==vec[j].y) continue;
            else
            {
                flag=0;
                break;
            }
        }
        if (flag) break;
    }
    if (flag) cout<<"Yes";
    else cout<<"No";
    return 0;
}

思路就是每次将第二个矩阵旋转90°,依次判断每个点的距离是不是完全一样

 

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


以这个样例为例,如果能旋转平移重合的话,每个点相对应的关系是一样的,类似相似三角形
..#..
.###.
.....
.....
.....
.....
.....
.....
.#...
###..
因此,在开始的map中已经保存好了上面矩阵的信息,下面的矩阵每次旋转完sort排序,得到的就是从左到右从上到下依次的点

 

 

 

peace

标签:atcoder,Sample,SS,TT,shapes,.....,beginner218,Input,Copy
来源: https://www.cnblogs.com/bcyyds/p/15260047.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有