아래 -> 오른쪽 -> 위 -> 왼쪽 -> ...
의 움직임이 반복됩니다.import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int k = Integer.parseInt(br.readLine());
int[][] board = new int [N][N];
int[][] direction = {{1,0},{0,1},{-1,0},{0,-1}};
int cnt = N*N;
int x = 0;
int y = 0;
int d = 0;
int[] answer = new int[2];
while (cnt>0) {
if (cnt == k) {
answer[0] = x;
answer[1] = y;
}
board[x][y] = cnt--;
// 다음으로 나아갈 수 있으면 그대로 이동
if (0<= x+direction[d][0] && x+direction[d][0] < N && 0<= y+direction[d][1] && y+direction[d][1] < N && board[x+direction[d][0]][y+direction[d][1]] ==0) {
x += direction[d][0];
y += direction[d][1];
}else { // 나아갈 수 없으면 방향을 바꿔서 이동
d =(d+1)%4;
x += direction[d][0];
y += direction[d][1];
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sb.append(board[i][j]+" ");
}
sb.append("\n");
}
System.out.print(sb.toString());
System.out.println((answer[0]+1)+" "+(answer[1]+1));
}
}