USACO section1.2 Transformations

模拟题,给出一个 n×n 的字符块的两个状态(分别为初始态和目标态)和几种操作,问目标态最少是由初始态的哪个操作完成的,WA 3 次。

/*

PROG: transform

LANG: C++

*/

# include <cstdio>

# include <cstring>



# define N 10 + 5



void print(char s[][N], int n)

{

    for (int i = 0; i < n; ++i)

        puts(s[i]);

}



/* rotate 90 degrees clockwise */

void rotate(char s[][N], int n)

{

    char buf[N][N];



    memset(buf, 0, sizeof(buf));

    for (int i = 0; i < n; ++i)

    for (int j = 0; j < n; ++j)

        buf[i][j] = s[n-j-1][i];

    memcpy(s, buf, sizeof(buf));

    //print(s, n);

}



void mirror(char s[][N], int n)

{

    char buf[N][N];



    memset(buf, 0, sizeof(buf));

    for (int i = 0; i < n; /*puts(buf[i]), */++i)

    for (int j = 0; j < n; ++j)

    {

        buf[i][j] = s[i][n-1-j];

    }

    memcpy(s, buf, sizeof(buf));

   // print(s, n);

}



char isEqual(char s[][N], char g[][N], int n)

{

    for (int i = 0; i < n; ++i)

    for (int j = 0; j < n; ++j)

        if (s[i][j] != g[i][j]) return 0;

    return 1;

}



int main()

{

    freopen("transform.in", "r", stdin);

    freopen("transform.out", "w", stdout);



    int n;

    char s[N][N], f[N][N], g[N][N];



    scanf("%d", &n);

    for(int i = 0; i < n; ++i)

        scanf("%s", s[i]);

    for(int i = 0; i < n; ++i)

        scanf("%s", g[i]);



    memcpy(f, s, sizeof(s));

    if (rotate(f, n), isEqual(f, g, n)) printf("1\n");

    else if (rotate(f, n), isEqual(f, g, n)) printf("2\n");

    else if (rotate(f, n), isEqual(f, g, n)) printf("3\n");

    else if (rotate(f, n), isEqual(s, g, n)) printf("6\n");

    else if (mirror(f, n), isEqual(f, g, n)) printf("4\n");

    else if (rotate(f, n), isEqual(f, g, n)) printf("5\n");

    else if (rotate(f, n), isEqual(f, g, n)) printf("5\n");

    else if (rotate(f, n), isEqual(f, g, n)) printf("5\n");

    else printf("7\n");



    fclose(stdin);

    fclose(stdout);



    return 0;

}

/**/

你可能感兴趣的:(transform)