Official

B - Coloring Matrix Editorial by en_translator


Performing the operation four times on the matrix restores the original state.
Thus, at most four kinds of matrices can result from rotating \(A\). Specifically, we can only consider the matrices resulting from performing the operation on \(A\) between zero and three times.
Thus, it is sufficient to, for every possible matrix that can result from rotating \(A\), check if \(B_{i,j} = 1\) if \(A_{i,j} = 1\).
If the index starts from zero, the indices in the Problem Statement and in the program are not the same, so be careful.

Sample code

#include <iostream>
#include <vector>
using namespace std;

vector<vector<int>> rotate(vector<vector<int>> a) {
	int n = a.size();
	vector<vector<int>> res(n, vector<int>(n));
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) res[j][n - 1 - i] = a[i][j];
	return res;
}

int main() {
	int n;
	cin >> n;
	vector<vector<int>> a(n, vector<int>(n));
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j];
	vector<vector<int>> b(n, vector<int>(n));
	for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> b[i][j];
	for (int _ = 0; _ < 4; _++) {
		bool ok = true;
		for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (a[i][j] == 1 && b[i][j] == 0) ok = false;
		if (ok) {
			cout << "Yes\n";
			return 0;
		}
		a = rotate(a);
	}
	cout << "No\n";
}

posted:
last update: