카테고리: 배열
, 완전탐색
https://www.acmicpc.net/problem/1730
위치는 (x,y)로 두고, x와 y의 위치 변화량을 dx와 dy로 정의합니다.
//상 하 좌 우
private static int[] dx = {0,0,-1,1};
private static int[] dy = {-1,1,0,0};
private static int findDirection(char c){
switch (c){
case 'U':
return 0;
case 'D':
return 1;
case 'L':
return 2;
case 'R':
return 3;
}
return 0;
}
import java.io.*;
import java.util.*;
class Main {
private static int[] dx = {0,0,-1,1};
private static int[] dy = {-1,1,0,0};
private static int findDirection(char c){
switch (c){
case 'U':
return 0;
case 'D':
return 1;
case 'L':
return 2;
case 'R':
return 3;
}
return 0;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
char[] arr = br.readLine().toCharArray();
char[][] pan = new char[n][n];
for(char[] array : pan){
Arrays.fill(array, (char)46);
}
//시작 위치 (0,0)
int x = 0;
int y = 0;
for(int i = 0; i < arr.length; i++){
//명령에 따라 이동 방향을 정한다
int direction = findDirection(arr[i]);
//다음 위치를 구한다.
int next_x = x + dx[direction];
int next_y = y + dy[direction];
//다음 위치가 유효하다면 이동하고 아니라면 무시하고 다음명령으로 간다.
if(next_x == -1 || next_x == n || next_y == -1 || next_y == n) continue;
//현재위치와 다음위치에 흔적을 남기며 이동한다
char draw = direction == 0 || direction == 1 ? (char)124 : (char)45;
pan[y][x] = pan[y][x] == 46 || pan[y][x] == draw ? draw : (char)43;
x = next_x;
y = next_y;
pan[y][x] = pan[y][x] == 46 || pan[y][x] == draw ? draw : (char)43;
}
for(char[] array : pan){
for(char c : array){
System.out.print(c);
}
System.out.println();
}
}
}