알고리즘/SWEA

D3 1873. 상호의 배틀필드

  • -

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LyE7KD2ADFAXc&

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

// TODO: https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5LyE7KD2ADFAXc&categoryId=AV5LyE7KD2ADFAXc&categoryType=CODE
public class Solution {
	static int T, R, C;
	static char[][] map;
	static char[] command;
	static Tank tank;
	static StringBuilder sb;
	public static void main(String[] args) throws IOException {
		//BufferedReader br = new BufferedReader(new StringReader(src));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		T = Integer.parseInt(br.readLine());
		for (int t = 1; t <= T; t++) {
			StringTokenizer tokens = new StringTokenizer(br.readLine());
			sb=new StringBuilder();
			R = Integer.parseInt(tokens.nextToken());
			C = Integer.parseInt(tokens.nextToken());
			tank = null;
			map = new char[R][C];
			for (int r = 0; r < R; r++) {
				map[r] = br.readLine().toCharArray();
				if (tank == null) {
					for (int c = 0; c < C; c++) {
						char info = map[r][c];
						if (info == '^' || info == 'v' || info == '<' || info == '>') {
							tank = new Tank(r, c);
							break;
						}
					}
				}
			}
			br.readLine();// 버림
			command = br.readLine().toCharArray();
			for (char c : command) {
				switch (c) {
				case 'S':
					switch (tank.d) {
					case '^':
						for (int i = tank.r - 1; i >= 0; i--) {
							if(shotEffect(i, tank.c)) {
								break;
							}
						}
						break;
					case 'v':
						for (int i = tank.r + 1; i < R; i++) {
							if(shotEffect(i, tank.c)) {
								break;
							}
						}
						break;
					case '>':
						for (int i = tank.c + 1; i < C; i++) {
							if(shotEffect(tank.r, i)) {
								break;
							}
						}
						break;
					default:
						for (int i = tank.c - 1; i >= 0; i--) {
							if(shotEffect(tank.r, i)) {
								break;
							}
						}
					}
					break;
				case 'U':
					go(tank.r - 1, tank.c, '^');
					break;
				case 'D':
					go(tank.r + 1, tank.c, 'v');
					break;
				case 'L':
					go(tank.r, tank.c - 1, '<');
					break;
				case 'R':
					go(tank.r, tank.c + 1,'>');
					break;
				}

			}

			sb.append("#").append(t).append(" ");
			for (char[] row : map) {
				for (char c : row) {
					sb.append(c);
				}
				sb.append("\n");
			}
			System.out.print(sb);
		}
	}


	static boolean shotEffect(int r, int c) {
		boolean isOver = false;
		char info = map[r][c];
		if (info == '*') {
			map[r][c] = '.';
			isOver = true;
		} else if (info == '#') {
			isOver = true;
		}
		return isOver;
	}

	static void go(int r, int c, char d) {
		map[tank.r][tank.c] = tank.d = d;	// 방향 표시 후
		if (0 <= r && 0 <= c && r < R && c < C && map[r][c] == '.') {	// 갈 수 있다면 가기
			map[tank.r][tank.c] = '.';
			tank.r = r;
			tank.c = c;
			map[tank.r][tank.c] = tank.d;
		}
	}

	static class Tank {
		int r, c;
		char d;

		public Tank(int r, int c) {
			this.r = r;
			this.c = c;
			d = map[r][c];
		}
	}

	private static String src = "1\r\n" +
			"3 7\r\n" +
			"***....\r\n" +
			"*-..#**\r\n" +
			"#<.****\r\n" +
			"23\r\n" +
			"SURSSSSUSLSRSSSURRDSRDS";
}
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.