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

 

https://school.programmers.co.kr/learn/courses/30/lessons/68645

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


 

 

해결방안

 

시뮬레이션, 구현

 


 

 

코드

 

#include <string>
#include <vector>
using namespace std;

int ch[1001][1001];

void Tri(int y, int x, int n) // (y, x)는 시작위치
{
	int val = ch[y][x];
	for (int i = 0; i < n; i++) // 세로 방향
		ch[y + i][x] = val++;
	
	for (int i = 1; i < n; i++) // 밑 줄 가로 방향
		ch[y + n - 1][x + i] = val++;
	
	for (int i = 1; i < n - 1; i++)  // 대각선 방향
		ch[y + n - 1 - i][x + n - 1 - i] = val++;
	
	// 만약 (y+2, x+1) 위치의 값이 0이라면, 그 위치에 (y+1, x+1) 위치의 값에 1을 더한 값을 저장
	if (ch[y + 2][x + 1] == 0)
		ch[y + 2][x + 1] = ch[y + 1][x + 1] + 1;
}

vector<int> solution(int n) {
	int size = n;

	ch[0][0] = 1; // 배열의 시작 위치인 (0, 0)에 1을 저장

	int y = 0, x = 0;
	for (int i = n; i > 0; i -= 3) {
		Tri(y, x, i);
		y += 2;
		x += 1;
	}

	vector<int> answer;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			if(ch[i][j] != 0)
				answer.push_back(ch[i][j]);
		}
	}

	return answer;
}