일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 플러터 개발환경 설정
- php 프로그래밍 입문 3판
- 자바 스프링
- 파이썬
- 한정 분기
- SWEA
- php 프로그래밍 입문 문제풀이
- 자바
- php 프로그래밍 입문 예제
- C언어
- 페이코 친구코드
- programmers
- 페이코 초대코드
- php 프로그래밍 입문 솔루션
- php 프로그래밍 입문
- php 프로그래밍 입문 연습문제
- JAVA SPRING
- php 프로그래밍
- 플러터
- Flutter
- Java
- php
- spring
- 페이코 추천인
- 스프링
- 백준
- C
- 배열
- 페이코 추천인코드
- 최단 경로
Archives
- Today
- Total
02-02 06:48
ImJay
[SWEA/Java] 1873. 상호의 배틀필드 본문
반응형
[SWEA/Java] 1873. 상호의 배틀필드
해설
풀이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static char[][] graph;
static int direction, h, w, x, y;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
// 테스트 케이스의 수 입력
int testCase = Integer.parseInt(br.readLine());
for (int t = 1; t <= testCase; t++) {
// 게임 맵의 크기 입력
String input = br.readLine();
StringTokenizer st = new StringTokenizer(input);
h = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
// 게임 맵 초기화
graph = new char[h][w];
// 게임 맵 상태 입력 및 전차의 시작 위치 찾기
for (int r = 0; r < h; r++) {
input = br.readLine();
for (int c = 0; c < w; c++) {
graph[r][c] = input.charAt(c);
findStart(r, c);
}
}
// 사용자 입력의 개수 입력
int n = Integer.parseInt(br.readLine());
// 사용자 입력에 따른 동작 수행
input = br.readLine();
for (int i = 0; i < n; i++) {
runCommand(input.charAt(i));
}
// 결과 출력
sb.append("#" + t + " ");
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
sb.append(graph[i][j]);
}
sb.append("\n");
}
}
System.out.print(sb);
}
// 전차의 시작 위치 찾기
public static void findStart(int r, int c) {
if (graph[r][c] == '^' || graph[r][c] == 'v' || graph[r][c] == '<' || graph[r][c] == '>') {
x = r;
y = c;
switch (graph[r][c]) {
case '^':
direction = 1;
break;
case 'v':
direction = 2;
break;
case '<':
direction = 3;
break;
case '>':
direction = 4;
break;
}
}
}
// 사용자 입력에 따른 동작 수행
public static void runCommand(char command) {
switch (command) {
case 'U':
direction = 1;
move();
break;
case 'D':
direction = 2;
move();
break;
case 'L':
direction = 3;
move();
break;
case 'R':
direction = 4;
move();
break;
case 'S':
shoot();
break;
}
}
// 전차 이동
public static void move() {
int newX = x;
int newY = y;
char status = 0;
switch (direction) {
case 1:
newX--;
status = '^';
break;
case 2:
newX++;
status = 'v';
break;
case 3:
newY--;
status = '<';
break;
case 4:
newY++;
status = '>';
break;
}
// 이동 가능한지 확인 후 이동
if (0 <= newX && newX < h && 0 <= newY && newY < w && graph[newX][newY] == '.') {
graph[x][y] = '.';
x = newX;
y = newY;
}
graph[x][y] = status;
}
// 포탄 발사
public static void shoot() {
switch (direction) {
case 1:
for (int i = x - 1; i > -1; i--) {
if (graph[i][y] == '*') {
graph[i][y] = '.';
break;
}
if (graph[i][y] == '#') {
break;
}
}
break;
case 2:
for (int i = x + 1; i < h; i++) {
if (graph[i][y] == '*') {
graph[i][y] = '.';
break;
}
if (graph[i][y] == '#') {
break;
}
}
break;
case 3:
for (int i = y - 1; i > -1; i--) {
if (graph[x][i] == '*') {
graph[x][i] = '.';
break;
}
if (graph[x][i] == '#') {
break;
}
}
break;
case 4:
for (int i = y + 1; i < w; i++) {
if (graph[x][i] == '*') {
graph[x][i] = '.';
break;
}
if (graph[x][i] == '#') {
break;
}
}
break;
}
}
}
반응형
'SW Expert Academy > D3' 카테고리의 다른 글
[SWEA/Java] 5215. 햄버거 다이어트 (0) | 2024.02.04 |
---|---|
[SWEA/Java] 7964. 부먹왕국의 차원 관문 (0) | 2024.01.29 |
[SWEA/Java] 1860. 진기의 최고급 붕어빵 (0) | 2024.01.23 |
[SWEA/Java] 1493. 수의 새로운 연산 (1) | 2024.01.22 |
[SWEA/Java] 9229. 한빈이와 Spot Mart (1) | 2024.01.21 |
Comments