백준 [1347] 미로 만들기
문제보기
풀이방법
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
static char[][] map;
static int[] dx = { 0, 0, 1, -1 }; //동서남북
static int[] dy = { 1, -1, 0, 0 };
static int len;
static int startR, startC;
static int endR, endC;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
len = Integer.parseInt(br.readLine());
map = new char[101][101];
String str = br.readLine();
startR = 50;
startC = 50;
endR = 50;
endC = 50;
maze(str, 50, 50, 2);
for (int i = startR; i <= endR; i++) {
for (int j = startC; j <= endC; j++) {
if (map[i][j] != '.')
System.out.print("#");
else
System.out.print('.');
}
System.out.println();
}
}
// dir: 0 1 2 3 -> 동 서 남 북
public static void maze(String str, int r, int c, int dir) {
map[50][50] = '.';
for (int i = 0; i < len; i++) {
char move = str.charAt(i);
if (move == 'F') {
r = r + dx[dir];
c = c + dy[dir];
map[r][c] = '.';
if (r < startR)
startR = r;
if (c < startC)
startC = c;
if (r > endR)
endR = r;
if (c > endC)
endC = c;
} else if (move == 'L') {
if (dir == 0) { // 동
dir = 3;
} else if (dir == 1) { // 서
dir = 2;
} else if (dir == 2) { // 남
dir = 0;
} else if (dir == 3) { // 북
dir = 1;
}
} else if (move == 'R') {
if (dir == 0) { // 동
dir = 2;
} else if (dir == 1) { // 서
dir = 3;
} else if (dir == 2) { // 남
dir = 1;
} else if (dir == 3) { // 북
dir = 0;
}
}
}
}
}