728x90
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int tc=1;tc<=t;tc++) {
int h = sc.nextInt();
int w = sc.nextInt();
char str[][] = new char[h][w];
int posx=0,posy = 0;
for(int i=0;i<h;i++) {
String word=sc.next();
for(int j=0;j<w;j++) {
str[i][j]=word.charAt(j);
if(str[i][j]=='^'||str[i][j]=='v'||str[i][j]=='<'
||str[i][j]=='>'){
posx=i;
posy=j;
}
}
}
int n = sc.nextInt();
String cmd=sc.next();
for(int i=0;i<n;i++) {
char c = cmd.charAt(i);
if(c=='S') {
if(str[posx][posy]=='<') {
for(int j=posy-1;j>=0;j--) {
if(str[posx][j]=='#')break;
if(str[posx][j]=='*') {
str[posx][j]='.';
break;
}
}
}
else if(str[posx][posy]=='>') {
for(int j=posy+1;j<w;j++) {
if(str[posx][j]=='#')break;
if(str[posx][j]=='*') {
str[posx][j]='.';
break;
}
}
}
else if(str[posx][posy]=='^') {
for(int j=posx-1;j>=0;j--) {
if(str[j][posy]=='#')break;
if(str[j][posy]=='*') {
str[j][posy]='.';
break;
}
}
}
else {
for(int j=posx+1;j<h;j++) {
if(str[j][posy]=='#')break;
if(str[j][posy]=='*') {
str[j][posy]='.';
break;
}
}
}
}
else if(c=='U') {
str[posx][posy]='^';
if(posx-1>=0&&str[posx-1][posy]=='.') {
str[posx-1][posy]='^';
str[posx][posy]='.';
posx=posx-1;
}
}
else if(c=='D') {
str[posx][posy]='v';
if(posx+1<h&&str[posx+1][posy]=='.') {
str[posx+1][posy]='v';
str[posx][posy]='.';
posx=posx+1;
}
}
else if(c=='L') {
str[posx][posy]='<';
if(posy-1>=0&&str[posx][posy-1]=='.') {
str[posx][posy-1]='<';
str[posx][posy]='.';
posy=posy-1;
}
}
else {
str[posx][posy]='>';
if(posy+1<w&&str[posx][posy+1]=='.') {
str[posx][posy+1]='>';
str[posx][posy]='.';
posy=posy+1;
}
}
}
System.out.print("#"+tc+" ");
for(int i=0;i<h;i++) {
for(int j=0;j<w;j++) {
System.out.print(str[i][j]);
}
System.out.println();
}
}
}
}
주어진 조건을 처리했다.
처음에는 실수로 강철 벽돌이 나왔을 때 포탄이 소멸해주는 것을 처리해주지 않았다가 다시 풀었다.
728x90
'SW Expert Academy > SWEA D3' 카테고리의 다른 글
| [SW Expert Academy] 화섭이의 정수 나열 (D3) (0) | 2024.10.02 |
|---|---|
| [SW Expert Academy] 북북서 (D3) (0) | 2024.10.02 |
| [SW Expert Academy] 민정이와 광직이의 알파벳 공부 (D3) (0) | 2024.10.01 |
| [SW Expert Academy] 숫자가 같은 배수 (D3) (0) | 2024.10.01 |
| [SW Expert Academy] 알 덴테 스파게티 (D3) (9) | 2024.10.01 |