Skip to content

[1주차] 이혜원 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
이혜원: [PG] 49994 방문길이_240912
  • Loading branch information
icegosimperson committed Sep 13, 2024
commit 26a216802f78e4b258ce58b18a052ddbaf79ea18
39 changes: 39 additions & 0 deletions Programmers/Level2/HW_49994.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// [PG] 49994_방문 길이
// 게임 캐릭터가 지나간 길 중 캐릭터가 처음 걸어본 길의 길이 구하기
// 좌표평면(-5, 5) 벗어나는 명령어는 무시
import java.util.*;
class Solution {
private static boolean isValidMove(int nx, int ny){
return 0 <= nx && nx < 11 && 0 <= ny && ny < 11;
}
private static final HashMap<Character, int[]> location = new HashMap<>();

private static void initLocation(){
location.put('U', new int[]{0, 1});
location.put('D', new int[]{0, -1});
location.put('L', new int[]{-1, 0});
location.put('R', new int[]{1, 0});
}
public int solution(String dirs) { // U(위), D(아래), L, R
initLocation();
int x = 5, y=5;
HashSet<String> answer = new HashSet<>();

for(int i=0; i<dirs.length(); i++){
int[] offset = location.get(dirs.charAt(i));
int nx = x + offset[0];
int ny = y + offset[1];

if(!isValidMove(nx, ny))
continue;

answer.add(x + " " + y + " " + nx + " " + ny);
answer.add(nx + " " + ny + " " + x + " " + y);

x = nx;
y = ny;
}

return answer.size()/2;
}
}