zoukankan      html  css  js  c++  java
  • USACO题目——Transformations

    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
    
    我的解答,代码比较多,不够简洁。
    /*
    ID:rongkan1
    LANG: C++
    PROG:transform
    */

    #include <stdio.h>
    #include <stdlib.h>
    void rotate270(char **sour,int n,char **destin);
    void rotate180(char **sour,int n,char **destin);
    void rotate90(char **sour,int n,char **destin);
    void reflection(char **sour,int n, char **destin);
    int isequal(char **p1,char **p2,int n);
    int main()
    {
    FILE *fin = fopen("transform.in","r"), *fout = fopen("transform.out","w");

    int n;
    fscanf(fin,"%d",&n);

    char **tmp = new char*[n];
    char** des=new char*[n]; //用来计算的
    char** p1=new char*[n]; //存储读入的转换前矩阵
    char** p2=new char*[n]; //存储读入的转换后矩阵,
    char *line = new char[n+1];

    for(int i=0;i<n;i++)
    {
    *(tmp+i)=new char[n];
    *(des+i)=new char[n];
    *(p1+i)=new char[n];
    *(p2+i)=new char[n];
    }

    for(int i = 0; i<n; i++)
    {
    fscanf(fin,"%s",line);
    for(int j=0;j<n;j++)
    {
    p1[i][j]=line[j];
    des[i][j]=line[j];
    }
    }
    for(int i = 0; i<n; i++)
    {
    fscanf(fin,"%s",line);
    for(int j=0;j<n;j++){
    p2[i][j]=line[j];
    }
    }
    //上面输入
    rotate90(p1,n,des);
    if(isequal(p2,des,n) == 1)
    {

    fprintf(fout,"%d\n",1);
    fclose(fin);fclose(fout);
    return 0;
    }

    rotate180(p1,n,des);
    if(isequal(p2,des,n) == 1)
    {

    fprintf(fout,"%d\n",2);fclose(fin);fclose(fout);
    return 0;
    }

    rotate270(p1,n,des);
    if(isequal(p2,des,n) == 1)
    {

    fprintf(fout,"%d\n",3);fclose(fin);fclose(fout);
    return 0;
    }

    reflection(p1,n,des);
    if(isequal(p2,des,n) == 1)
    {
    fprintf(fout,"%d\n",4);fclose(fin);fclose(fout);
    return 0;
    }

    reflection(p1,n,des);
    rotate90(des,n,tmp);
    if(isequal(p2,tmp,n) == 1)
    {

    fprintf(fout,"%d\n",5);fclose(fin);fclose(fout);
    return 0;
    }

    reflection(p1,n,des);
    rotate180(des,n,tmp);
    if(isequal(p2,tmp,n) == 1)
    {

    fprintf(fout,"%d\n",5);fclose(fin);fclose(fout);
    return 0;
    }

    reflection(p1,n,des);
    rotate270(des,n,tmp);
    if(isequal(p2,tmp,n) == 1)
    {

    fprintf(fout,"%d\n",5);fclose(fin);fclose(fout);
    return 0;
    }

    if(isequal(p1,p2,n) == 1)
    {

    fprintf(fout,"%d\n",6);fclose(fin);fclose(fout);
    return 0;
    }

    fprintf(fout,"%d\n",7);
    fclose(fin);fclose(fout);
    return 0;


    }

    void rotate90(char **sour,int n,char **destin) //旋转90度
    {
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    destin[j][n-1-i] = sour[i][j];
    }
    }
    }

    void rotate180(char **sour,int n,char **destin)
    {
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    destin[n-1-i][n-1-j] = sour[i][j];
    }
    }
    }

    void rotate270(char **sour,int n,char **destin) //旋转270度
    {
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    destin[n-1-j][i] = sour[i][j];
    }
    }
    }


    void reflection(char **sour,int n, char **destin)
    {
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    destin[i][n-1-j] = sour[i][j];
    }
    }
    }

    int isequal(char **p1,char **p2,int n) //判断数组是否相等
    {
    for(int i=0; i<n; i++)
    {
    for(int j=0; j<n; j++)
    {
    if(p1[i][j] != p2[i][j])
    {
    return 0;
    }
    }
    }

    return 1;
    }

    下面是官方解答:

    /*
    We represent a board as a data structure containing the dimension and the contents. We pass around the data structure itself, not a reference to it, so that we can return new boards, and so on.

    This makes it easy to define reflect and rotate operations that return reflected and rotated boards.

    Once we have these, we just check to see what combination of transformations makes the old board into the new board.
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <assert.h>

    #define MAXN 10

    typedef struct Board Board;
    struct Board {
    int n;
    char b[MAXN][MAXN];
    };

    /* rotate 90 degree clockwise: [r, c] -> [c, n+1 - r] */
    Board
    rotate(Board b)
    {
    Board nb;
    int r, c;

    nb = b;
    for(r=0; r<b.n; r++)
    for(c=0; c<b.n; c++)
    nb.b[c][b.n+1 - r] = b.b[r][c];

    return nb;
    }

    /* reflect board horizontally: [r, c] -> [r, n-1 -c] */
    Board
    reflect(Board b)
    {
    Board nb;
    int r, c;

    nb = b;
    for(r=0; r<b.n; r++)
    for(c=0; c<b.n; c++)
    nb.b[r][b.n-1 - c] = b.b[r][c];

    return nb;
    }

    /* return non-zero if and only if boards are equal */
    int
    eqboard(Board b, Board bb)
    {
    int r, c;

    if(b.n != bb.n)
    return 0;

    for(r=0; r<b.n; r++)
    for(c=0; c<b.n; c++)
    if(b.b[r][c] != bb.b[r][c])
    return 0;
    return 1;
    }

    Board
    rdboard(FILE *fin, int n)
    {
    Board b;
    int r, c;

    b.n = n;
    for(r=0; r<n; r++) {
    for(c=0; c<n; c++)
    b.b[r][c] = getc(fin);
    assert(getc(fin) == '\n');
    }
    return b;
    }

    void
    main(void)
    {
    FILE *fin, *fout;
    Board b, nb;
    int n, change;

    fin = fopen("transform.in", "r");
    fout = fopen("transform.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d\n", &n);
    b = rdboard(fin, n);
    nb = rdboard(fin, n);

    if(eqboard(nb, rotate(b)))
    change = 1;
    else if(eqboard(nb, rotate(rotate(b))))
    change = 2;
    else if(eqboard(nb, rotate(rotate(rotate(b)))))
    change = 3;
    else if(eqboard(nb, reflect(b)))
    change = 4;
    else if(eqboard(nb, rotate(reflect(b)))
    || eqboard(nb, rotate(rotate(reflect(b))))
    || eqboard(nb, rotate(rotate(rotate(reflect(b))))))
    change = 5;
    else if(eqboard(nb, b))
    change = 6;
    else
    change = 7;

    fprintf(fout, "%d\n", change);

    exit(0);
    }


    我得代码和官方解答有很大差距啊

  • 相关阅读:
    监测你的SQL SERVER让瓶颈暴露
    SQL Server日志文件总结及日志满的处理
    SQL保持多台服务器数据的一致性
    SAS的函数
    业务单号自动增长的处理办法
    有一点迷茫了
    怎么强制弹出窗口永远在最前面(转)
    XML技术上传文件
    SQL复制表结构的通用存储过程(转)
    ACCESS中使用SQL语句应注意的地方、与sql server的区别及几点技巧(整理中)
  • 原文地址:https://www.cnblogs.com/stonehat/p/2199635.html
Copyright © 2011-2022 走看看