๋ณด๋ ๊ฒ์ ๋ฌธ์
๐ ๊ฐ๋จํ ๊ตฌํ ๋ฌธ์ ์์.
package org.example.implemetation;
import static javax.swing.UIManager.*;
import java.util.HashMap;
public class Sol {
//๋ณด๋ํ ๊ฒ์
public Integer boardGame(int[][] board, String route){
HashMap<Character, int[]> hashMap = new HashMap<>() {{
put('U', new int[] {-1, 0});
put('D', new int[] {1, 0});
put('L', new int[] {0, -1});
put('R', new int[] {0, 1});
}};
int x=0;
int y=0;
int cnt=0;
for(int i=0;i<route.length();i++){
Character op = route.charAt(i);
int nx = x + hashMap.get(op)[0];
int ny = y + hashMap.get(op)[1];
if(checkBoundary(board.length, nx, ny)){
x=nx;
y=ny;
cnt += board[nx][ny];
}else{
return null;
}
}
return cnt;
}
public boolean checkBoundary(int length, int nx, int ny){
if(nx<0 || nx>=length || ny<0 || ny>=length) return false;
return true;
}
public static void main(String[] args) {
Sol s = new Sol();
int[][] board = new int[][] {
{0, 0, 0, 1},
{1, 1, 1, 0},
{1, 1, 0, 0},
{0, 0, 0, 0}
};
String route = "RRDLLD";
System.out.println(s.boardGame(board, route));
}
}
'์๊ณ ๋ฆฌ์ฆ๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์ ํ์ด[Queue] - ๋ฐ์ค ํฌ์ฅ, ๋ฌธ์์ด ๋ณ๊ฒฝํ๊ธฐ (0) | 2023.07.18 |
---|---|
๋ฌธ์ ํ์ด[Stack] - ๊ดํธ ๋ฌธ์ , ๊ณ์ฐ๊ธฐ ๋ฌธ์ , ๋ธ๋ผ์ฐ์ ๊ตฌํ (0) | 2023.07.12 |
๋ฌธ์ ํ์ด[ํ.๊ทธ.๋จธ] - ๊ณต์ ์ฐ์ฑ , ๋ฐํํ๋ฉด ์ ๋ฆฌ (0) | 2023.07.04 |
๋ฌธ์ ํ์ด[ํ.๊ทธ.๋จธ] - ๋ฌ๋ฆฌ๊ธฐ ๊ฒฝ์ฃผ, ์ถ์ต ์ ์ (0) | 2023.07.03 |
ํ์ ์๊ณ ๋ฆฌ์ฆ(Greedy) (0) | 2023.07.03 |