Advertisement
steverobinson

Ant tunnel

Oct 15th, 2010
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. class colonysets
  6. {
  7.     public:
  8.         struct coordinates
  9.         {
  10.             int x,y;
  11.             int index;
  12.         };
  13.  
  14.         class adjacency
  15.         {
  16.            public:
  17.             float distance;
  18.             bool traversed;
  19.         };
  20.         coordinates colonies[10];               //Max Number of Colonies would be 10 according to Question
  21.         float length[10];
  22.         int path[10];
  23.         adjacency adjMat[10][10];
  24.         int correctPath[10];
  25.         float leastLength;
  26.         int n;
  27.  
  28.         colonysets(int tn)                      //Constructor
  29.         {
  30.             n=tn;
  31.             for(int i=0;i<n;i++)
  32.                 length[i]=0;
  33.  
  34. /****Receiving Inputs*****/
  35.  
  36.             for(int i=0;i<n;i++)
  37.             {
  38.                 cin>>colonies[i].x;
  39.                 cin>>colonies[i].y;
  40.                 colonies[i].index=i;
  41.             }
  42.  
  43. /*****ADjacency Matrix Initialization*****/
  44.  
  45.             for(int i=0;i<n;i++)
  46.                 for(int j=0;j<n;j++)
  47.                 {
  48.                     if(i==j)
  49.                         adjMat[i][j].distance=0;
  50.                     else
  51.                     {
  52.                         float distance;
  53.                         float temp;
  54.                         temp=((colonies[i].x-colonies[j].x)*(colonies[i].x-colonies[j].x))+((colonies[i].y-colonies[j].y)*(colonies[i].y-colonies[j].y));
  55.                         distance=sqrt(temp);
  56.                         adjMat[i][j].distance=distance;
  57.                     }
  58.                 }
  59.  
  60. /** ADJ MAT PRINT**/
  61.                 /*for(int i=0; i<n;i++)
  62.                 {
  63.                     cout<<endl;
  64.                     for(int j=0;j<n;j++)
  65.                     cout<<"\t"<<adjMat[i][j].distance;
  66.                 }*/
  67.  
  68. /*****Distance Calculation For all possible Paths****/
  69.  
  70.             for(int i=0;i<n;i++)
  71.             {
  72.                 for(int l=0;l<n;l++)
  73.                     for(int m=0;m<n;m++)
  74.                     {
  75.                         path[l]=-1;
  76.                         adjMat[l][m].traversed=false;
  77.                     }
  78.  
  79.                for(int d=0;d<n;d++)
  80.                     adjMat[d][i].traversed=true;
  81.  
  82.                 path[0]=i;
  83.                 int k=1;
  84.                 int count=0;
  85.                 for(int j=path[0];count<n;j=path[k-1],count++)
  86.                 {
  87.  
  88.                     float templength=32767;
  89.                     for(int c=0;c<n;c++)
  90.                     {
  91.                         if((adjMat[j][c].traversed==false)&&(adjMat[j][c].distance<templength)&&(adjMat[j][c].distance!=0))
  92.                         {
  93.                             templength=adjMat[j][c].distance;
  94.                             path[k]=c;
  95.                         }
  96.                     }
  97.                     for(int a=0;a<n;a++)
  98.                             adjMat[a][path[k]].traversed=true;
  99.                     //cout<<endl<<path[k-1];
  100.                     k++;
  101.                     if(templength!=32767)
  102.                         length[i]+=templength;
  103.  
  104.                 }
  105.                // cout<<endl<<length[i];
  106.             }
  107.  
  108.     /*****Least Distance Calculation *****/
  109.  
  110.             for(int i=0;i<n-1;i++)
  111.                 for(int j=i+1;j<n;j++)
  112.                     if(length[i]>length[j])
  113.                      {
  114.                            float temp=length[i];
  115.                            length[i]=length[j];
  116.                            length[j]=temp;
  117.                       }
  118.             leastLength=length[0];
  119.  
  120. /*****Detection of Path with Least Distance*****/
  121.  
  122.             for(int i=0;i<n;i++)
  123.                 length[i]=0;
  124.             for(int i=0;i<n;i++)
  125.             {
  126.                 for(int l=0;l<n;l++)
  127.                     for(int m=0;m<n;m++)
  128.                     {
  129.                         path[l]=-1;
  130.                         adjMat[l][m].traversed=false;
  131.                     }
  132.  
  133.                for(int d=0;d<n;d++)
  134.                     adjMat[d][i].traversed=true;
  135.  
  136.                 path[0]=i;
  137.                 int k=1;
  138.                 int count=0;
  139.                 for(int j=path[0];count<n;j=path[k-1],count++)
  140.                 {
  141.  
  142.                     float templength=32767;
  143.                     for(int c=0;c<n;c++)
  144.                     {
  145.                         if((adjMat[j][c].traversed==false)&&(adjMat[j][c].distance<templength)&&(adjMat[j][c].distance!=0))
  146.                         {
  147.                             templength=adjMat[j][c].distance;
  148.                             path[k]=c;
  149.                         }
  150.                     }
  151.                     for(int a=0;a<n;a++)
  152.                             adjMat[a][path[k]].traversed=true;
  153.                     k++;
  154.                     if(templength!=32767)
  155.                         length[i]+=templength;
  156.  
  157.                 }
  158.                 if(length[i]==leastLength)
  159.                     {
  160.                         for(int y=0;y<n;y++)
  161.                             correctPath[y]=path[y];
  162.                         goto terminate;
  163.                     }
  164.             }
  165.             terminate:
  166.             int dummy;
  167.         }
  168.  
  169.     //Distance Calculator
  170.     float distance_calc(int x1,int y1,int x2,int y2)
  171.     {
  172.         float distance;
  173.         float temp;
  174.         temp=((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2));
  175.         distance=sqrt(temp);
  176.         return distance;
  177.     }
  178.  
  179.  
  180.     void output()
  181.     {
  182.         /*for(int i=0;i<n;i++)
  183.             cout<<"\n=>"<<correctPath[i]; */
  184.         for(int i=0;i<n-1;i++)
  185.         {
  186.  
  187.             cout<<"\nTUNNEL FROM ("<<colonies[correctPath[i]].x<<","<<colonies[correctPath[i]].y<<") TO ("<<colonies[correctPath[i+1]].x<<","<<colonies[correctPath[i+1]].y<<") is ";
  188.             cout.precision(2);
  189.             cout.setf(ios::fixed,ios::floatfield);
  190.             cout<<distance_calc(colonies[correctPath[i]].x,colonies[correctPath[i]].y,colonies[correctPath[i+1]].x,colonies[correctPath[i+1]].y)<<" m";
  191.         }
  192.         cout<<"\nTOTAL LENGTH OF TUNNEL is "<<leastLength<<" m";
  193.  
  194.     }
  195.  
  196.     colonysets(){}
  197.  
  198.  
  199.  
  200. };
  201.  
  202.  
  203.  
  204. int main()
  205. {
  206.     colonysets colonyset[500]; //Assuming Number of Data Sets Would be <500. Hope so!
  207.  
  208.     int setcount=0;
  209.     int n;
  210.     cin>>n;
  211.     while(n!=0)
  212.     {
  213.         colonyset[setcount]=colonysets(n);
  214.         setcount++;
  215.         cin>>n;
  216.     }
  217.     cout<<"OUTPUT IS";
  218.     for(int i=0;i<setcount;i++)
  219.     {
  220.         cout<<"\nSET "<<i+1;
  221.         colonyset[i].output();
  222.     }
  223.    // system("PAUSE");                   USE IF YOU NEED TO LOOK AT OUTPUT. Equivalent to getch().
  224.     return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement