首页 > USACO题解 > USACO Transformations

USACO Transformations

USACO Transformations模拟题目,按照题目给定的要求变换图形即可,变换的优先级依次减小。这个题目我写的很乱,因为中间二维数组传参数时竟然不能改变数组内容,实在很郁闷,后来测试时又可以了,不知道哪里出了问题。于是中间我还传指向数组名的指针,本地编译一大堆警告,拿到USACO就CE了。各种悲剧,最后,代码都不成样子了:(思路就是这样吧,具体可以看题目提供的Analysis里面的代码,那个简洁多了)

/*
	ID:stackex1
	LANG:C++
	PROG:transform
*/
#include <stdio.h>
#include <string.h>

#define MAX_LEN 12

char tmp[MAX_LEN][MAX_LEN], tmp2[MAX_LEN][MAX_LEN];

void rotate90clock()
{
	int i, j, n;
	char t[MAX_LEN][MAX_LEN];
	n = strlen(tmp[0]);
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < n; ++j)
		{
			t[j][n-1-i] = tmp[i][j];
		}
		t[i][j] = '\0';
	}
	memcpy(tmp, t, sizeof(t));
}

void rotate90clock2()
{
	int i, j, n;
	char t[MAX_LEN][MAX_LEN];
	n = strlen(tmp2[0]);
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < n; ++j)
		{
			t[j][n-1-i] = tmp2[i][j];
		}
		t[i][j] = '\0';
	}
	memcpy(tmp2, t, sizeof(t));
}

int matrixcmp(char a[][MAX_LEN], char b[][MAX_LEN])
{
	int i, j, n;
	n = strlen(a[0]);
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < n; ++j)
		{
			if(a[i][j] != b[i][j])
				return 1;
		}
	}
	return 0;
}

int check_reflect_ok(char src[][MAX_LEN], char dst[][MAX_LEN])
{
	int i, j, n, k, bn;
	n = strlen(src[0]);
	//for(k = 0; k < n; ++k)
	//{
		bn = 1;
		for(i = 0; i < n; ++i)
		{
			for(j = 0; j < n; ++j)
			{
				if(src[i][n-1-j] != dst[i][j])
				{
					bn = 0;
					break;
				}
			}
			if(bn == 0)break;
		}
		if(bn == 1) return 1;
	//}
	return 0;
}

int check_comb_ok(char src[][MAX_LEN], char dst[][MAX_LEN])
{
	int i, j, n, k, bn;
	n = strlen(src[0]);
	//for(k = 0; k < n; ++k)
	//{
		for(i = 0; i < n; ++i)
		{
			for(j = 0; j < n; ++j)
			{
				tmp2[i][j] = src[i][n-1-j];
				//putchar(tmp2[i][j]);
			}
			tmp2[i][j] = '\0';
			//putchar('\n');
		}
		//putchar('\n');
		
		rotate90clock2();
		if(matrixcmp(tmp2, dst) == 0) return 1;
		rotate90clock2();
		if(matrixcmp(tmp2, dst) == 0) return 2;
		rotate90clock2();
		if(matrixcmp(tmp2, dst) == 0) return 3;
	//}
	
	return 0;
}

int work(char src[][MAX_LEN], char dst[][MAX_LEN])
{
	int i, j, n;
	char t[MAX_LEN][MAX_LEN];
	memcpy(tmp, src, sizeof(tmp));
	n = strlen(src[0]);
	/*Start transform*/
	rotate90clock();
	if(matrixcmp(tmp, dst) == 0) return 1;
	rotate90clock();
	if(matrixcmp(tmp, dst) == 0) return 2;
	rotate90clock();
	if(matrixcmp(tmp, dst) == 0) return 3;
	memcpy(tmp, src, sizeof(tmp));
	//
	if(check_reflect_ok(tmp, dst)) return 4;
	if(check_comb_ok(tmp, dst)) return 5;
	//
	if(matrixcmp(src, dst) == 0) return 6;
	return 7;
}

int main(int argc, char **argv)
{
	FILE *fin = fopen("transform.in", "r");
	FILE *fout = fopen("transform.out", "w");
	
	int i, j, n;
	char m1[MAX_LEN][MAX_LEN], m2[MAX_LEN][MAX_LEN];
	
	fscanf(fin, "%d%*c", &n);
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < n; ++j)
			m1[i][j] = fgetc(fin);
		fgetc(fin);
		m1[i][j] = '\0';
	}
	for(i = 0; i < n; ++i)
	{
		for(j = 0; j < n; ++j)
			m2[i][j] = fgetc(fin);
		fgetc(fin);
		m2[i][j] = '\0';
	}
	
	fprintf(fout, "%d\n", work(m1, m2));
	
	return 0;
}


觉得文章还不错?点击此处对作者进行打赏!


本文地址: 程序人生 >> USACO Transformations
作者:代码疯子(Wins0n) 本站内容如无声明均属原创,转载请保留作者信息与原文链接,谢谢!


更多



  1. 本文目前尚无任何评论.