JAVA/Coding Test

[JAVA] 백준 1913 달팽이_탐색

오늘도개발 2024. 7. 5. 20:12

 

문제 : 

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

 

 

접근 : 

 

  - 아래, 오른쪽, 위, 왼쪽 순으로 방향을 설정한다.

 

  - 만약 다음 진행할 위치가 벽밖으로 나가거나 이미 숫자가 있는 경우에는 방향을 다음 순번으로 옮긴다.

 

  - 만약, 방향이 왼쪽 다음 인 경우 처음 순번인 아래 쪽부터 다시 진행한다.

 

 

 

 

 

코드 구현 :

 

import java.io.*;

public class Main {
    static class Position{
        private int x;
        private int y;

        public Position(int x, int y){
            this.x = x;
            this.y = y;
        }
        public int getX() {
            return x;
        }
        public int getY() {
            return y;
        }
    }

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


        Position[] dir = {
                new Position(1,0),new Position(0,1),new Position(-1,0),new Position(0,-1)
        };

        int[][] map = new int[n][n];
        Position now = new Position(0,0);
        Position next;
        int flag = 0;
        int val = n*n;
        while(val > 0){
            map[now.getX()][now.getY()] = val--;

            next = new Position(now.getX() + dir[flag].getX(), now.getY() + dir[flag].getY());
            if(next.getX() >= n || next.getY() >= n || next.getX() < 0 || map[next.getX()][next.getY()] != 0){
                flag = (flag < 3) ? flag + 1 : 0;
            }
            now = new Position(now.getX() + dir[flag].getX(), now.getY() + dir[flag].getY());
        }

        Position target_position = new Position(-1,-1);

        for(int i = 0 ; i < n ; i++){
            for(int j = 0 ; j < n ; j++){
                if(map[i][j] == target) target_position = new Position(i+1, j+1);
                sb.append(map[i][j]).append(" ");
            }
            sb.append("\n");
        }
        sb.append(target_position.getX()).append(" ").append(target_position.getY());
        System.out.println(sb);
        br.close();
    }
}