1 minute read

๐Ÿ“Œ ๋‚œ์ด๋„

๐Ÿฅˆ Silver 3


๐Ÿ“Œ ๋ฌธ์ œ

https://www.acmicpc.net/problem/1913

image

image


๐Ÿ“Œ ํ’€์ด

์ด ๋‹ฌํŒฝ์ด ๋ฌธ์ œ๋Š” ๋‚ด๊ฐ€ ๋ฐฑ์ค€์„ ์ฒ˜์Œํ’€๋•Œ ํ’€๋˜ ๋ฌธ์ œ์˜€๋‹ค.
๋„์ €ํžˆ ๊ตฌํ˜„์„ ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ๋ฏธ๋ค„๋‘๊ณ , ๋‚˜์ค‘์— ํ’€๊ธฐ๋กœ ํ•œ ๋ฌธ์ œ์˜€๋‹ค.

์ฒ˜์Œ์— ๊ตฌ์ƒํ•œ ๋ฐฉ๋ฒ•์€ ์ค‘์•™๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ ํ›„, ํผ์ ธ๋‚˜๊ฐ€๋Š” ๋ฐฉ์‹์ด์˜€๋‹ค.
๊ทธ ์–ด๋– ํ•œ ๋ฒ•์น™๋„ ์ฐพ์•„๋‚ด์ง€ ๋ชปํ•˜๊ณ  ๋‹จ์ˆœํžˆ ์œ„๋กœ๊ฐ”๋‹ค๊ฐ€ ์•„๋ž˜๋กœ๊ฐ“๋‹ค๊ฐ€ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ, ๊ตฌํ˜„์ด ๋ถˆ๊ฐ€๋Šฅํ–ˆ๋‹ค.

์ดํ›„ ๋‹ค์‹œ ์ด ๋ฌธ์ œ๋ฅผ ๋ดค์„๋•Œ, ๊ทธ๋ฆผ์„ ๊ทธ๋ ค๋ณด๋‹ˆ ํ•œ๊ฐ€์ง€ ํŠน์ง•์„ ๋ฐœ๊ฒฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
๋ฐ”๋กœ ๋‹ค์Œ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 4/2์ˆœ์œผ๋กœ ๋ฐ”๊นฅ์„ ๋‘๋ฅด๋Š” ๊ฒƒ์ด์˜€๋‹ค.

image

๊ทธ๋ž˜์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.


๐Ÿ“Œ Code

package Implements;

import java.io.*;
import java.util.StringJoiner;

public class N1913 {
    static int N;
    static int total;
    static int target;
    static int[][] snail;

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        N = Integer.parseInt(br.readLine());
        total = N * N;
        target = Integer.parseInt(br.readLine());

        snail = new int[N][N];
        TotalMake();
        FindSnail();
    }

    static void FindSnail() {
        int x = 0;
        int y = 0;
        StringJoiner sj;
        for (int i = 0; i < N; i++) {
            sj = new StringJoiner(" ");
            for (int j = 0; j < N; j++) {
                sj.add(Integer.toString(snail[i][j]));
                if (snail[i][j] == target) {
                    x = i + 1;
                    y = j + 1;
                }
            }
            System.out.println(sj);
        }
        System.out.println(x + " " + y);
    }

    static void TotalMake() {
        for (int i = 0; i < N / 2; i++) {
            MakeSnail(i, N - i - 1);
        }
        snail[N / 2][N / 2] = total;
    }

    static void MakeSnail(int min, int max) {
        for (int i = min; i <= max - 1; i++) {
            snail[i][min] = total--;
        }
        for (int i = min; i <= max - 1; i++) {
            snail[max][i] = total--;
        }
        for (int i = max; i >= min + 1; i--) {
            snail[i][max] = total--;
        }
        for (int i = max; i >= min + 1; i--) {
            snail[min][i] = total--;
        }
    }
}

package (์ด๋ฆ„); ๋ฅผ ๋•Œ๊ณ , class ์ด๋ฆ„์„ Main์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ๋œ๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜