티스토리 뷰

 

//반시계로 추가하면서 K와 같은 숫자가 나오면 자리 반환

public class Main10157_자리배정 {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int C = sc.nextInt();
		int R = sc.nextInt();
		int K = sc.nextInt();

		if (C * R < K) {
			System.out.println(0);
			return;
		}

		int[][] seats = new int[R][C];

		int cnt = 1;
		int nx = 0, ny = 0, d = 0, x = 0, y = 0;

		int[] dx = { 0, 1, 0, -1 };
		int[] dy = { 1, 0, -1, 0 };

		while (cnt <= K) {
			seats[y][x] = cnt;
			if (cnt == K) {
				System.out.println((x + 1) + " " + (y + 1));
				return;
			}
			cnt++;

			nx = x + dx[d];
			ny = y + dy[d];

			if (nx < 0 || nx >= C || ny < 0 || ny >= R || seats[ny][nx] != 0) {
				d = (d + 1) % 4;
				nx = x + dx[d];
				ny = y + dy[d];

			}
			
			x = nx;
			y = ny;
		}

	}
}

 

// 달팽이숫자
// 시계방향으로 숫자 채우기

public class main {	
    public static void main(String args[]) throws Exception {

				Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();

		for (int test_case = 1; test_case <= T; test_case++) {

			int N = sc.nextInt();
			int[][] snail = new int[N][N];

			int N2 = 1;

			for (int start = 0; start < (N + 1) / 2; start++) {
				int len = N - 2 * start; // 가로 길이
				int x = start;
				int y = start;

				for (int n = 0; n < len - 1; n++) {
					snail[y][x++] = N2++;
				}
				for (int n = 0; n < len - 1; n++) {
					snail[y++][x] = N2++;
				}
				for (int n = 0; n < len - 1; n++) {
					snail[y][x--] = N2++;
				}
				for (int n = 0; n < len - 1; n++) {
					snail[y--][x] = N2++;
				}

				if (len == 1) {
					snail[y][x] = N2;
				}

			}
			System.out.println("#" + test_case);
			for (int[] s : snail) {
				for (int n : s)
					System.out.print(n + " ");
				System.out.println();
			}

		}

	}
}

 

후..^^ 심한 말.. 험한 욕..

R C 바꿔 생각 해서 이게 머라구 진짜 오래 붙잡고 있었다 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/03   »
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
글 보관함