计蒜客 B. 网格画 矩阵旋转

传送门
思路:通过这道题我发现了实现矩阵旋转的三种方法。
第一种:再开一个新数组 将 原矩阵模拟旋转,再将值赋给原矩阵。
第二种:原矩阵直接操作,暂时想不起。
第三种:先转成转置矩阵,再模拟列互换。

/**
* From:
* Qingdao Agricultural University
* Created by XiangwangAcmer
* Date : 2019-11-09-13.14.48
* Talk is cheap.Show me your code.
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<string>
#include<cstdlib>
#define ll long long
using namespace std;
const ll maxn = 1e6 + 5;
const ll minn = 1e9 + 5;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LIMIT = 4294967295LL;
vector<int>v[maxn];
int dp[maxn];
vector<int>G[maxn];
bool row[maxn], col[maxn];
bool flag = 0;
queue<int>q;
int n;
char a[505][505], b[505][505], c[505][505];
bool solve()
{
    int cnt = 0;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            if(a[i][j] == b[i][j])
                cnt++;
    if(cnt == n * n)
        return 1;
    return 0;
}
int tran()
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            c[i][j] = a[n - j - 1][i];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            a[i][j] = c[i][j];
}
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                cin >> a[i][j];
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                cin >> b[i][j];
        if(solve())
            cout << 0 << endl;
        else
        {
            tran();
            if(solve())
                cout << 90 << endl;
            else
            {
                tran();
                if(solve())
                    cout << 180 << endl;
                else
                {
                    tran();
                    if(solve())
                        cout << 270 << endl;
                        else cout << -1 << endl;
                }
            }
        }
    }
    return 0;
}


发布了244 篇原创文章 · 获赞 8 · 访问量 5125

猜你喜欢

转载自blog.csdn.net/weixin_43960370/article/details/102992161