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.
一开始觉得挺水的吧,但事实是想了很久,emmmm,其实想明白确实挺水的,2*2的方格,只能逆时针或顺时针转,但其实两种转法是一样的,所以只要模拟顺时针转或逆时针转,然后每转一次就比较一次,如果直至转到和初始状态(第二个方格)一样,就说明不能达成目标方格(第一个方格)。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(a) memset(a,0,sizeof(a))
char a[3][3];
char b[3][3];
char c[5];
char d[5];
int jud()
{
int p = 1;
for(int i = 0; i < 3; i++){
if(c[i]!=d[i]){
p = 0;
break;
}
}
return p;
}
int main()
{
for(int i = 0; i < 2; i++) scanf("%s", a[i]);
for(int i = 0; i < 2; i++) scanf("%s", b[i]);
int k1 = 0, k2 = 0;
for(int i = 0; i < 2; i++){
if(i==0){
for(int j = 0; j < 2; j++){
if(a[i][j]!='X') c[k1++] = a[i][j];
if(b[i][j]!='X') d[k2++] = b[i][j];
}
}else{
for(int j = 1; j >= 0; j--){
if(a[i][j]!='X') c[k1++] = a[i][j];
if(b[i][j]!='X') d[k2++] = b[i][j];
}
}
}
//for(int i = 0; i < 3; i++) cout << c[i] << " " << d[i] << endl;
int flag = 1;
int cnt = 0;
while(jud()==0){
char ss = d[0];
d[0] = d[1];
d[1] = d[2];
d[2] = ss;
cnt++;
if(cnt>3){
flag = 0;
break;
}
}
//for(int i = 0; i < 3; i++) cout << c[i] << " " << d[i] << endl;
if(flag){
printf("YES\n");
}else{
printf("NO\n");
}
return 0;
}