티스토리 뷰
문제 링크입니다 : https://programmers.co.kr/learn/courses/30/lessons/49994
코딩테스트 연습 - 방문 길이
programmers.co.kr
구현문제
#include <string>
#include <vector>
#define boardSize 11
#define start 5
using namespace std;
vector<vector<vector<bool>>> visited;
int solution(string dirs) {
int answer = 0;
visited.assign(boardSize, vector<vector<bool>>(boardSize, vector<bool>(4, false)));
int y = start;
int x = start;
for(int i = 0; i < dirs.size(); i++) {
switch(dirs[i]) {
case 'U':
if(y == 0) continue;
if(!visited[y - 1][x][1]) {
visited[y][x][0] = true;
}
y--;
break;
case 'D':
if(y == boardSize - 1) continue;
if(!visited[y + 1][x][0]) {
visited[y][x][1] = true;
}
y++;
break;
case 'L':
if(x == 0) continue;
if(!visited[y][x - 1][3]) {
visited[y][x][2] = true;
}
x--;
break;
case 'R':
if(x == boardSize - 1) continue;
if(!visited[y][x + 1][2]) {
visited[y][x][3] = true;
}
x++;
break;
}
}
for(int i = 0; i < boardSize; i++) {
for(int j = 0; j < boardSize; j++) {
for(int k = 0; k < 4; k++) {
if(visited[i][j][k]) answer++;
}
}
}
return answer;
}
개발 환경 : vscode
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
'알고리즘 > 프로그래머스' 카테고리의 다른 글
프로그래머스 - 스티커 모으기(2) (0) | 2020.09.06 |
---|---|
프로그래머스 - 배달 (0) | 2020.09.06 |
프로그래머스 - 찾아라 프로그래밍 마에스터 게임 맵 최단거리 (0) | 2020.09.02 |
프로그래머스 - 찾아라 프로그래밍 마에스터 폰켓몬 (0) | 2020.09.02 |
프로그래머스 - 2017 팁스타운 단어 퍼즐 (0) | 2020.09.02 |
댓글