Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. package DemoSimple.Task2;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. public class Main {
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11. int n = Integer.parseInt(reader.readLine());
  12.  
  13. int[] samPos = new int[2];
  14. int[] nikPos = new int[2];
  15.  
  16. char[][] field = new char[n][];
  17. for (int i = 0; i <field.length; i++) {
  18. String line = reader.readLine();
  19.  
  20. field[i]=line.toCharArray();
  21.  
  22. if (line.contains("N")){
  23. nikPos[0]=i;
  24. nikPos[1]=line.indexOf('N');
  25. } else if (line.contains("S")) {
  26. samPos[0]=i;
  27. samPos[1]=line.indexOf('S');
  28. }
  29. }
  30.  
  31. String command = reader.readLine();
  32.  
  33. for (int i = 0; i <command.length() ; i++) {
  34.  
  35. moveEnemy(field);
  36.  
  37. boolean samIsDead = isSamDead(field,samPos);
  38. if (samIsDead){
  39. field[samPos[0]][samPos[1]] = 'X';
  40. System.out.println("Sam died at " + samPos[0] + ", " + samPos[1]);
  41. break;
  42. } else {
  43. moveSam(field,samPos,command.charAt(i));
  44. }
  45. if (samPos[0]==nikPos[0]){
  46. System.out.println("Nikoladze killed!");
  47. field[nikPos[0]][nikPos[1]]='X';
  48. break;
  49. }
  50. }
  51.  
  52.  
  53. for (int r = 0; r < field.length; r++) {
  54. for (int c = 0; c < field[r].length; c++) {
  55. System.out.print(field[r][c]);
  56. }
  57. System.out.println();
  58. }
  59. }
  60.  
  61. private static void moveSam(char[][] field, int[] samPos, char direction) {
  62. if (direction=='U'){
  63. field[samPos[0]--][samPos[1]]='.';
  64. field[samPos[0]][samPos[1]]='S';
  65. } else if (direction=='D'){
  66. field[samPos[0]++][samPos[1]]='.';
  67. field[samPos[0]][samPos[1]]='S';
  68. } else if (direction=='L'){
  69. field[samPos[0]][samPos[1]--]='.';
  70. field[samPos[0]][samPos[1]]='S';
  71. } else if (direction=='R'){
  72. field[samPos[0]][samPos[1]++]='.';
  73. field[samPos[0]][samPos[1]]='S';
  74. } else if (direction=='W'){
  75.  
  76. }
  77. }
  78.  
  79. private static boolean isSamDead(char[][] field, int[] samPos) {
  80. for (int i = 0; i < samPos[1]; i++) {
  81. if (field[samPos[0]][i]=='b'){
  82. return true;
  83. }
  84. }
  85. for (int i = samPos[1]+1; i < field[samPos[0]].length; i++) {
  86. if (field[samPos[0]][i]=='d'){
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92.  
  93. private static void moveEnemy(char[][] field) {
  94. for (int r = 0; r < field.length; r++) {
  95. for (int c = 0; c < field[r].length; c++) {
  96. if (field[r][c]=='b'){
  97. if (c==field[r].length-1){
  98. field[r][c]='d';
  99. } else {
  100. field[r][c++]='.';
  101. field[r][c]='b';
  102. break;
  103. }
  104. }else if (field[r][c]=='d'){
  105. if (c==0){
  106. field[r][c]='b';
  107. } else {
  108. field[r][c--]='.';
  109. field[r][c]='d';
  110. break;
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement