zoukankan      html  css  js  c++  java
  • USACO1.2.2Transformations

    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
    题解:a数组是转换前的状态,b数组是转换后的状态。
    只要枚举这七种情况即可
    顺时针转90度:b[i][j]=a[n-j-1][i];
    顺时针转180度:b[i][j]=a[n-i-1][n-j-1];
    顺时针转270度:b[i][j]=a[j][n-i-1];
    水平翻转:b[i][j]=a[i][n-j-1];
    第五种情况的话,先把b水平翻转一下,然后再判断是否符合1-3中的一种。
    不改变:b[i][j]=a[i][j];
    如果以上六种情况都不符合,那就直接输出7就OK了。。。
    View Code
      1 /*
      2 ID:spcjv51
      3 PROG:transform
      4 LANG:C
      5 */
      6 #include<stdio.h>
      7 #include<string.h>
      8 #define maxn 11
      9 char a[maxn][maxn],b[maxn][maxn],c[maxn][maxn];
     10 int n;
     11 int trans1()
     12 {
     13     int i,j;
     14     for(i=0; i<n; i++)
     15         for(j=0; j<n; j++)
     16             if(b[i][j]!=a[n-j-1][i]) return 0;
     17     return 1;
     18 }
     19 int trans2()
     20 {
     21     int i,j;
     22     for(i=0; i<n; i++)
     23         for(j=0; j<n; j++)
     24             if(b[i][j]!=a[n-i-1][n-j-1]) return 0;
     25     return 1;
     26 
     27 }
     28 int trans3()
     29 {
     30     int i,j;
     31     for(i=0; i<n; i++)
     32         for(j=0; j<n; j++)
     33             if(b[i][j]!=a[j][n-i-1]) return 0;
     34     return 1;
     35 }
     36 int trans4()
     37 {
     38     int i,j;
     39     for(i=0; i<n; i++)
     40         for(j=0; j<n; j++)
     41             if(b[i][j]!=a[i][n-j-1]) return 0;
     42     return 1;
     43 }
     44 int trans6()
     45 {
     46     int i,j;
     47     for(i=0; i<n; i++)
     48         for(j=0; j<n; j++)
     49             if(b[i][j]!=a[i][j]) return 0;
     50     return 1;
     51 }
     52 
     53 int main(void)
     54 {
     55     freopen("transform.in","r",stdin);
     56     freopen("transform.out","w",stdout);
     57     int i,j,m,ans,flag;
     58     scanf("%d",&n);
     59     for(i=0; i<n; i++)
     60         scanf("%s",a[i]);
     61     for(i=0; i<n; i++)
     62         scanf("%s",b[i]);
     63     flag=1;
     64     if(flag&&trans1())
     65     {
     66         printf("1\n");
     67         flag=0;
     68     }
     69     if(flag&&trans2())
     70     {
     71         printf("2\n");
     72         flag=0;
     73     }
     74     if(flag&&trans3())
     75     {
     76         printf("3\n");
     77         flag=0;
     78     }
     79     if(flag&&trans4())
     80     {
     81         printf("4\n");
     82         flag=0;
     83     }
     84     if(flag)
     85     {
     86         int i,j;
     87         for(i=0; i<n; i++)
     88             for(j=0; j<n; j++)
     89                 c[i][j]=b[i][n-j-1];
     90         for(i=0; i<n; i++)
     91             for(j=0;j<n;j++)
     92                 b[i][j]=c[i][j];
     93         if(trans1()||trans2()||trans3())
     94         {
     95             printf("5\n");
     96             flag=0;
     97         }
     98     }
     99 
    100     if(flag&&trans6())
    101     {
    102         printf("6\n");
    103         flag=0;
    104     }
    105     if(flag) printf("7\n");
    106     return 0;
    107 }
    
    
    




  • 相关阅读:
    ASM ClassReader failed to parse class file解决方法
    Android Studio3.1.2升级问题:Configuration 'compile' is obsolete and has been replaced with 'implementation'.
    解决Android Studio出现Failed to open zip file. Gradle's dependency cache may be corrupt的问题
    AS使用lombok注解报错:Annotation processors must be explicitly declared now. The following dependencies on the compile classpath are found to contain annotation processor.
    IDEA调试SpringMvc项目时,出错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener,解决办法
    解决Android Studio出现GC overhead limit exceeded
    android ScrollView 控制行数
    [android警告]AndroidManifest.xml警告 Not targeting the latest versions of Android
    Android中的windowSoftInputMode属性详解
    MySQL中的isnull、ifnull和nullif函数用法
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2858812.html
Copyright © 2011-2022 走看看