Thursday, November 10, 2016

BFS

public class NovFifth {

    public static void main(String args[]){
        int a = 10;
        int b = 100;
        
        String inputOperation1 = "*2";
        String inputOperation2 = "*3";
        String inputOperation3 = "+1";
        
        char[] operations1 = inputOperation1.toCharArray();
        char[] operations2 = inputOperation2.toCharArray();
        char[] operations3 = inputOperation3.toCharArray();
        
        char operator1 = operations1[0];
        int operand1 = Character.getNumericValue(operations1[1]);
        char operator2 = operations2[0];
        int operand2 = Character.getNumericValue(operations2[1]);
        char operator3 = operations3[0];
        int operand3 = Character.getNumericValue(operations3[1]);
        
        Operation[] operationArray = new Operation[3];
        Operation Operation1 = new Operation(operator1, operand1);
        Operation Operation2 = new Operation(operator2, operand2);
        Operation Operation3 = new Operation(operator3, operand3);
        operationArray[0] = Operation1;
        operationArray[1] = Operation2;
        operationArray[2] = Operation3;
        
        
        
        Node startPoint = new Node();
        Node start = new Node();
        start.level = 0;
        start.number = a;
        startPoint = start;
        
        Node endPoint = new Node();
        endPoint = start;
        
        Node currentNode = start;
        
        int minimunLength = 0;
        while(true){
            boolean answerFlag = false;
            int currentLevel = currentNode.level;
            
            for(int i=0; i<3; i++){
                Operation op = operationArray[i];
                int answer = calc(currentNode.number, op.operator, op.operand);
                if(answer == b){
                    answerFlag = true;
                    minimunLength = currentLevel + 1;
                }else if(answer < b){
                    //Push Queue
                    Node node  = new Node();
                    node.level = currentLevel  + 1;
                    node.number = answer;
                    endPoint.next = node;
                    endPoint = endPoint.next;
                }
            }
            
            if(answerFlag){
                break;
            }
            
            //Pop Queue
            currentNode = startPoint;
            startPoint = startPoint.next;
            
        }
        printQueue(startPoint);
        System.out.println("Answer : "+minimunLength);
        
    }
    
    public static int calc(int current, char operator, int operand){
        int returnNum = 0;
        if(operator == '*'){
            returnNum = current * operand;
        }else{
            returnNum = current + operand;
        }
        
        return returnNum;
    }
    
    public static void printQueue(Node node){
        int count = 0;
        while(true){
            //System.out.print(node.number+" ");
            if(node.next == null){
                break;
            }else{
                node = node.next;
                count++;
            }
        }
    }    
}

class Operation{
    public char operator;
    public int operand;
    public Operation(char operator, int operand){
        this.operator = operator;
        this.operand = operand;
    }
    
}

class Node{
    int number;
    int level;
    Node next;
}

No comments:

Post a Comment