백준 1347

hong030·2023년 3월 4일
0
  • 실버 3단계 문제

풀이)

  1. 입력 받기.
  2. 입력받는 num의 최대는 50이므로, 첫 시작 좌표를 50,50으로 잡는다. 또한 북쪽은 1, 동쪽 2, 남쪽 3, 서쪽 4로 지정해 오른쪽 회전할 때마다 방향 +1, 왼쪽 회전할 때마다 방향 -1을 해준다. 첫 시작은 남쪽을 보고 있으므로 방향 = 3으로 지정한다.
  3. 왔다간 곳의 좌표를 정규화해준다.
  4. 해당 크기만큼의 배열을 만들어 "#"으로 초기화하고, 간 곳의 좌표는 "."으로 바꾼다.

주의할 점)

동서남북 좌표를 이용한 문제는 방향 변수를 지정해 각각 숫자를 지정하여 푼다.

내 코드)

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

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

		//입력받기.
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		int num = Integer.parseInt(bf.readLine());
		String input[] = bf.readLine().split("");
		
		//내가 이동한 곳 좌표값을 저장할 table.
		//첫 시작은 50,50에 있고 남쪽을 보므로 (50,50, 3) 이다.
		ArrayList<int[]> table = new ArrayList<>();
		
		int dir = 3;
		int x=50, y=50;
		int []loc = {x, y};
		table.add(loc);
		
		for(int i=0;i<num;i++) {
			if(input[i].equals("R")) {
				dir++;
				if(dir>4) 
					dir = 1;
			}
			
			else if (input[i].equals("L")) {
				dir--;
				if(dir<1) 
					dir = 4;				

			}
			
			else {
				if(dir==1) {
					x--;
				}else if (dir==2) {
					y++;
				}else if (dir==3) {
					x++;
				}else {
					y--;
				}
				
				int[] arrN = {x, y};
				table.add(arrN);
			}
		}
		for(int i=0;i<table.size();i++) {
			System.out.printf("%d %d \n", table.get(i)[0], table.get(i)[1]);
		}
		
		
		int maxX = table.get(0)[0], minX = table.get(0)[0];
		int maxY = table.get(0)[1], minY = table.get(0)[1];
		for(int i=0;i<table.size();i++) {
			if (maxX<table.get(i)[0]) maxX = table.get(i)[0];
			if (maxY<table.get(i)[1]) maxY = table.get(i)[1];
			if (minX>table.get(i)[0]) minX = table.get(i)[0];
			if (minY>table.get(i)[1]) minY = table.get(i)[1];
		}
		
		String [][]newTable = new String[maxX-minX+1][maxY-minY+1];
		
		for(int i=0;i<newTable.length;i++) {
		    for(int j=0;j<newTable[0].length;j++) {
		    	newTable[i][j]="#";
		    }
		}		
		
		for(int i=0;i<table.size();i++) {
			int a = table.get(i)[0]-minX;
			int b = table.get(i)[1]-minY;
			newTable[a][b] = ".";
		}
		
		for(int i=0;i<newTable.length;i++) {
			for(int j=0;j<newTable[0].length;j++) {
				System.out.print(newTable[i][j]);
			}
			System.out.println();
		}
		
		
	}
}

profile
자바 주력, 프론트 공부 중인 초보 개발자. / https://github.com/hongjaewonP

0개의 댓글