-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion4.java
More file actions
37 lines (34 loc) · 1.13 KB
/
Copy pathQuestion4.java
File metadata and controls
37 lines (34 loc) · 1.13 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
package practice1_Impl;
import java.util.Arrays;
public class Question4 {
public int[] solution(int c, int r, int k){
int[] answer = new int[2];
if(k > c * r) return new int[] {0, 0};
int[][] seat = new int[c][r];
int[] dx = {-1, 0, 1, 0};
int[] dy = {0, 1, 0, -1};
int x = 0, y = 0, count = 1, d = 1; // 초기 방향
while(count < k){
int nx = x + dx[d];
int ny = y + dy[d];
if(nx < 0 || nx >= c || ny < 0 || ny >=r || seat[nx][ny] > 0){
d = (d + 1) % 4;
continue;
}
seat[x][y] = count;
count++;
x = nx;
y = ny;
}
answer[0] = x + 1;
answer[1] = y + 1;
return answer;
}
public static void main(String[] args){
Question4 T = new Question4();
System.out.println(Arrays.toString(T.solution(6, 5, 12)));
System.out.println(Arrays.toString(T.solution(6, 5, 20)));
System.out.println(Arrays.toString(T.solution(6, 5, 30)));
System.out.println(Arrays.toString(T.solution(6, 5, 31)));
}
}