Coding_Test 연습/Programmers

[프로그래머스] (C++) LV2 삼각 달팽이

Codetesing 2022. 6. 1. 09:29

흔한 좌표 회전 문제이다.

아래, 왼쪽, 왼쪽위 대각선 3개의 경우를 나눠서 풀면 된다.

#include <string>
#include <vector>

using namespace std;

vector<int> solution(int n) {
    vector<int> answer;
    vector<vector<int>> map(n, vector<int>(n, 0));

    int row = -1, col = 0, num = 1;

    for(int i = 0; i < n; i++)
        for (int j = i; j < n; j++)
        {
            if (i % 3 == 0)
                row++;
            else if (i % 3 == 1)
                col++;
            else
            {
                row--;
                col--;
            }

            map[row][col] = num++;
        }

    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (map[i][j] != 0)
                answer.push_back(map[i][j]);

    return answer;
}