zcmu-2959(字符串找规律+注意内存范围)

问题 B: Amity Assessment

时间限制: 2 Sec  内存限制: 256 MB
提交: 51  解决: 17
[提交][状态][讨论版]

题目描述

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.

输入

 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 "YES"(without quotes) if the puzzles can reach the same configuration (and Bessie and Elsie are truly BFFLs). Otherwise, print "NO" (without quotes).

样例输入

AB

XC

XB

AC

扫描二维码关注公众号,回复: 3287791 查看本文章

样例输出

YES

提示

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

[提交][状态][讨论版]

题意:前两行每行输入2*2的已知矩阵a,后两行输入2*2的结果矩阵b,如果a能通过移动元素到空位置得到b,就输出“YES”,

否则输出“NO”。其中‘X'代表空位置。

思路:一开始想dfs模拟,后来发现种类太多,行不通。可以将矩阵拉直,变成abcx,如果延长就变成abcabc,结果矩阵可以视为bca,因为bca是abcabc的子串,所以输出“YES“。(要注意将数组开的大一点,否则过不去)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
using namespace std;
char a[10][10],b[10][10];
int main(void)
{
    string ss,s1;
    scanf("%s",a[0]);
    scanf("%s",a[1]);
    scanf("%s",b[0]);
    scanf("%s",b[1]);
    if(a[0][0]!='X') ss.push_back(a[0][0]);
    if(a[0][1]!='X') ss.push_back(a[0][1]);
    if(a[1][1]!='X') ss.push_back(a[1][1]);
    if(a[1][0]!='X') ss.push_back(a[1][0]);
    ss=ss+ss;
    if(b[0][0]!='X') s1.push_back(b[0][0]);
    if(b[0][1]!='X') s1.push_back(b[0][1]);
    if(b[1][1]!='X') s1.push_back(b[1][1]);
    if(b[1][0]!='X') s1.push_back(b[1][0]);
    int l=ss.size(),fg=0;
    for(int i=0;i<l-3;i++)
    {
        if(ss[i]==s1[0]&&ss[i+1]==s1[1]&&ss[i+2]==s1[2]) fg=1;
    }
    //cout<<ss<<" "<<s1<<endl;
    if(fg) printf("YES\n");
    else printf("NO\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41829060/article/details/82751515