Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2019
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Demo {
  4. public static void main(String[] args) {
  5. Scanner input=new Scanner(System.in);
  6. int n=Integer.parseInt(input.nextLine());
  7. String str= input.nextLine();
  8. int[] field=BugsField(n,str);
  9. while(true){
  10. String string=input.nextLine();
  11. if(string.equals("end")){
  12. break;
  13. }
  14. int[] arr=commnad(string);
  15. if(string.contains("right")){
  16. moveRight(arr,field);
  17. }else if(string.contains("left")){
  18. moveLeft(arr,field);
  19. }
  20. }
  21. for(int i:field){
  22. System.out.print(i+" ");
  23. }
  24.  
  25.  
  26. }
  27. public static int[] BugsField(int size,String str){
  28. String[] string=str.split(" ");
  29. int[] arr=new int[string.length];
  30. int[] field=new int[size];
  31. for(int i=0;i < arr.length; i++){
  32. arr[i]=Integer.parseInt(string[i]);
  33.  
  34. }
  35.  
  36. for(int i=0; i < arr.length; i++){
  37. if(arr[i]>=0 && arr[i]<size){
  38. field[arr[i]]=1;
  39. }
  40. }
  41. return field;
  42. }
  43. public static int[] commnad(String str){
  44. str=str.replace(" ","");
  45. int[] array=new int[2];
  46. if(str.contains("right")){
  47. String[] string=str.split("right");
  48. for(int i=0;i<string.length;i++){
  49. array[i]=Integer.parseInt(string[i]);
  50. }
  51. }else if(str.contains("left")){
  52. String[] string=str.split("left");
  53. for(int i=0;i<string.length;i++){
  54. array[i]=Integer.parseInt(string[i]);
  55. }
  56. }
  57. return array;
  58. }
  59. public static void moveRight(int[] arr,int[] field){
  60. int start=arr[0];
  61. int moving=arr[1];
  62. for(int i=0;i<field.length;i++){
  63. if((start>=0 && start<=field.length-1) && field[start]==1){
  64. field[start]=0;
  65. if(start+moving>=0 && start+moving<=field.length-1){
  66. if(field[start+moving]==1 && start+moving+1<=field.length-1 ){
  67. field[start+moving+1]=1;
  68. }else{
  69. field[start+moving]=1;
  70. }
  71. }
  72.  
  73. }
  74. }
  75. }
  76. public static void moveLeft(int[] arr,int[] field){
  77. int start=arr[0];
  78. int moving=arr[1];
  79. for(int i=0;i<field.length;i++){
  80. if((start>=0 && start<=field.length-1) && field[start]==1){
  81. field[start]=0;
  82. if(start-moving>=0 && start-moving<=field.length-1){
  83. if(field[start-moving]==1 && start-moving-1<=field.length-1 ){
  84. field[start-moving-1]=1;
  85. }else{
  86. field[start-moving]=1;
  87. }
  88. }
  89.  
  90. }
  91. }
  92. }
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement