File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .io .*;
2+ import java .util .*;
3+
4+ public class Main {
5+ static int c ;
6+ static int r ;
7+ static int k ;
8+ static int [][] arr ;
9+ static boolean [][] visited ;
10+ // 상, 우, 하, 좌 순서 (시계 방향)
11+ static int [] dx = { 0 , 1 , 0 , -1 }; // x축(열) 변화량
12+ static int [] dy = { 1 , 0 , -1 , 0 }; // y축(행) 변화량
13+
14+ public static void main (String [] args ) {
15+ Scanner sc = new Scanner (System .in );
16+ c = sc .nextInt ();
17+ r = sc .nextInt ();
18+ k = sc .nextInt ();
19+
20+ // k가 범위를 벗어나면 0 출력
21+ if (k > c * r ) {
22+ System .out .println (0 );
23+ return ;
24+ }
25+
26+ // 방문 여부 체크용 (1~C, 1~R 사용을 위해 크기 넉넉히)
27+ visited = new boolean [c + 2 ][r + 2 ];
28+
29+ solve ();
30+ }
31+
32+ public static void solve () {
33+ int x = 1 ; // 문제의 시작 좌표 (1, 1)
34+ int y = 1 ;
35+ int dir = 0 ; // 0: 상, 1: 우, 2: 하, 3: 좌
36+
37+ visited [x ][y ] = true ; // 시작점 방문 처리
38+
39+ for (int i = 0 ; i < k - 1 ; i ++) {
40+ int nx = x + dx [dir ];
41+ int ny = y + dy [dir ];
42+
43+ // 범위를 벗어나거나 이미 방문했으면 방향 전환
44+ if (nx < 1 || nx > c || ny < 1 || ny > r || visited [nx ][ny ]) {
45+ dir = (dir + 1 ) % 4 ; // 방향 회전
46+ nx = x + dx [dir ];
47+ ny = y + dy [dir ];
48+ }
49+
50+ x = nx ;
51+ y = ny ;
52+ visited [x ][y ] = true ;
53+ }
54+
55+ System .out .println (x + " " + y );
56+ }
57+
58+ }
You can’t perform that action at this time.
0 commit comments