Notice
Recent Posts
Recent Comments
Link
변수의 기록
(코테) 프로그래머스_방문길이 *방문 길이 본문
import java.util.*;
class Solution {
public int solution(String dirs) {
int answer = 0;
//t[] current = {0,0};
int nowX = 0 , nowY = 0;
//boolean[][] visted = new boolean[10][10];
Set<String> visted = new HashSet<>();
Map<Character,int[]> map = new HashMap<>();
map.put('U',new int[]{0,1}); //U
map.put('D',new int[]{0,-1}); //D
map.put('R',new int[]{1,0}); //R
map.put('L',new int[]{-1,0}); //L
//이동
for(int i =0; i < dirs.length();i++){
int nx =nowX , ny = nowY;
int[] now =map.get(dirs.charAt(i));
int x=now[0] , y = now[1];
int nextX = nx+x , nextY = ny+y;
if(nextX>5 || nextX<-5 || nextY> 5 || nextY <-5) continue;
String path = nowX + "," + nowY + "-" + nextX + "," + nextY;
String reverse = nextX + ","+ nextY + "-" + nowX + "," + nowY;
if(!visted.contains(path)){
visted.add(path);
visted.add(reverse);
answer++;
}
nowX= nextX;
nowY= nextY;
}
return answer;
}
}
'기타 > 코딩테스트' 카테고리의 다른 글
(코테) 프로그래머스_[1차] 프렌즈4블록 *시뮬레이션,완전탐색 (0) | 2025.07.02 |
---|---|
(코테) 프로그래머스_튜플 (1) | 2025.07.01 |
(코테) 프로그래머스_124 나라의 숫자 *3진법 (0) | 2025.06.30 |
(코테) 프로그래머스_괄호 회전하기 * (0) | 2025.06.27 |
(코테) 프로그래머스_신고 결과 받기 *map,set (0) | 2025.06.27 |