简体   繁体   English

带堆栈的配电箱路由(Java语言)

[英]Switchbox routing with stack (java language)

I've been working on my college assignment about stacks in java, but I can't seem to find the help for this assignment. 我一直在从事有关Java中堆栈的大学作业,但似乎找不到该作业的帮助。 It's about switchbox routing and I'm required to use stack for this assignment. 这是关于配电箱路由的,我需要使用堆栈进行此分配。 The assignment is due tomorrow, but there's no need to rush for answers since I'm only looking for the solutions and not the grades. 作业定于明天到期,但由于我只在寻找解决方案,而不是成绩,因此无需着急回答。 Since the problem isn't written in english, I'm going to give short explanation. 由于问题不是用英语写的,所以我将给出简短的解释。

The switchbox contains 4n pins, with n pins on each side of the box. 配电箱包含4n针,配电箱的每一侧都有n针。 The switchbox is routable only when none of the lines connecting a pair of pins intersect another. 仅当连接一对引脚的线中的任何一条都不相交时,开关盒才可布线。 The input will be pairs of connected pin numbers. 输入将是成对的连接引脚号。

A few possible solutions I've found but can't understand: 1. The problem can be solved with a similar algorithm as the one for the parentheses matching 2. Putting the pin numbers in stack and an array and compare them (this is the most confusing one) 我发现了一些可能的解决方案,但无法理解:1.该问题可以通过与括号匹配的算法类似的算法来解决。2.将引脚号放入堆栈和数组中并进行比较(这是最令人困惑的一个)

My code so far (trying the second algorithm): 到目前为止,我的代码(尝试第二种算法):

import java.util.Scanner;
import java.util.Stack;
public class Tester {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int cases=sc.nextInt();
        for(int i=0;i<cases;i++){            
            int pin=sc.nextInt();
            SwitchBox box=new SwitchBox(pin);
            for(int j=0;j<pin*4;j++){
                box.add(sc.nextInt());
            }            
            boolean res=box.isRoutable();
            if(res){
                System.out.println("routable");
            }
            else{
                System.out.println("not routable");
            }
        }
    }
    static class SwitchBox{
        Stack<Integer> pins;
        int[] pairs;
        int[] comparator;
        public SwitchBox(int pin){            
            this.pins=new Stack<Integer>();
            this.pairs=new int[(pin*4)];
            this.comparator=new int[this.pairs.length];
        }
        public boolean isRoutable(){
            Stack<Integer> s=new Stack<Integer>();
            for(int i=0;i<pairs.length;i++){
                pairs[i]=pins.peek();
                s.push(pins.pop());
            }
            for(int i=pairs.length-1;i>=0;i--){
                if(pairs[i]!=s.pop()){
                    return true;
                }
            }
            return false;
        }
        public void add(int pinNum){
            if(pins.isEmpty()){
                pins.push(pinNum);
            }
            else{
                Stack<Integer> temp=new Stack<Integer>();
                int top=(int)pins.peek();
                while(top>pinNum){
                    temp.push(pins.pop());
                    if(pins.isEmpty()) top=pinNum;
                    else top=(int)pins.peek();
                }
                pins.push(pinNum);
                while(!temp.isEmpty()){
                    pins.push(temp.pop());
                }
            }
        }
    }

}

In your add method else block is wrong. 在您的add方法中, else块是错误的。 I was unable to understand what you try to do but you need to do next thing 我无法理解您想做什么,但您需要做下一件事

public void add(int pinNum) {
    if (pins.isEmpty()) {
        pins.push(pinNum);
    } else {
        Integer last = pins.peek();
        if (last == pinNum) {
            pins.pop();
        } else {
            pins.push(pinNum);
        }
    }
}

After that isRoutable method just need to check pins stack. 之后, isRoutable方法只需要检查pins堆栈即可。 If it empty, then all fine. 如果为空,则一切正常。 Else there are intersecting lines. 否则有相交的线。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM