USACO 1.2 Transformations

方块转换问题

关于旋转,直接用旋转公式即可。

new_x = x * cos - y * sin
new_y = y * cos + x * sin

然后考虑下原坐标新坐标关系


/*
ID:xueyifa4
PROG:transform
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>	
using namespace std;
const int max_n = 11;
int n;
char ch[max_n][max_n],	p[max_n][max_n];
char now[max_n][max_n];

void tran(int num)
{
	int sin, cos;
	switch (num)
	{
		case 90: sin = -1; cos = 0; break;
		case 180: sin = 0; cos = -1; break;
		case 270: sin = 1; cos = 0; break;
		default : cout<<"Wrong! fool programer!"<<endl;
	}
	double mid = n  / 2.0 - 0.5;
	for (int i = 0; i != n; ++ i)
		for (int j = 0; j != n; ++ j)
		{
			double x = i - mid, y = j - mid;
			int new_x, new_y;
			new_x = x * cos - y * sin + mid;
			new_y = y * cos + x * sin + mid;
			now[new_x][new_y] = ch[i][j];
		}
}

void reflect()
{
	for (int i = 0; i != n; ++ i)
		for (int j = 0; j != n; ++ j)
			now[i][j] = ch[i][n - 1 - j];
	for (int i = 0; i != n; ++ i)
		for (int j = 0; j != n; ++ j)
			ch[i][j] = now[i][j];

}

bool check()
{
	for (int i = 0; i != n; ++ i)
		for (int j = 0; j != n; ++ j)
			if (p[i][j] != now[i][j])	return false;
	return true;
}

int doit()
{
	for (int i = 1; i != 4; ++ i)
	{
		tran(i * 90);
		if (check())	return i;
	}

	reflect();
	if (check())	return 4;
	for (int i = 0; i != n; ++ i)
		for (int j = 0 ; j != n; ++ j)	ch[i][j] = now[i][j];
	for (int i = 1; i != 4; ++ i)
	{
		tran(i * 90);	
		if (check())	return 5;
	}
	reflect();
	if (check())	return 6;
	return 7;
}


int main()
{
	freopen("transform.in", "r", stdin);
	freopen("transform.out", "w", stdout);
	scanf("%d\n", &n);
	for (int i = 0; i != n; ++ i)	gets(ch[i]);
	for (int i = 0; i != n; ++ i)	gets(p[i]);
	printf("%d\n", doit());
	return 0;
}


你可能感兴趣的:(USACO 1.2 Transformations)