USACO 1.2.2 – Transformations


#include <fstream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

#define fo(ii, ii0, iin) for(ii=ii0; ii<iin; ii++)
#define fo2(ii, ii0, iin, jj, jj0, jjn) fo(ii, ii0, iin) fo(jj, jj0, jjn)
#define rep(ii, iin) fo(ii, 0, iin)
#define rep2(ii, iin, jj, jjn) rep(ii, iin) rep(jj, jjn)

int i, j, x, y;

struct matrix {
	int N;
	int **m;

	matrix(int n) {
		N = n;
		m = new int *[N];
		rep(i, N)
			m[i] = new int[N];

	}

	bool operator ==(const matrix &t) {
		rep2(x, N, y, N)
				if (m[x][y] != t.m[x][y])
					return false;
		return true;
	}

	matrix rot() {
		matrix m2(N);
		rep2(x, N, y, N)
				m2.m[x][y] = m[y][N - 1 - x];
		return m2;
	}

	matrix reflect() {
		matrix m2(N);
		rep2(x, N, y, N)
				m2.m[x][y] = m[x][N - 1 - y];
		return m2;
	}
};

int find(matrix m1, matrix m2) {
	if (m1.rot() == m2)
		return 1;
	if (m1.rot().rot() == m2)
		return 2;

	if (m1.rot().rot().rot() == m2)
		return 3;

	if (m1.reflect() == m2)
		return 4;

	if (m1.reflect().rot() == m2 || m1.reflect().rot().rot() == m2
			|| m1.reflect().rot().rot().rot() == m2)
		return 5;
	if (m1 == m2)
		return 6;
	return 7;
}
int main_transform() {
	ofstream fout("transform.out");
	ifstream fin("transform.in");

	int N;
	fin >> N;

	matrix m1(N), m2(N);
	for (int x = 0; x < N; ++x) {
		string in;
		fin >> in;
		for (int y = 0; y < N; ++y) {
			m1.m[x][y] = in[y];
		}
	}

	for (int x = 0; x < N; ++x) {
		string in;
		fin >> in;
		for (int y = 0; y < N; ++y) {
			m2.m[x][y] = in[y];
		}
	}

	fout << find(m1, m2) << endl;
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: