삐까냥의 파도타기
1873. 상호의 배틀필드 본문
문제 출처 : https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LyE7KD2ADFAXc&
오랜만에 푸는 문제라 구현하는것에 집중했습니다.
몸풀기로 D3 문제.
2018년 3월 11일 코드 import java.util.Scanner; public class Q1873 { public static void main(String[] args) { Scanner sc = new Scanner(System.in);
int num = sc.nextInt(); for (int i = 0; i < num; i++) { int y = sc.nextInt(); int x = sc.nextInt();
char[][] map = new char[y][x]; for (int j = 0; j < y; j++) { String value = sc.next(); map[j] = value.toCharArray(); }
int inputSize = sc.nextInt(); String input = sc.next(); solution(i+1, map, inputSize, input); } }
static void solution(int num, char[][] map, int inputSize, String input) { int positionX = -1, positionY = -1; char nowDirection = 'n'; for (int y = 0; y < map.length; y++) { for (int x = 0; x < map[0].length; x++) { nowDirection = getNowDirection(map[y][x]); if (nowDirection != 'N') { positionY = y; positionX = x; y = map.length; break; } } }
for (int i = 0; i < inputSize; i++) { boolean isPossible = false;
char nowInput = input.charAt(i); switch (nowInput) { case 'U' : nowDirection = nowInput; map[positionY][positionX] = '^';
int movePosition = positionY-1; if (movePosition >= 0) { isPossible = getPossible(map[movePosition][positionX]); } if (isPossible) { map[movePosition][positionX] = '^'; map[positionY][positionX] = '.'; positionY = movePosition; } break;
case 'D' : nowDirection = nowInput; map[positionY][positionX] = 'v';
movePosition = positionY+1; if (movePosition < map.length) { isPossible = getPossible(map[movePosition][positionX]); } if (isPossible) { map[movePosition][positionX] = 'v'; map[positionY][positionX] = '.'; positionY = movePosition; } break;
case 'L' : nowDirection = nowInput; map[positionY][positionX] = '<';
movePosition = positionX-1; if (movePosition >= 0) { isPossible = getPossible(map[positionY][movePosition]); } if (isPossible) { map[positionY][movePosition] = '<'; map[positionY][positionX] = '.'; positionX = movePosition; } break;
case 'R' : nowDirection = nowInput; map[positionY][positionX] = '>';
movePosition = positionX+1; if (movePosition < map[0].length ) { isPossible = getPossible(map[positionY][movePosition]); } if (isPossible) { map[positionY][movePosition] = '>'; map[positionY][positionX] = '.'; positionX = movePosition; }
break;
case 'S' : if (nowDirection == 'U') { for (int y = positionY-1; y >= 0; y--) { boolean isChangedMap = changeMap(map, y, positionX); if (isChangedMap) { break; } } } else if (nowDirection == 'D') { for (int y = positionY+1; y < map.length; y++) { boolean isChangedMap = changeMap(map, y, positionX); if (isChangedMap) { break; } } } else if (nowDirection == 'L') { for (int x = positionX-1; x >= 0; x--) { boolean isChangedMap = changeMap(map, positionY, x); if (isChangedMap) { break; } } } else if (nowDirection == 'R') { for (int x = positionX+1; x < map[0].length; x++) { boolean isChangedMap = changeMap(map, positionY, x); if (isChangedMap) { break; } } } default : break; } }
showResult(num, map); }
static char getNowDirection(char temp) { switch (temp) { case '^' : return 'U';
case 'v' : return 'D';
case '<' : return 'L';
case '>' : return 'R';
default : return 'N'; } }
static boolean getPossible(char what) { switch (what) { case '.' : return true;
default : return false; } }
static boolean changeMap(char[][] map, int y, int x) { char temp = map[y][x]; switch (temp) { case '.' : case '-' : return false;
case '*' : map[y][x] = '.'; case '#' : default : return true; } }
static void showResult(int num, char[][] map) { System.out.print("#" + num + " "); for (int y = 0; y < map.length; y++) { for (int x = 0; x < map[0].length; x++) { System.out.print(map[y][x]); } System.out.println(); } } } |
'코딩 > SW Expert Academy' 카테고리의 다른 글
1865. 동철이의 일 분배 (0) | 2018.03.13 |
---|---|
3750. Digit sum (0) | 2018.03.12 |
1860. 진기의 최고급 붕어빵 (0) | 2017.11.15 |
1244. [S/W 문제해결 응용] 2일차 - 최대 상금 (0) | 2017.11.15 |
1491. 원재의 벽 꾸미기 (0) | 2017.11.14 |