백준_1913_달팽이

덤벨로퍼·2023년 12월 16일
0

코테

목록 보기
19/37

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

While 문을 이용한 풀이

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

public class Main {
    static int N;
    static int find;
    static int[][] arr;
    static int[] dx = {1, 0, -1, 0}; // 하, 우, 상, 좌
    static int[] dy = {0, 1, 0, -1};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        N = Integer.parseInt(br.readLine());
        find = Integer.parseInt(br.readLine());

        arr = new int[N][N];

        initialize();

        int x = 0;
        int y = 0;

        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (arr[i][j] == find) {
                    x = i + 1;
                    y = j + 1;
                }
                sb.append(arr[i][j]).append(" ");
            }
            sb.append("\n");
        }
        sb.append(x).append(" ").append(y);

        System.out.println(sb);
    }

    private static void initialize() {
        int dir = 0;
        int x = 0;
        int y = 0;

        arr[x][y] = N * N;

        while (true) {
            int nx = x + dx[dir];
            int ny = y + dy[dir];

            if (nx >= 0 && ny >= 0 && nx < N && ny < N && arr[nx][ny] == 0) {
                arr[nx][ny] = arr[x][y] - 1;

                if (arr[nx][ny] == 1) {
                    break;
                }
                x = nx;
                y = ny;
            } else {
                dir = (dir + 1) % 4;
            }
        }
    }
}

for문을 사용한 풀이

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

public class Main {
    static int N, find;
    static int[][] arr;
    static int[] dx = {1, 0, -1, 0};
    static int[] dy = {0, 1, 0, -1};
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        N = Integer.parseInt(br.readLine());
        find = Integer.parseInt(br.readLine());
        arr = new int[N][N];
        
        init();
        
        StringBuilder sb = new StringBuilder();
        int x = 0;
        int y = 0;
        for(int i = 0; i < N; i++){
            for(int j = 0; j < N; j++){
                if(arr[i][j] == find){
                    x = i + 1;
                    y = j + 1;
                }
                sb.append(arr[i][j]).append(" ");
            }
            sb.append("\n");
        }
        sb.append(x).append(" ").append(y);
        System.out.print(sb);
        
    }

    private static void init() {
        int dir = 0;
        int x = 0;
        int y = 0;

        for (int i = N * N; i >= 1; i-- ) {
            arr[x][y] = i;

            int nx = x + dx[dir];
            int ny = y + dy[dir];

            if (nx < 0 || nx >= N || ny < 0 || ny >= N || arr[nx][ny] != 0) {
                dir = (dir + 1) % 4;
                nx = x + dx[dir];
                ny = y + dy[dir];
            }
            x = nx;
            y = ny;
        }
    }
}
profile
💪 점진적 과부하로 성장하는 개발자

0개의 댓글