https://school.programmers.co.kr/learn/courses/30/lessons/68645#qna
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;
/*
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
*/
vector<int> getAnswer(const vector<vector<int> > arr, int n) {
vector<int> v;
for (int i = 0; i < n; i++)
for (int j = 0; j <= i; j++)
v.push_back(arr[i][j]);
return v;
}
vector<int> solution(int n) {
vector<vector<int> > arr(n, vector<int>(n, 0));
int i = 0, x = 0, y = 0;
int command = 1;
while (arr[x][y] == 0) { // 넣을 자리에 이미 들어가 있으면 종료
arr[x][y] = ++i;
if (command == 1 && (x >= n - 1 || arr[x + 1][y] != 0))
command++;
else if (command == 2 && (y >= n - 1 || arr[x][y + 1] != 0))
command++;
else if (command == 3 && arr[x - 1][y - 1] != 0) // 0 아니면 이미 채워진 거 => 내려가면 됨 cmd 1
command = 1;
if (command == 1)
x++;
else if (command == 2)
y++;
else if (command == 3) {
x--; y--;
}
}
return getAnswer(arr, n);
}
알고 계셨나요?
else if (command == 3)
x--; y--;
이렇게 한 줄로 줄여쓸 수 있지만 {}를 생략하고 쓸 순 없다는 사실!
이거 덕분에 두시간을 내다 버렸습니다....
알려주신 jahlee님께 그랜절 박습니다
'<algorithm> > 프로그래머스' 카테고리의 다른 글
프로그래머스 무인도 여행 c++ (1) | 2023.09.02 |
---|---|
프로그래머스 쿼드압축 후 개수 세기 c++ (0) | 2023.09.02 |
프로그래머스 게임 맵 최단거리 c++ (dfs, bfs) (0) | 2023.08.31 |
프로그래머스 단어 변환 c++ (0) | 2023.08.30 |
프로그래머스 [3차] n진수 게임 c++ (0) | 2023.08.30 |