알고리즘/백준

1730_판화

베리영young 2025. 4. 26. 23:37

사용 알고리즘: 구현

사용 언어: java

 

package ssafyRoom2_Monthly_Silver_Random_Defense;

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

public class Main_bj_1730_판화 {
	static int N;
	static String order;
	static char[][] map;

	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		order = br.readLine();
		map = new char[N][N];
		for (int i = 0; i < N; i++) {
			Arrays.fill(map[i], '.');
		}
		
		int cx = 0, cy = 0;
		for (int i = 0; i < order.length(); i++) {
			char ord = order.charAt(i);
			
			int nx = -1, ny = -1;
			if(ord == 'U') {
				nx = cx-1;
				ny = cy;
				if(inTheMap(nx,ny)) {
					if(map[cx][cy] == '.' || map[cx][cy] == '|') {
						map[cx][cy] = '|';
					} else {
						map[cx][cy] = '+';
					}
					
					if(map[nx][ny] == '.' || map[nx][ny] == '|') {
						map[nx][ny] = '|';
					} else {
						map[nx][ny] = '+';
					}
					
					cx = nx;
					cy = ny;
				}
			} else if(ord == 'D') {
				nx = cx+1;
				ny = cy;
				if(inTheMap(nx,ny)) {
					if(map[cx][cy] == '.' || map[cx][cy] == '|') {
						map[cx][cy] = '|';
					} else {
						map[cx][cy] = '+';
					}
					
					if(map[nx][ny] == '.' || map[nx][ny] == '|') {
						map[nx][ny] = '|';
					} else {
						map[nx][ny] = '+';
					}
					
					cx = nx;
					cy = ny;
				}
			} else if(ord == 'L') {
				nx = cx;
				ny = cy-1;
				if(inTheMap(nx,ny)) {
					if(map[cx][cy] == '.' || map[cx][cy] == '-') {
						map[cx][cy] = '-';
					} else {
						map[cx][cy] = '+';
					}
					
					if(map[nx][ny] == '.' || map[nx][ny] == '-') {
						map[nx][ny] = '-';
					} else {
						map[nx][ny] = '+';
					}
					
					cx = nx;
					cy = ny;
				}
			} else { //R
				nx = cx;
				ny = cy+1;
				if(inTheMap(nx,ny)) {
					if(map[cx][cy] == '.' || map[cx][cy] == '-') {
						map[cx][cy] = '-';
					} else {
						map[cx][cy] = '+';
					}
					
					if(map[nx][ny] == '.' || map[nx][ny] == '-') {
						map[nx][ny] = '-';
					} else {
						map[nx][ny] = '+';
					}
					
					cx = nx;
					cy = ny;
				}
			}
		}
		
		//
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
	}

	private static boolean inTheMap(int x, int y) {
		if(x>=0&&x<N && y>=0&&y<N) return true;
		return false;
	}

}

 

중복되는 코드를 줄일 순 없을까..

'알고리즘 > 백준' 카테고리의 다른 글

1812_사탕  (1) 2025.05.05
1235_학생 번호  (0) 2025.05.05
2531_회전 초밥  (0) 2025.04.26
28088_응애(Easy)  (1) 2025.04.23
6118_숨바꼭질  (1) 2025.04.22