SWEA 1873 상호의 배틀필드

1 개요[ | ]

SWEA 1873 상호의 배틀필드
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 3-6

2 C++[ | ]

3 Java[ | ]

import java.util.*;
public class Solution {
	static int H, W;
	static char[][] map;
	static int x, y;
	static char direction;
	static void showMap() {
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				System.out.print(map[i][j]);
			}
			System.out.println();
		}
	}
	static void initTank() {
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				switch(map[i][j]) {
				case '^':
				case 'v':
				case '<':
				case '>':
					direction=map[i][j];
					x=j;
					y=i;
					return;
				}
			}
		}
	}
	static void move(char c) {
		int temp_x=x, temp_y=y;
		switch(c) {
		case 'U': temp_y--; direction='^'; break;
		case 'D': temp_y++; direction='v'; break;
		case 'L': temp_x--; direction='<'; break;
		case 'R': temp_x++; direction='>'; break;
		}
		if( temp_x < 0 ) temp_x = 0;
		else if( temp_x > W-1 ) temp_x = W-1;
		if( temp_y < 0 ) temp_y = 0;
		else if( temp_y > H-1 ) temp_y = H-1;
		if( map[temp_y][temp_x] == '.' ) {
			map[y][x] = '.';
			map[temp_y][temp_x] = direction;
			x = temp_x;
			y = temp_y;
			return;
		}
		map[y][x] = direction;
	}
	static void shoot() {
		int shell_x = x; 
		int shell_y = y;
		int shell_dx=0, shell_dy=0;
		switch(direction) {
		case '^': shell_dy = -1; break;
		case 'v': shell_dy =  1; break;
		case '<': shell_dx = -1; break;
		case '>': shell_dx =  1; break;
		}
		while(true) {
			shell_x += shell_dx;
			shell_y += shell_dy;
			if( shell_x < 0 || shell_x > W-1 ) return;
			if( shell_y < 0 || shell_y > H-1 ) return;
			if( map[shell_y][shell_x] == '#') return;
			if( map[shell_y][shell_x] == '*') {
				map[shell_y][shell_x] = '.';
				return;
			}
		}
	}
	static void proc(char c) {
		if( c == 'S') {
			shoot();
			return;
		}
		move(c);
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for(int tc=1; tc<=T; tc++) {
			H = sc.nextInt();
			W = sc.nextInt();
			map = new char[H][W];
			for(int i=0; i<H; i++) {
				map[i] = sc.next().toCharArray();
			}
			int N = sc.nextInt();
			String inputs = sc.next();
			initTank();
			for(int i=0; i<N; i++) {
				proc(inputs.charAt(i));
			}
			System.out.format("#%d ", tc);
			showMap();
		}
		sc.close();
	}
}

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}