풀이)
주의할 점)
동서남북 좌표를 이용한 문제는 방향 변수를 지정해 각각 숫자를 지정하여 푼다.
내 코드)
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();
}
}
}