Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- dfs
- 17070번
- 2870번 c++
- Lv.3
- 백준 17070번 c++
- Unity
- Algorithm
- 백준 1103번 c++
- 2468 c++
- Lv2
- 플레이어 이동
- 코테
- 백준 2870번
- C#
- 2870번 수학숙제
- 코딩테스트
- 2870번 수학숙제 c++
- c++
- 수학숙제
- 오브젝트 풀링
- 프로그래머스
- 백준
- 백준 17070번
- 백준 1103번
- Beakjoon
- 백준 1103번 게임
- 2870번
- 백준 c++ 2468번
- 백준 c++ 2870번
- 유니티
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.3 미로 탈출 명령어 본문
https://school.programmers.co.kr/learn/courses/30/lessons/150365
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
주어진 크기의 격자 미로에서 주어진 조건에 따라 출발지에서 목적지로 가는 명령어를 구하는 문제
- 이동 우선순위 : d -> l -> r -> u // x축이 위아래임을 주의
- 한번 이동했던 위치로 다시 이동할 수 있음
- 출발지에서 도착지로 딱 맞게 도착해야 함
즉, 목적지에 딱 맞게 도착할 수 있다면 우선순위에 따른 이동을 먼저 수행해야 함
풀이 코드
using System;
using System.Text;
public class Solution
{
public enum EDirType { d = 0, l = 1, r = 2, u = 3 }
public int GetMinDis(int x, int y, int r, int c) => Math.Abs(x - r) + Math.Abs(y - c);
public string solution(int n, int m, int x, int y, int r, int c, int k)
{
// 도착할 수 없음
if(GetMinDis(x, y, r, c) > k || (GetMinDis(x, y, r, c) % 2 != 0 && k % 2 == 0))
return "impossible";
StringBuilder sb = new StringBuilder();
int[] dirY = new int[4] { 0, -1, 1, 0 };
int[] dirX = new int[4] { 1, 0, 0, -1 };
// 최단거리가 될 때까지 우선순위 이동
while(k != GetMinDis(x, y, r, c))
{
k--;
for(int i = 0; i < 4; i++)
{
int moveX = x + dirX[i];
int moveY = y + dirY[i];
if(1 <= moveX && moveX <= n && 1 <= moveY && moveY <= m)
{
x = moveX;
y = moveY;
sb.Append(((EDirType)i).ToString());
break;
}
}
}
// 최단거리 이동 d l r u
while(x < r) { sb.Append('d'); x++; }
while(y > c) { sb.Append('l'); y--; }
while(y < c) { sb.Append('r'); y++; }
while(x > r) { sb.Append('u'); x--; }
return sb.ToString();
}
}
'CodingTest > Programmers Lv.3' 카테고리의 다른 글
[프로그래머스 C#] Lv.3 양과 늑대 (0) | 2024.10.12 |
---|---|
[프로그래머스 C#] Lv.3 연속 펄스 부분 수열의 합 (0) | 2024.10.11 |
[프로그래머스 C#] Lv.3 디스크 컨트롤러 (0) | 2024.05.22 |
[프로그래머스 C#] Lv.3 모두 0으로 만들기 (0) | 2024.04.10 |
[프로그래머스 C#] Lv.3 기지국 설치 (0) | 2024.04.09 |