Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Ort{
- private double X;
- private double Y;
- private boolean B;
- private String Name;
- private int Dest;
- Ort(double Xin, double Yin, String Nin){
- X=Xin;
- Y=Yin;
- Name=Nin;
- B=false;
- Dest=-1;
- }
- //Variablenset
- void setD(int In){
- Dest=In;
- }
- void setB(boolean In){
- B=In;
- }
- //Variablen ausgabe
- double getX(){
- return X;
- }
- double getY(){
- return Y;
- }
- String getN(){
- return Name;
- }
- boolean getB(){
- return B;
- }
- int getD(){
- return Dest;
- }
- double distto(Ort O2){
- return (Math.pow((X-O2.getX())*(X-O2.getX())+(Y-O2.getY())*(Y-O2.getY()), 0.5));
- }
- }
- public class Erbe{
- private static Ort[] Land=new Ort[]{new Ort(5,0,"S1"),new Ort(0,5,"S2"),new Ort(-5,0,"S3"),new Ort(0,-5,"S4"),new Ort(0,-10,"S5")};
- private static double MinDist=0;
- public static Scanner sc=new Scanner(System.in);
- public static void Ausgabe(Ort[] In){
- int c=0;
- while(Land[c].getD()!=-1){
- System.out.println("S"+c);
- c=Land[c].getD();
- }
- }
- public static void Ausgabe2(Ort[] In){
- int c=0;
- while(c<In.length){
- System.out.println("S"+c +"nach: S"+In[c].getD());
- }
- }
- public static double Wegstrecke(int Tiefe,int Loc){
- Land[Loc].setB(true);
- int c=0;
- if(Loc==0)c=1;
- double Strecke=0;
- double TDist=0;
- while (c<Land.length){
- if(!Land[c].getB()){
- TDist=Land[Loc].distto(Land[c])+Wegstrecke(Tiefe+1,c);
- }
- if (TDist<Strecke||Strecke==0){
- Strecke=TDist;
- Land[Loc].setD(c);
- }
- if (Tiefe==0&&(Strecke<MinDist||MinDist==0)){
- MinDist=Strecke;
- System.out.println("Neue kürzeste Strecke:");
- Ausgabe(Land);
- System.out.println(MinDist);
- }
- c++;
- }
- if (Tiefe==Land.length-1)Land[Loc].setD(-1);
- Land[Loc].setB(false);
- return Strecke;
- }
- public static void main(String[] Args){
- System.out.println(Land[1].distto(Land[2]));
- System.out.println(Wegstrecke(0,0));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement