• P1205 方块变换题解


    题目传送门

    二维矩阵旋转的视频讲解:
    https://www.bilibili.com/video/BV1az411b7Tm/

    注意:
    重点理解在(m)(n)不一样时的情况讨论。

    #include <bits/stdc++.h>
    
    using namespace std;
    const int N = 15;
    char a[N][N];
    char c[N][N];
    char t[N][N];
    char t2[N][N];
    int n;
    
    //判断两个二维数组是不是一致
    bool equal() {
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (t[i][j] != c[i][j]) return false;
        return true;
    }
    
    int main() {
        cin >> n;
        //读入原始图案
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                cin >> a[i][j];
        //读入目标图案
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= n; ++j)
                cin >> c[i][j];
    
        //1、顺时针转90度
        //通过实际例子,找规律,得i,i--->j,n-i+1
        /**
         1 2 3        7  4  1
         4 5 6   -->  8  5  2
         7 8 9        9  6  3
         以数字1为例,原来的坐标(1,1),现在的坐标(1,3)
         以数字2为例,原来的坐标(1,2),现在的坐标(2,3)
         以数字3为例,原来的坐标(1,3),现在的坐标(3,3)
         成功发现行坐标变成了列坐标。那么列坐标呢?它是怎么变化的呢?
         以7,8,9为例,它们原来在不同的列,现在都是第1列,看来后期的
         列与行有关,当然也得与n有关,所以猜测试为n-i,结果呢,3-3=0,不对啊,
         再加个1试试,就是n-i+1,再验算一下,完全正确!
         */
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[j][n - i + 1] = a[i][j];
    
        //如果通过,输出1,返回
        if (equal()) {
            cout << 1 << endl;
            return 0;
        }
        //2、顺时针转180度
        //通过实际例子,找规律,得x,y---> n-x+1,n-y+1
        /**
         1 2 3        9  8  7
         4 5 6   -->  6  5  4
         7 8 9        3  2  1
         用纸画出来模拟一下
         找规律
         1,1--->n,n
         1,2--->n,n-1
         1,3--->n,n-2
         2,1--->n-1,n
         2,2--->n-1,n-1
         总结 : x,y---> n-x+1,n-y+1
         */
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[n - i + 1][n - j + 1] = a[i][j];
        //如果通过,输出2,返回
        if (equal()) {
            cout << 2 << endl;
            return 0;
        }
        //3、顺时针转270度
        //通过实际例子,找规律,得x,y---> n-y+1,x
        /**
         1 2 3        3  6  9
         4 5 6   -->  2  5  8
         7 8 9        1  4  7
         用纸画出来模拟一下
         找规律
         1,1--->n,1
         1,2--->n-1,1
         1,3--->n-2,1
         2,1--->n,2
         2,2--->n-1,2
         总结 : x,y---> n-y+1,x
         */
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[n - j + 1][i] = a[i][j];
    
        //如果通过,输出3,返回
        if (equal()) {
            cout << 3 << endl;
            return 0;
        }
    
        //4、图案在水平方向翻转
        //通过实际例子,找规律,得x,y---> x,n-y+1
        /**
         1 2 3        3  2  1
         4 5 6   -->  6  5  4
         7 8 9        9  8  7
         用纸画出来模拟一下
         找规律
         1,1--->1,n
         1,2--->1,n-1
         1,3--->1,n-2
         2,1--->2,n
         2,2--->2,n-1
         总结 : x,y---> x,n-y+1
         */
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[i][n - j + 1] = a[i][j];
        //如果通过,输出4,返回
        if (equal()) {
            cout << 4 << endl;
            return 0;
        }
        //5、组合 图案在水平方向翻转,
        // 然后再按照 1 ∼3 之间的一种再次转换。
        //***********************************************************************************************
        //5.1 水平+90度
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[i][n - j + 1] = a[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t2[i][j] = t[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[j][n - i + 1] = t2[i][j];
    
        //如果通过,输出5,返回
        if (equal()) {
            cout << 5 << endl;
            return 0;
        }
        //***********************************************************************************************
    
        //***********************************************************************************************
        //5.2 水平+180度
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[i][n - j + 1] = a[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t2[i][j] = t[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[n - i + 1][n - j + 1] = t2[i][j];
    
        //如果通过,输出5,返回
        if (equal()) {
            cout << 5 << endl;
            return 0;
        }
        //***********************************************************************************************
    
    
        //***********************************************************************************************
        //5.3 水平+270度
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[i][n - j + 1] = a[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t2[i][j] = t[i][j];
    
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[n - j + 1][i] = t2[i][j];
    
        //如果通过,输出5,返回
        if (equal()) {
            cout << 5 << endl;
            return 0;
        }
        //***********************************************************************************************
        //6、不改变
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                t[i][j] = a[i][j];
    
        //如果通过,输出6,返回
        if (equal()) {
            cout << 6 << endl;
            return 0;
        }
    
        //7、无效转换
        cout << 7 << endl;
        return 0;
    }
    
  • 相关阅读:
    Linux C Socket编程原理及简单实例
    clock_gettime 用法
    Linux未来监控tracing框架——eBPF
    eBPF监控工具bcc系列五工具funccount
    【转】如何测量电源纹波?
    【转】在网页中运行VB6程序
    如何为互阻抗放大器电路选择具有足够带宽的运算放大器
    互阻放大器的稳定工作及其评估
    【原创】OPA857 TEST模式使用
    [转]What you need to know about transimpedance amplifiers – part 1
  • 原文地址:https://www.cnblogs.com/littlehb/p/15040165.html
Copyright © 2020-2023  润新知