Official

B - Grid Rotation Editorial by en_translator


If no rotation is performed, the number of squares that differ in \(S\) and \(T\) is the answer.

Repainting a cell and rotating the entire grid can be swapped, so we may assume that rotating the grid is always performed firstly. By trying the number of rotations from \(0\), \(1\), \(2\), and \(3\), the problem can be solved.

Sample code (Python)

N = int(input())
S = [input() for _ in range(N)]
T = [input() for _ in range(N)]

def right_rot90(S):
  return list(zip(*S[::-1]))

def count_diff(S,T):
  return sum([1 for si,ti in zip(S,T) for sij,tij in zip(si,ti) if sij!=tij])

ans = 10**9
for rot_count in range(4):
  ans = min(ans, count_diff(S,T)+rot_count)
  S = right_rot90(S)

print(ans)

Sample code (C++)

#include<bits/stdc++.h>
using namespace std;

int count_diff(vector<string>S, vector<string>T){
	int N = S.size();
	int ans = 0;
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)if(S[i][j]!=T[i][j])ans++;
	return ans;
}

vector<string> right_rot90(vector<string>S){
	int N = S.size();
	vector<string>ret(N, string(N,'.'));
	for(int i=0;i<N;i++)for(int j=0;j<N;j++)ret[i][j]=S[N-1-j][i];
	return ret;
}

int main(){
	int n;
	cin >> n;
	vector<string> s(n), t(n);
	for(auto &x:s)cin >> x;
	for(auto &x:t)cin >> x;
	
	int ans = 1e9;
	for(int rot_count=0;rot_count<4;rot_count++){
		ans = min(ans, count_diff(s,t)+rot_count);
		s = right_rot90(s);
	}
	cout << ans << endl;
}

posted:
last update: