101 - The Block Problem UVA Online Judge Solution with Java

5:47 AM | , , , ,



import java.util.Scanner;
public class blockproblem {
    int[] cariStack(int n,String str[],String cari){
        int x=-1,i=0;
        int indx=-1;
        int hasil[]=new int[2];
        do{
            if(str[i]!=null){
                x=str[i].indexOf(cari);
            }
            if(x>=0) indx=i;
            i++;
        }while((x<0)&&(i<n));
        hasil[0]=indx;
        hasil[1]=x;
        return hasil;
    }
    public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       blockproblem bp=new blockproblem();
       int n=s.nextInt();
       String st[]=new String[n];
       for (int i = 0; i < n; i++) {
            st[i]=String.valueOf(i);
       }
       String cmd="",src,pos,des;
       while(!(cmd.equalsIgnoreCase("quit"))){
            cmd=s.next();
            if(!(cmd.equalsIgnoreCase("quit"))){
                src=s.next();
                pos=s.next();
                des=s.next();
                if(cmd.equalsIgnoreCase("move")){
                    if(pos.equalsIgnoreCase("onto")){
                        int [] urut=bp.cariStack(n,st,src);
                        int [] urutdes=bp.cariStack(n,st,des);
                        if(urut[0]>=0){
                           if(st[urutdes[0]]==null) st[urutdes[0]] = src;
                           else st[urutdes[0]] += src;
                           if(st[urut[0]].length()==src.length()) {
                                st[urut[0]]=null;
                           }else if(urut[1]==0){
                                st[urut[0]]=st[urut[0]].substring(1, st[urut[0]].length());
                           }else if(urut[1]==st[urut[0]].length()-1){
                               st[urut[0]]=st[urut[0]].substring(0, st[urut[0]].length()-1);
                           }else{
                               st[urut[0]]=st[urut[0]].substring(0, urut[1]-1)
                                       + st[urut[0]].substring(urut[1]+1, st[urut[0]].length());
                           }
                        }
                    }else if(pos.equalsIgnoreCase("over")) {
                        int [] urut=bp.cariStack(n,st,src);
                        int [] urutdes=bp.cariStack(n,st,des);
                        if(urut[0]>=0){
                           if(st[urutdes[0]+1]==null) st[urutdes[0]+1] = src;
                           else st[urutdes[0] + 1] += src;
                           if(st[urut[0]].length()==src.length()) {
                                st[urut[0]]=null;
                           }else if(urut[1]==0){
                                st[urut[0]]=st[urut[0]].substring(1, st[urut[0]].length());
                           }else if(urut[1]==st[urut[0]].length()-1){
                               st[urut[0]]=st[urut[0]].substring(0, st[urut[0]].length()-1);
                           }else{
                               st[urut[0]]=st[urut[0]].substring(0, urut[1]-1)
                                       + st[urut[0]].substring(urut[1]+1, st[urut[0]].length());
                           }
                        }
                   }
                }else if(cmd.equalsIgnoreCase("pile")){
                    if(pos.equalsIgnoreCase("onto")){
                        if(Integer.parseInt(des)<n){
                            int [] urut=bp.cariStack(n,st,src);
                            if(urut[0]>=0){
                               if(st[Integer.parseInt(des)]==null) st[Integer.parseInt(des)] = st[urut[0]];
                               else st[Integer.parseInt(des)] += st[urut[0]];
                               st[urut[0]]=null;
                            }
                        }
                    }else if(pos.equalsIgnoreCase("over")) {
                        if(Integer.parseInt(des)<n-1){                           
                            int [] urut=bp.cariStack(n,st,src);
                            if(urut[0]>=0){
                               if(st[Integer.parseInt(des)+1]==null) st[Integer.parseInt(des) + 1] = st[urut[0]];
                               else st[Integer.parseInt(des) + 1] += st[urut[0]];
                               st[urut[0]]=null;
                            }
                        }
                    }
                }
           }
       }
        for (int i = 0; i < st.length; i++) {
            System.out.println(i+":"+st[i]);
        }
    }
}

0 comments:

Post a Comment

Please leave a comment