S 1.2 transform C程序

 

题目连接:

http://ace.delos.com/usacoprob2?S=transform&a=vaOx5hM2Vn9

 

题目:

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.

PROGRAM NAME: transform

INPUT FORMAT

Line 1:A single integer, N
Line 2..N+1:N lines of N characters (each either `@' or `-'); this is the square before transformation
Line N+2..2*N+1:N lines of N characters (each either `@' or `-'); this is the square after transformation

SAMPLE INPUT (file transform.in)

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

OUTPUT FORMAT

A single line containing the 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

 

 

题目翻译:

描述

一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案。写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式:

1:转90度:图案按顺时针转90度。

2:转180度:图案按顺时针转180度。

3:转270度:图案按顺时针转270度。

4:反射:图案在水平方向翻转(以中央铅垂线为中心形成原图案的镜像)。

5:组合:图案在水平方向翻转,然后再按照1到3之间的一种再次转换。

6:不改变:原图案不改变。

7:无效转换:无法用以上方法得到新图案。

如果有多种可用的转换方法,请选择序号最小的那个。

只使用1--7中的一个步骤来完成这次转换。

[编辑] 格式

PROGRAM NAME: transform

INPUT FORMAT:

file (transform.in)

第一行: 单独的一个整数N。

第二行到第N+1行: N行每行N个字符(不是“@”就是“-”);这是转换前的正方形。

第N+2行到第2*N+1行: N行每行N个字符(不是“@”就是“-”);这是转换后的正方形。

OUTPUT FORMAT:

file (transform.out)

单独的一行包括1到7之间的一个数字(在上文已描述)表明需要将转换前的正方形变为转换后的正方形的转换方法。

[编辑] SAMPLE INPUT

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

[编辑] SAMPLE OUTPUT

1

 

 

/*
ID:smilecl1
LANG:C
TASK:transform
*/

/*
 * TIME: 2012/11/7
 * MODIFY: 2012/11/9
 * EMAIL:
SmileCloud201@gmail.com
 * NAME: LiYun
 */

/*
 * 思路:
 * 本题中方式1,可以调用rotate_90()函数直接实现;方式2,通过2次调用rotate_90()函数实现;
 * 方式三,在方式2的基础上,再次调用rotate_90()函数就可以实现了;方式四,翻转,调用专门的函数reflect()实现;
 * 方式五,包含三种情况。1、方式四+rotate_90()函数实现。
 * 2、可以在方式五的1的基础上,即 1+rotate_90()函数实现。
 * 3、可以在方式五的2的基础上,即 2+rotate_90()函数实现。
 * 方式六,直接用equal()函数实现即可。
 * 注意:以上各个方式的判断情况的顺序是有讲究的。因为一直在利用temp的临时变量的值,来计算下一个方式。这样做的
 * 目的是尽量减少rotate()函数的调用次数,来减少执行时间。程序看起来长,其实相当于swich····case语句,执行时间不长。
*/

#include <stdio.h>
#include <string.h>
#define N 11

int equal(char s1[N][N], char change[N][N], int n)
{
    int i;
    int j;
    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            if ( s1[i][j] != change[i][j] )
            return 0;
    return 1;
}

int rotate_90 (char s1[N][N], char s2[N][N], int n)
{
    int i;
    int j;
    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            s2[i][j] = s1[n-j-1][i];
    return 0;
}

int reflect (char s1[N][N], char s2[N][N], int n)
{
    int i;
    int j;
   for(i=0;i<n;i++)
     for(j=0;j<n;j++)
       s2[i][j]=s1[i][n-1-j];
    return 0;
}


int turn(char primary[N][N], char change[N][N], int n)
{
    /* 这里temp1,temp2是两个临时变量,因为后一次旋转90度,用到前一次的结果,所以这里需要两个临时变量 */
    char temp1[N][N];
    char temp2[N][N];

    rotate_90( primary, temp1, n );
    if ( equal(change, temp1, n) )
        return 1;
    else
    {
        /* 这里temp1是初始的变量,旋转180度,先旋转90,再旋转90度。注意实参的值,在此犯过错误!!下面雷同。 */
        rotate_90( temp1, temp2, n );
        if ( equal(change, temp2, n) )
            return 2;
        else
        {
            rotate_90( temp2, temp1, n );
            if ( equal(change, temp1, n) )
                return 3;
            else
            {
                reflect(primary, temp1, n);
                if ( equal(change, temp1, n) )
                    return 4;
                else
                {
                    rotate_90( temp1, temp2, n );
                    if ( equal(change, temp2, n) ) return 5;
                    else
                    rotate_90( temp2, temp1, n );
                    if ( equal(change, temp1, n)) return 5;
                    else
                    rotate_90( temp1, temp2, n );
                    if ( equal(change, temp2, n)) return 5;
                }
            }
        }
    }
    if ( equal(primary, change, n) )
        return 6;
    else return 7;
}

int main()
{
    int n = 0;
    int i = 0;
    int result=7;
    char primary[N][N];
    char change[N][N];

    FILE *fin;
    FILE *fout;

    fin = fopen ("transform.in", "r");
    fout = fopen ("transform.out", "w");
    fscanf (fin, "%d", &n);
    for (i = 0; i != n; ++i)
    {
        fscanf (fin, "%s", primary[i]);
    }
    for (i = 0; i != n; ++i)
    {
        fscanf (fin, "%s", change[i]);
    }
    result = turn(primary, change, n);
    fprintf(fout, "%d\n", result);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值