Transformations

题目描述:

A square pattern of size N x N (1 <= N <= 10) black and white square tiles is transformed into another square pattern. Write a program that will recognize the minimum transformation that has been applied to the original pattern given the following list of possible transformations:

  • #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees.
  • #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees.
  • #3: 270 Degree Rotation: The pattern was rotated clockwise 270 degrees.
  • #4: Reflection: The pattern was reflected horizontally (turned into a mirror image of itself by reflecting around a vertical line in the middle of the image).
  • #5: Combination: The pattern was reflected horizontally and then subjected to one of the rotations (#1-#3).
  • #6: No Change: The original pattern was not changed.
  • #7: Invalid Transformation: The new pattern was not obtained by any of the above methods.

In the case that more than one transform could have been used, choose the one with the minimum number above.

输入输出格式:

SAMPLE INPUT (file transform.in)

3
@-@
---
@@-
@-@
@--
--@

OUTPUT FORMAT

A single line containing the number from 1 through 7 (described above) that categorizes the transformation required to change from the `before' representation to the `after' representation.

SAMPLE OUTPUT (file transform.out)

1

题目大意:

题意:给定一个字符串方块和目标字符串方块。 
90度顺时针旋转 
2: 180度顺时针旋转 
3: 270度顺时针旋转 
4: 以中心轴为轴翻转 
5: 以中心轴为轴翻转后进行1-3种中任意一种操作 
6: 不变 
7: 不能转换过去 
问通过这些操作中的任意一个操作是否能够由给定字符串方块转换到目标字符串方块。如果多种操作都可以,那么输出编号最小的操作的编号。
解题思路:

其实是一个水题,就是列举出所有可能性,然后一个一个试出来就行,这里介绍两个函数void *memset(void *s, int ch, size_t n);函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。memset:作用是在一段内存块中填充某个给定的值,它是进行清零操作的一种最快方法,void* memcpy(void* destination, const void* source, size_t num);
void* dest 目标内存    const void* src  源内存    size_t num  字节个数,代码如下

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <map>
#include <cstdio>
#include <algorithm>
#define N 20
using namespace std;
int n;
char s[N][N], r[N][N];
void Rotation(){
	char temp[N][N];
	memset(temp,'\0',sizeof(temp));
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++){
			temp[i][j]=s[n-1-j][i];
		}
	memcpy(s,temp,sizeof(temp));
}
void reflected(){
	for(int i=0;i<n;i++){
		for(int j=0;j<n/2;j++){
			swap(s[i][j],s[i][n-1-j]);
		}
	}
}
bool check(){
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(s[i][j]!=r[i][j])
				return false;
		}
	}
	return true;
}
bool option(int op){
	if(op==1)
		Rotation();
	else if(op==2)
		Rotation();
	else if(op==3)
		Rotation();
	else if(op==4){
		Rotation();//要转回去
		reflected();
	}
	else if(op==5){
		for(int i=0;i<3;i++){
			Rotation();
			if(check()){
				return true;
			}
		}
		return false;
	}
	else if(op==6){
		Rotation();
		reflected();
	}
	return check();
}
int main(){
	int choose;
	freopen("D:\\test.in","r",stdin);
	freopen("D:\\transform1.out","w",stdout);
	scanf("%d",&n);
	memset(s,'\0',sizeof(s));
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cin>>s[i][j];
	for(int i=0;i<n;i++)
		for(int j=0;j<n;j++)
			cin>>r[i][j];
	for(choose=1;choose<7;choose++){
		if(option(choose)){
			printf("%d\n",choose);
			break;
		}
	}
	if(choose==7){
		printf("%d\n",7);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值