-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion2.java
More file actions
61 lines (54 loc) · 1.78 KB
/
Copy pathQuestion2.java
File metadata and controls
61 lines (54 loc) · 1.78 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package practice1_Impl;
import java.util.Arrays;
public class Question2 {
// 시뮬레이션 & 구현
// 배열의 이동
public int[] solution(int[][] board, int k){
int[] answer = new int[2];
int n = board.length;
// 행 , 열 합
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
int cnt = 0, x = 0, y = 0, d = 1;
while(cnt < k){
// 현재는 ( 0 , 0 ) , 움직여야 하니까 cnt + 1 초
cnt++;
// d > 오른쪽 방향 보기 시작. ( d가 direction )
int nx = x + dx[d];
int ny = y + dy[d];
if (nx < 0 || nx >= n || ny < 0 || ny >= n || board[nx][ny] == 1) {
d = (d+1) % 4; // 방향만 전환
continue;
}
x = nx;
y = ny;
}
answer[0] = x;
answer[1] = y;
return answer;
}
public static void main(String[] args){
Question2 T = new Question2();
int[][] arr1 = {
{0, 0, 0, 0, 0},
{0, 1, 1, 0, 0},
{0, 0, 0, 0, 0},
{1, 0, 1, 0, 1},
{0, 0, 0, 0, 0}
};
System.out.println(Arrays.toString(T.solution(arr1, 10)));
int[][] arr2 = {{0, 0, 0, 1, 0, 1},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1},
{1, 1, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}};
System.out.println(Arrays.toString(T.solution(arr2, 20)));
int[][] arr3 = {{0, 0, 1, 0, 0},
{0, 1, 0, 0, 0},
{0, 0, 0, 0, 0},
{1, 0, 0, 0, 1},
{0, 0, 0, 0, 0}};
System.out.println(Arrays.toString(T.solution(arr3, 25)));
}
}