Thursday, June 24, 2021

Published June 24, 2021 by Anonymous with 0 comment

Check whether Matrix T is a result of one or more 90° rotations of Matrix mat

#include <bits/stdc++.h>

using namespace std;

  

string findRotation(vector<vector<int> >& mat,

                    vector<vector<int> >& T)

{

    

    

    if (T.size() != mat.size()

        || T[0].size() != mat[0].size()) {

        

        return "No";

    }

  

    

    

    map<vector<int>, int> m;

  

    

    for (int i = 0; i < mat.size(); i++) {

  

        

        

        m[mat[i]] += 1;

  

        

        reverse(mat[i].begin(), mat[i].end());

  

        

        

        m[mat[i]] += 1;

    }

  

    

    for (int i = 0; i < mat[0].size(); i++) {

  

        

        vector<int> r = {};

  

        

        for (int j = 0; j < mat.size(); j++) {

            r.push_back(mat[j][i]);

        }

  

        

        

        m[r] += 1;

  

        

        reverse(r.begin(), r.end());

  

        

        

        m[r] += 1;

    }

  

    

    for (int i = 0; i < T.size(); i++) {

  

        

        

        

        if (m[T[i]] <= 0) {

            return "No";

        }

  

        

        

        m[T[i]] -= 1;

    }

  

    

    for (int i = 0; i < T[0].size(); i++) {

  

        

        vector<int> r = {};

  

        

        for (int j = 0; j < T.size(); j++) {

            r.push_back(T[j][i]);

        }

  

        

        

        if (m[r] <= 0) {

            return "No";

        }

  

        

        

        m[r] -= 1;

    }

  

    

    return "Yes";

}

  

int main()

{

    

    vector<vector<int> > mat

        = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    vector<vector<int> > T

        = { { 3, 6, 9 }, { 2, 5, 8 }, { 1, 4, 7 } };

  

    

    cout << findRotation(mat, T);

  

    return 0;

}

Adblock test (Why?)


Original page link

Best Cool Tech Gadgets

Top favorite technology gadgets
      edit

0 comments:

Post a Comment