Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Solution {
- public static void main(String[] args) {
- Scanner sc=new Scanner(System.in);
- int t=sc.nextInt();
- for(int a=1;a<=t;a++) {
- int h=sc.nextInt();
- int w=sc.nextInt();
- char[][]map=new char[h][w];
- int[]tank=new int[2];
- for(int b=0;b<h;b++) {
- String s=sc.next();
- for(int c=0;c<w;c++) {
- map[b][c]=s.charAt(c);
- if(map[b][c]=='^'||map[b][c]=='v'||map[b][c]=='<'||map[b][c]=='>') {
- tank[0]=b;
- tank[1]=c;
- }
- }
- }
- int n=sc.nextInt();
- String s=sc.next();
- for(int b=0;b<n;b++) {
- char command=s.charAt(b);
- if(command=='S') {
- switch(map[tank[0]][tank[1]]) {
- case '^':
- shoot(map,tank[0],tank[1],h,w,'U');
- break;
- case 'v':
- shoot(map,tank[0],tank[1],h,w,'D');
- break;
- case '<':
- shoot(map,tank[0],tank[1],h,w,'L');
- break;
- case '>':
- shoot(map,tank[0],tank[1],h,w,'R');
- break;
- }
- }
- else {
- move(map,tank[0],tank[1],h,w,tank,command);
- }
- System.out.print("");
- }
- System.out.print("#"+a+" ");
- for(int b=0;b<h;b++) {
- for(int c=0;c<w;c++) {
- System.out.print(map[b][c]);
- }
- System.out.println();
- }
- }
- sc.close();
- }
- private static void move(char[][] map, int i, int j, int h, int w, int[] tank, char command) {
- switch(command) {
- case 'U':
- map[i][j]='^';
- if(i-1>=0&&map[i-1][j]=='.') {
- tank[0]=i-1;
- map[i-1][j]=map[i][j];
- map[i][j]='.';
- }
- break;
- case 'D':
- map[i][j]='v';
- if(i+1<=h-1&&map[i+1][j]=='.') {
- tank[0]=i+1;
- map[i+1][j]=map[i][j];
- map[i][j]='.';
- }
- break;
- case 'L':
- map[i][j]='<';
- if(j-1>=0&&map[i][j-1]=='.') {
- tank[1]=j-1;
- map[i][j-1]=map[i][j];
- map[i][j]='.';
- }
- break;
- case 'R':
- map[i][j]='>';
- if(j+1<=w-1&&map[i][j+1]=='.') {
- tank[1]=j+1;
- map[i][j+1]=map[i][j];
- map[i][j]='.';
- }
- break;
- }
- }
- private static void shoot(char[][] map, int i, int j, int h, int w, char c) {
- switch(c) {
- case 'U':
- if(i-1>=0) {
- if(map[i-1][j]=='*') {
- map[i-1][j]='.';
- }
- else if(map[i-1][j]=='.'||map[i-1][j]=='-') {
- shoot(map,i-1,j,h,w,c);
- }
- }
- break;
- case 'D':
- if(i+1<=h-1) {
- if(map[i+1][j]=='*') {
- map[i+1][j]='.';
- }
- else if(map[i+1][j]=='.'||map[i+1][j]=='-') {
- shoot(map,i+1,j,h,w,c);
- }
- }
- break;
- case 'L':
- if(j-1>=0) {
- if(map[i][j-1]=='*') {
- map[i][j-1]='.';
- }
- else if(map[i][j-1]=='.'||map[i][j-1]=='-') {
- shoot(map,i,j-1,h,w,c);
- }
- }
- break;
- case 'R':
- if(j+1<=w-1) {
- if(map[i][j+1]=='*') {
- map[i][j+1]='.';
- }
- else if(map[i][j+1]=='.'||map[i][j+1]=='-') {
- shoot(map,i,j+1,h,w,c);
- }
- }
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment