Amity Assessment(思维题)

Description

Bessie the cow and her best friend Elsie each received a sliding puzzle on Pi Day. Their puzzles consist of a 2×2 grid and three tiles labeled 'A', 'B', and 'C'. The three tiles sit on top of the grid, leaving one grid cell empty. To make a move, Bessie or Elsie can slide a tile adjacent to the empty cell into the empty cell as shown below:

In order to determine if they are truly Best Friends For Life (BFFLs), Bessie and Elsie would like to know if there exists a sequence of moves that takes their puzzles to the same configuration (moves can be performed in both puzzles). Two puzzles are considered to be in the same configuration if each tile is on top of the same grid cell in both puzzles. Since the tiles are labeled with letters, rotations and reflections are not allowed.

 

Input

 The first two lines of the input consist of a 2×2 grid describing the initial configuration of Bessie's puzzle. The next two lines contain a 2×2 grid describing the initial configuration of Elsie's puzzle. The positions of the tiles are labeled 'A', 'B', and 'C', while the empty cell is labeled 'X'. It's guaranteed that both puzzles contain exactly one tile with each letter and exactly one empty position.

Output

 Output "YES"(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print "NO" (without quotes).

Sample Input

AB

XC

XB

AC

Sample Output

YES

HINT

The solution to the sample is described by the image. All Bessie needs to do is slide her 'A' tile down.

题意:

给出第一个2*2的初始格子,里面有三种标记字母A,B,C,"X"代表格子为空,判断第二个2*2的格子能否由第一个格子中的字母上下左右移动得到。(任何字母都可以移动至"X"空格)

思路:

对于任意一个给定的初始格子,取顺时针字母:

s1=s1+reverse(s2)(2*2格子的第一行字母+第二行字母的倒置)

对于任意一个判断的格子,也取顺时针字母,并且将得到的两串相同字母相加(用于判断字符串是否与s1的顺时针排列相同):

s3=s3+reverse(s4)+s3+reverse(s4)

最后判断s1的顺时针排列是否存在于s3即可:

str2.find(str1)!=string::npos(存在)

Code:

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#define mod 998244353;
#define Max 0x3f3f3f3f;
#define Min 0xc0c0c0c0;
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
using namespace std;
typedef long long ll;
const int maxn=100005;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
char str[2][2];
int sx,sy;
int main(){
    ios::sync_with_stdio(false);
    string s1,s2,s4,s3,str1,str2;
    while(cin>>s1){
        cin>>s2;
        reverse(s2.begin(),s2.end());
        s1+=s2;
        for(int i=0;i<s1.length();i++){
            if(s1[i]!='X'){
                str1+=s1[i];        //str只存有效的A,B,C字母
            }
        }
        cin>>s3>>s4;
        reverse(s4.begin(),s4.end());
        s3=s3+s4+s3+s4;
        for(int i=0;i<s3.length();i++){
            if(s3[i]!='X'){         //str只存有效的A,B,C字母
                str2+=s3[i];
            }
        }
        if(str2.find(str1)!=string::npos){  //string::npos代表size_t的最大值
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
    }
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值