We have two figures S and T on a two-dimensional grid with square cells. S lies within a grid with N rows and N columns, and consists of the cells where S i,j is #. T lies within the same grid with N rows and N columns, and consists of the cells where T i,j is #. Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.c++代码
时间: 2023-07-02 14:15:47 浏览: 105
Figures and Exercises MATLAB code.zip_9FXS_matlab-and-quantum_qu
Sure, here's a C++ code that checks whether it's possible to match S and T by 90-degree rotations and translations:
```c++
#include <iostream>
#include <vector>
using namespace std;
// function to rotate a grid by 90 degrees
vector<string> rotate90(vector<string>& grid) {
int n = grid.size();
vector<string> rotated(n, string(n, ' '));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
rotated[j][n-i-1] = grid[i][j];
}
}
return rotated;
}
// function to check if two grids are equal
bool areEqual(vector<string>& grid1, vector<string>& grid2) {
int n = grid1.size();
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (grid1[i][j] != grid2[i][j]) {
return false;
}
}
}
return true;
}
// function to check if S can be matched to T by rotations and translations
bool match(vector<string>& S, vector<string>& T) {
int n = S.size();
// check if S can be matched to T by 0-degree rotation
if (areEqual(S, T)) {
return true;
}
// check if S can be matched to T by 90-degree rotation
vector<string> rotated = rotate90(S);
if (areEqual(rotated, T)) {
return true;
}
// check if S can be matched to T by 180-degree rotation
rotated = rotate90(rotated);
if (areEqual(rotated, T)) {
return true;
}
// check if S can be matched to T by 270-degree rotation
rotated = rotate90(rotated);
if (areEqual(rotated, T)) {
return true;
}
// check if S can be matched to T by translation
for (int i = 1-n; i < n; i++) {
for (int j = 1-n; j < n; j++) {
vector<string> translated(n, string(n, ' '));
for (int x = 0; x < n; x++) {
for (int y = 0; y < n; y++) {
int xi = x + i;
int yj = y + j;
if (xi >= 0 && xi < n && yj >= 0 && yj < n) {
translated[x][y] = S[xi][yj];
}
}
}
if (areEqual(translated, T)) {
return true;
}
}
}
// S cannot be matched to T by rotations and translations
return false;
}
int main() {
int n;
cin >> n;
vector<string> S(n), T(n);
for (int i = 0; i < n; i++) {
cin >> S[i];
}
for (int i = 0; i < n; i++) {
cin >> T[i];
}
if (match(S, T)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
```
The `match` function takes two grids `S` and `T` as input and checks whether `S` can be matched to `T` by 90-degree rotations and translations. First, it checks if `S` can be matched to `T` by 0-degree rotation. If not, it rotates `S` by 90 degrees, 180 degrees, and 270 degrees, and checks if any of these rotations can match `T`. If not, it translates `S` in all possible directions and checks if any of these translations can match `T`. If a match is found, the function returns `true`. Otherwise, it returns `false`.
In the `main` function, the program reads in the input grid size `n`, the grids `S` and `T`, and calls the `match` function to check if `S` can be matched to `T`. If a match is found, the program outputs "Yes". Otherwise, it outputs "No".
阅读全文