usaco基础第五题Transformations

  看到这个题还以为是个搜索题,搜索向来不会,以为得卡在这儿,后面仔细看了下题,几个变换发型没什么难的,就是这句In the case that more than one transform could have been used, choose the one with the minimum number above. 以为要用很多次变换才能变换到目的数组,看了下题目的翻译,才知道这句话的意思是如果有多种可用的转换方法,请选择序号最小的那个。如果是这样的话那就简单了。只要按照序号枚举就行了。记得第六个应该放在第六个位置。还有只要遇到一样的状态,就打印序号,然后退出。如果忘记退出会打印多个数字wa。

/*
ID: like_091
PROG: transform
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
const int N = 12;
char before[N][N], now[N][N];
int n;
//翻转九十度
void rotat(char x[N][N])
{
	char temp[N][N];
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			temp[j][n - i + 1] = x[i][j];
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			x[i][j] = temp[i][j];
}
//水平翻转
void reflect(char x[N][N])
{
	char temp[N][N];
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			temp[i][n - j + 1] = x[i][j];
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			x[i][j] = temp[i][j];
}
//判断是否是一样
bool equal(char p[N][N], char q[N][N])
{
	bool flag = true;
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			if (p[i][j] != q[i][j])
				flag = false;
	return flag;
}
//把一个数组复制到另一个
void fun(char x[N][N], char y[N][N])
{
	for (short i = 1; i <= n; i++)
		for (short j = 1; j <= n; j++)
			y[i][j] = x[i][j];
}
int main(void)
{
	ifstream cin("transform.in");
	ofstream cout("transform.out");
	while (cin>>n)
	{
		char a[N][N];
		for (short i = 1; i <= n; i++)
			for (short j = 1; j <= n; j++)
				cin>>before[i][j];
		for (short i = 1; i <= n; i++)
			for (short j = 1; j <= n; j++)
				cin>>now[i][j];
		fun(before, a);//把原来的数组复制到a中
		for (int k = 1; k <= 3; k++)
		{
			rotat(a);//翻转90度
			if (equal(a, now))
			{
				cout<<k<<endl;
				break;
			}
		}
		//已经相等就退出
		if (equal(a, now))continue;
		fun(before, a);//重新复制到a中
		reflect(a);//水平翻转
		if (equal(a, now))
		{
			cout<<"4"<<endl;
			continue;
		}
		for (int k = 1; k <= 3; k++)
		{
			rotat(a);//翻转90度
			if (equal(a, now))
			{
				cout<<"5"<<endl;
				break;
			}
		}
		//如果已经相等就退出
		if (equal(a, now))continue;
		//记得6应该放到此处,放到前面wa
		if (equal(before, now))
		{
			//如果相等无需变换
			cout<<"6"<<endl;
			continue;
		}
		cout<<"7"<<endl;
	}
	return 0;
}
posted @ 2011-02-19 12:30  like@neu  阅读(172)  评论(0编辑  收藏  举报