[Programmers] 행렬 테두리 회전하기(Lv.2)

Alice·2023년 7월 31일
0

풀이 소요시간 : 30분

단순 구현문제다. 구현은 약 20분정도에 마쳤으나, C++ 문법을 잘못 알고있어서 큰일날 뻔한 문제다.

구조체의 값을 외부에서 변경하려면 꼭 &(참조) 가 필요하다는 것이다.

전역 변수로 선언된 배열이나 벡터값은 참조표기 없이 평소 값 수정이 자율적이였던지라 왜 값 변경이 안되고 예상하는 값이 나오지 않는지 한참 고민했다. 이것 말고는 따로 트러블은 없었다.


전체 코드

#include <string>
#include <algorithm>
#include <vector>

using namespace std;

struct Node {
    int x;
    int y;
    int num;
};

int x1, y1;
int x2, y2;

vector<Node> Vector;
int Map[101][101];

void Rotation() {
    
    for(auto& E : Vector)
    {
        if(E.x == x1)
        {
            if(E.y == y2)
            {
                E.x++;
            }
            else 
            {
                E.y++;
            }
        }
        else if(E.x == x2)
        {
            if(E.y == y1)
            {
                E.x--;
            }
            else
            {
                E.y--;
            }
        }
        else
        {
            if(E.y == y1)
            {
                E.x--;
            }
            else if(E.y == y2)
            {
                E.x++;
            }
        }
    }
    
}

vector<int> solution(int rows, int columns, vector<vector<int>> queries) {
    
    vector<int> Ans;
    for(int i = 1; i <= rows; i++)
    {
        for(int j = 1; j <= columns; j++)
        {
            Map[i][j] = (i - 1) * columns + j;
        }
    }
    //전역 변수 초기화
    
    for(vector<int> v : queries)
    {
        x1 = v[0];
        y1 = v[1];
        x2 = v[2];
        y2 = v[3];
        
        //꼭지점 노드 삽입
        Vector.push_back({x1, y1, Map[x1][y1]});
        Vector.push_back({x1, y2, Map[x1][y2]});
        Vector.push_back({x2, y1, Map[x2][y1]});
        Vector.push_back({x2, y2, Map[x2][y2]});
        
        for(int i = x1 + 1; i < x2; i++)
        {
            Vector.push_back({i, y1, Map[i][y1]});
            Vector.push_back({i, y2, Map[i][y2]});
        }
        
        for(int i = y1 + 1; i < y2; i++)
        {
            Vector.push_back({x1, i, Map[x1][i]});
            Vector.push_back({x2, i, Map[x2][i]});
        }
        
        Rotation();
        int Min = 100000000;
        for(auto E : Vector)
        {
            Map[E.x][E.y] = E.num;
            Min = min(Min, E.num); 
        }
    
        Ans.push_back(Min);
        Vector.clear();
        
    }
    
    return Ans;
    
}
profile
SSAFY 11th

0개의 댓글