Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Demo {
- public static void main(String[] args) {
- Scanner input=new Scanner(System.in);
- int n=Integer.parseInt(input.nextLine());
- String str= input.nextLine();
- int[] field=BugsField(n,str);
- while(true){
- String string=input.nextLine();
- if(string.equals("end")){
- break;
- }
- int[] arr=commnad(string);
- if(string.contains("right")){
- moveRight(arr,field);
- }else if(string.contains("left")){
- moveLeft(arr,field);
- }
- }
- for(int i:field){
- System.out.print(i+" ");
- }
- }
- public static int[] BugsField(int size,String str){
- String[] string=str.split(" ");
- int[] arr=new int[string.length];
- int[] field=new int[size];
- for(int i=0;i < arr.length; i++){
- arr[i]=Integer.parseInt(string[i]);
- }
- for(int i=0; i < arr.length; i++){
- if(arr[i]>=0 && arr[i]<size){
- field[arr[i]]=1;
- }
- }
- return field;
- }
- public static int[] commnad(String str){
- str=str.replace(" ","");
- int[] array=new int[2];
- if(str.contains("right")){
- String[] string=str.split("right");
- for(int i=0;i<string.length;i++){
- array[i]=Integer.parseInt(string[i]);
- }
- }else if(str.contains("left")){
- String[] string=str.split("left");
- for(int i=0;i<string.length;i++){
- array[i]=Integer.parseInt(string[i]);
- }
- }
- return array;
- }
- public static void moveRight(int[] arr,int[] field){
- int start=arr[0];
- int moving=arr[1];
- for(int i=0;i<field.length;i++){
- if((start>=0 && start<=field.length-1) && field[start]==1){
- field[start]=0;
- if(start+moving>=0 && start+moving<=field.length-1){
- if(field[start+moving]==1 && start+moving+1<=field.length-1 ){
- field[start+moving+1]=1;
- }else{
- field[start+moving]=1;
- }
- }
- }
- }
- }
- public static void moveLeft(int[] arr,int[] field){
- int start=arr[0];
- int moving=arr[1];
- for(int i=0;i<field.length;i++){
- if((start>=0 && start<=field.length-1) && field[start]==1){
- field[start]=0;
- if(start-moving>=0 && start-moving<=field.length-1){
- if(field[start-moving]==1 && start-moving-1<=field.length-1 ){
- field[start-moving-1]=1;
- }else{
- field[start-moving]=1;
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement