Advertisement
Guest User

Majstersztyk sztuki programowania

a guest
Oct 23rd, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.14 KB | None | 0 0
  1. package problemdrogi;
  2.  
  3.  
  4. public class ProblemDrogi {
  5.  
  6.    
  7.    
  8.    
  9.     /*
  10.    
  11. package astro;
  12.  
  13. public class Astro {
  14.  
  15.     public static void main(String[] args) {
  16.        
  17.  
  18.        int[] astro = new int [4];
  19.        int[][]as = {{1,1,0,0,1},{2,0,1,0,1},{3,0,1,1,0},{4,1,1,0,0},{5,0,0,1,1}};
  20.        for(int i=0;i<=4;i++){    
  21.         for(int m=i+1;m<=4;m++){
  22.            for(int l=1;l<=4;l++){
  23.             astro[l-1]=as[i][l]+as[m][l];
  24.                   // System.out.print(astro[k][m-1]+" ");        
  25.            }
  26.            if(((astro[0])*(astro[1])*(astro[2])*(astro[3]))>=1)          
  27.                    System.out.println("Kosmonauci:"+(i+1)+" i "+(m+1));
  28.  
  29.                    }
  30.                    }                
  31.         }
  32.  
  33.        }
  34.  
  35.     */
  36.    
  37.     public static void main(String[] args) {
  38.        
  39.         int[][]times = {{0,0,0,0,0,0,0,0,0},
  40.                     {0,0  ,300,402,356, 1  ,1  ,1  ,1  ,1  },
  41.                     {0,300,0  ,1  ,1  ,440,474,1  ,1  ,1  },
  42.                     {0,402,1  ,0  ,1  ,1  ,330,1  ,1  ,1  },
  43.                     {0,356,1  ,1  ,0  ,1  ,1  ,823,1  ,1  },
  44.                     {0,1  ,440,1  ,1  ,0  ,1  ,1  ,430,1  },
  45.                     {0,1  ,474,330,1  ,1  ,0  ,813,365,774},
  46.                     {0,1  ,1  ,1  ,823,1  ,813,0  ,1  ,403},
  47.                     {0,1  ,1  ,1  ,1  ,430,365,1  ,0  ,768},
  48.                     {0,1  ,1  ,1  ,1  ,1  ,774,403,768,0  }
  49.                 };
  50.         int pomi=0,apomi=0,bpomi=0,cpomi=0,dpomi=0;
  51.         int pomj=0,apomj=0,bpomj=0,cpomj=0,dpomj=0;
  52.         int pomk=0,apomk=0,bpomk=0,cpomk=0,dpomk=0;
  53.         int poml=0,apoml=0,bpoml=0,cpoml=0,dpoml=0;
  54.         int pomm=0,apomm=0,bpomm=0,cpomm=0,dpomm=0;
  55.         int pomn=0,apomn=0,bpomn=0,cpomn=0,dpomn=0;
  56.         int pomo=0,apomo=0,bpomo=0,cpomo=0,dpomo=0;
  57.         int pomp=0,apomp=0,bpomp=0;
  58.         int min_time=100000;
  59.         int t=0;
  60.        
  61.         System.out.println("Algorytm gdy znajdzie kombinacje z mniejszym czasem minimalnym automatycznie wypisuje ją na ekranie, ostatnia wypisana kompilacja będzie tą najbardziej optymalną.");
  62.        
  63.             for (int i = 2; i < 10; i++)
  64.             for (int j = 2; j < 10; j++)
  65.             for (int k = 3; k < 10; k++)
  66.             for (int l = 4; l < 10; l++)
  67.             for (int m = 5; m < 10; m++)
  68.             for (int n = 6; n < 10; n++)
  69.             for (int o = 7; o < 10; o++)
  70.             for (int p = 8; p < 10; p++)
  71.             {
  72.                 {
  73.                     {
  74.                         {  
  75.                             {
  76.                                  {    
  77.                                      {  
  78.                                          {                
  79.                                              {
  80.                                                 if(i==2)
  81.                                                 {
  82.                                                     apomi=times[1][i];
  83.                                                     bpomi=times[i][j];
  84.                                                     cpomi=times[i][1];
  85.                                                     dpomi=times[j][i];
  86.                                                     times[1][i]=0;
  87.                                                     times[i][j]=0;
  88.                                                     times[i][1]=0;
  89.                                                     times[j][i]=0;
  90.                                                     pomi=1;
  91.                                                 }
  92.                                                 if(j==2)
  93.                                                 {
  94.                                                    
  95.                                                     apomj=times[i][j];
  96.                                                     bpomj=times[j][i];
  97.                                                     cpomj=times[j][k];
  98.                                                     dpomj=times[k][j];
  99.                                                     times[i][j]=0;
  100.                                                     times[j][i]=0;
  101.                                                     times[j][k]=0;
  102.                                                     times[k][j]=0;
  103.                                                     pomj=1;
  104.                                                 }
  105.                                                 if(k==3)
  106.                                                 {
  107.                                                     apomk=times[k][l];                                      
  108.                                                     bpomk=times[j][k];                                                  
  109.                                                     cpomk=times[l][k];
  110.                                                     dpomk=times[k][j];
  111.                                                     times[k][l]=0;
  112.                                                     times[j][k]=0;
  113.                                                     times[l][k]=0;
  114.                                                     times[k][j]=0;
  115.                                                     pomk=1;
  116.                                                 }
  117.                                                 if(l==4)
  118.                                                 {
  119.                                                     apoml=times[l][k];
  120.                                                     cpoml=times[l][m];      
  121.                                                     dpoml=times[k][l];    
  122.                                                     bpoml=times[m][l];
  123.                                                      times[l][k]=0;
  124.                                                      times[l][m]=0;
  125.                                                      times[k][l]=0;
  126.                                                      times[m][l]=0;
  127.                                                     poml=1;
  128.                                                    
  129.                                                 }
  130.                                                 if(m==5)
  131.                                                 {
  132.                                                     apomm=times[l][m];
  133.                                                             bpomm=times[n][m];
  134.                                                             cpomm=times[m][l];
  135.                                                             dpomm=times[m][n];
  136.                                                     times[l][m]=0;
  137.                                                     times[n][m]=0;
  138.                                                     times[m][l]=0;
  139.                                                     times[m][n]=0;
  140.                                                     pomm=1;
  141.                                                 }
  142.                                                 if(n==6)
  143.                                                 {
  144.                                                     apomn=times[m][n];
  145.                                                             bpomn=times[n][o];
  146.                                                             cpomn=times[n][m];
  147.                                                             dpomn=times[o][n];
  148.                                                     times[m][n]=0;
  149.                                                     times[n][o]=0;
  150.                                                     times[n][m]=0;
  151.                                                     times[o][n]=0;
  152.                                                     pomn=1;
  153.                                                 }
  154.                                                 if(o==7)
  155.                                                 {
  156.                                                             apomo=times[n][o];
  157.                                                             bpomo=times[o][p];
  158.                                                             cpomo=times[o][n];
  159.                                                             dpomo=times[p][o];
  160.                                                     times[n][o]=0;
  161.                                                     times[o][p]=0;
  162.                                                     times[o][n]=0;
  163.                                                     times[p][o]=0;
  164.                                                     pomo=1;
  165.                                                 }
  166.                                                 if(p==8)
  167.                                                 {
  168.                                                     apomp=times[o][p];
  169.                                                     bpomp=  times[p][o];
  170.                                                     times[o][p]=0;
  171.                                                     times[p][o]=0;
  172.                                                     pomp=1;
  173.                                                 }
  174.                
  175.                    
  176.                        
  177.                         t=times[1][i]+times[i][j]+times[j][k]+times[k][l]+times[l][m]+times[m][n]+times[n][o]+times[o][p]+times[p][9];
  178.                         if(min_time>t)
  179.                         {
  180.                             min_time=t;
  181.                             System.out.println("1 do "+i+"-> dlugosc:"+times[1][i]+", "+j+"-> dlugosc:"+times[i][j]+", "+k+"-> dlugosc:"+times[j][k]+", "+k+"-> dlugosc:"+times[k][l]+", "+l+"-> dlugosc:"+times[l][m]+", "+m+"-> dlugosc:"+times[m][n]+", "+n+"-> dlugosc:"+times[n][o]+", "+o+"-> dlugosc:"+times[o][p]+", "+p+"-> dlugosc:"+times[p][9]);
  182.                                     //                +j+"->"+k+"->"+l+"->"+ m+"->Sofia" + " Czas minimalny kombinacji: " + min_time);
  183.                                                                      }
  184.                                                                 }
  185.                                                     if(pomi==1)
  186.                                                 {
  187.                                                    
  188.                                                     times[1][i]=apomi;
  189.                                                     times[i][j]=bpomi;
  190.                                                     times[i][1]=cpomi;
  191.                                                     times[j][i]=dpomi;
  192.                                                     pomi=1;
  193.                                                 }                    
  194.                                                    if(pomj==1)
  195.                                                    {
  196.                                                        times[i][j]=apomj;
  197.                                                     times[j][i]=bpomj;
  198.                                                     times[j][k]=cpomj;
  199.                                                     times[k][j]=dpomj;
  200.                                                     pomj=0;
  201.                                                    }
  202.                                                    if(pomk==1)
  203.                                                    {
  204.                                                        times[k][l]=apomk;
  205.                                                     times[j][k]=bpomk;
  206.                                                     times[l][k]=cpomk;
  207.                                                     times[k][j]=dpomk;
  208.                                                     pomj=0;
  209.                                                    }
  210.                                                    if(poml==1)
  211.                                                    {
  212.                                                        times[l][k]=apoml;
  213.                                                     times[k][m]=bpoml;
  214.                                                     times[k][l]=cpoml;
  215.                                                     times[m][l]=dpoml;
  216.                                                     pomj=0;
  217.                                                    }
  218.                                                    if(pomm==1)
  219.                                                    {
  220.                                                        times[l][m]=apomm;
  221.                                                     times[n][m]=bpomm;
  222.                                                     times[m][l]=cpomm;
  223.                                                     times[m][n]=dpomm;
  224.                                                     pomj=0;
  225.                                                    }
  226.                                                    if(pomn==1)
  227.                                                    {
  228.                                                        times[m][n]=apomn;
  229.                                                     times[n][o]=bpomn;
  230.                                                     times[n][m]=cpomn;
  231.                                                     times[o][n]=dpomn;
  232.                                                     pomj=0;
  233.                                                    }
  234.                                                    if(pomo==1)
  235.                                                    {
  236.                                                        times[n][o]=apomo;
  237.                                                     times[o][p]=bpomo;
  238.                                                     times[o][n]=cpomo;
  239.                                                     times[p][o]=dpomo;
  240.                                                     pomj=0;
  241.                                                    }
  242.                                                    if(pomp==1)
  243.                                                    {
  244.                                                        times[o][p]=apomp;
  245.                                                     times[p][o]=bpomp;    
  246.                                                     pomj=0;
  247.                                                    }
  248.                                              
  249.                                          }
  250.                                      }
  251.                                  }
  252.                             }
  253.                         }
  254.                     }
  255.                 }
  256.             }
  257.             System.out.println("Kombinacja powyżej jest najbardziej optymalna");
  258.     }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement