Guest User

Untitled

a guest
Mar 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. import java.util.*;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. Scanner sc=new Scanner(System.in);
  5. int t=sc.nextInt();
  6. for(int a=1;a<=t;a++) {
  7. int h=sc.nextInt();
  8. int w=sc.nextInt();
  9. char[][]map=new char[h][w];
  10. int[]tank=new int[2];
  11. for(int b=0;b<h;b++) {
  12. String s=sc.next();
  13. for(int c=0;c<w;c++) {
  14. map[b][c]=s.charAt(c);
  15. if(map[b][c]=='^'||map[b][c]=='v'||map[b][c]=='<'||map[b][c]=='>') {
  16. tank[0]=b;
  17. tank[1]=c;
  18. }
  19. }
  20. }
  21. int n=sc.nextInt();
  22. String s=sc.next();
  23. for(int b=0;b<n;b++) {
  24. char command=s.charAt(b);
  25. if(command=='S') {
  26. switch(map[tank[0]][tank[1]]) {
  27. case '^':
  28. shoot(map,tank[0],tank[1],h,w,'U');
  29. break;
  30. case 'v':
  31. shoot(map,tank[0],tank[1],h,w,'D');
  32. break;
  33. case '<':
  34. shoot(map,tank[0],tank[1],h,w,'L');
  35. break;
  36. case '>':
  37. shoot(map,tank[0],tank[1],h,w,'R');
  38. break;
  39. }
  40. }
  41. else {
  42. move(map,tank[0],tank[1],h,w,tank,command);
  43. }
  44. System.out.print("");
  45. }
  46. System.out.print("#"+a+" ");
  47. for(int b=0;b<h;b++) {
  48. for(int c=0;c<w;c++) {
  49. System.out.print(map[b][c]);
  50. }
  51. System.out.println();
  52. }
  53. }
  54. sc.close();
  55. }
  56.  
  57. private static void move(char[][] map, int i, int j, int h, int w, int[] tank, char command) {
  58. switch(command) {
  59. case 'U':
  60. map[i][j]='^';
  61. if(i-1>=0&&map[i-1][j]=='.') {
  62. tank[0]=i-1;
  63. map[i-1][j]=map[i][j];
  64. map[i][j]='.';
  65. }
  66. break;
  67. case 'D':
  68. map[i][j]='v';
  69. if(i+1<=h-1&&map[i+1][j]=='.') {
  70. tank[0]=i+1;
  71. map[i+1][j]=map[i][j];
  72. map[i][j]='.';
  73. }
  74. break;
  75. case 'L':
  76. map[i][j]='<';
  77. if(j-1>=0&&map[i][j-1]=='.') {
  78. tank[1]=j-1;
  79. map[i][j-1]=map[i][j];
  80. map[i][j]='.';
  81. }
  82. break;
  83. case 'R':
  84. map[i][j]='>';
  85. if(j+1<=w-1&&map[i][j+1]=='.') {
  86. tank[1]=j+1;
  87. map[i][j+1]=map[i][j];
  88. map[i][j]='.';
  89. }
  90. break;
  91. }
  92. }
  93.  
  94. private static void shoot(char[][] map, int i, int j, int h, int w, char c) {
  95. switch(c) {
  96. case 'U':
  97. if(i-1>=0) {
  98. if(map[i-1][j]=='*') {
  99. map[i-1][j]='.';
  100. }
  101. else if(map[i-1][j]=='.'||map[i-1][j]=='-') {
  102. shoot(map,i-1,j,h,w,c);
  103. }
  104. }
  105. break;
  106. case 'D':
  107. if(i+1<=h-1) {
  108. if(map[i+1][j]=='*') {
  109. map[i+1][j]='.';
  110. }
  111. else if(map[i+1][j]=='.'||map[i+1][j]=='-') {
  112. shoot(map,i+1,j,h,w,c);
  113. }
  114. }
  115. break;
  116. case 'L':
  117. if(j-1>=0) {
  118. if(map[i][j-1]=='*') {
  119. map[i][j-1]='.';
  120. }
  121. else if(map[i][j-1]=='.'||map[i][j-1]=='-') {
  122. shoot(map,i,j-1,h,w,c);
  123. }
  124. }
  125. break;
  126. case 'R':
  127. if(j+1<=w-1) {
  128. if(map[i][j+1]=='*') {
  129. map[i][j+1]='.';
  130. }
  131. else if(map[i][j+1]=='.'||map[i][j+1]=='-') {
  132. shoot(map,i,j+1,h,w,c);
  133. }
  134. }
  135. break;
  136. }
  137. }
  138. }
Add Comment
Please, Sign In to add comment