Advertisement
regzarr

greedy

Apr 24th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. /* VERY hardcoded version */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <limits.h>
  6.  
  7. #define LINE_LENGTH 256 // line from input file
  8. #define N 5 // number of cities (matrix size)
  9.  
  10. static int matrix_oriented[N][N] =
  11.   { {-1, 174, 315, 634, 544},
  12.     {174, -1, 152, -1, 595},
  13.     {315, 152, -1, 393, -1},
  14.     {634, 544, 393, -1, -1},
  15.     {544, 595, -1, 388, -1}};
  16.  
  17. void setBit (unsigned char *byte, int pos){
  18.   *byte = *byte | (1 << pos);
  19. }
  20.  
  21. void clearBit (unsigned char *byte, int pos){
  22.   *byte = *byte & (~1 << pos);
  23. }
  24.  
  25. // returns 0 or something other than 0
  26. int getBit (unsigned char byte, int pos){
  27.   return byte & (1 << pos);
  28. }
  29.  
  30. // from @startingPoint to all the other cities without visiting any twice
  31. // returns total cost
  32. unsigned greedy_oriented(int startingPoint){
  33.   int i = startingPoint, j = 0;
  34.   int holdJ, path[N] = { startingPoint }, count = 1;
  35.   int cost = 0;
  36.   unsigned char citiesVisited = 0; // 1 byte: least-significant bit for Timisoara,
  37.   // most-significant bit for Bucuresti. Bit is 0 if city hasn't been visited yet
  38.   setBit(&citiesVisited, startingPoint);
  39.   while (citiesVisited != 0xFF){
  40.     int minimum = INT_MAX;
  41.     while (j < N){
  42.       if (matrix_oriented[i][j] >= 0 && matrix_oriented[i][j] < minimum
  43.       && (getBit(citiesVisited, j) == 0)){
  44.     minimum = matrix_oriented[i][j];
  45.     printf("minimum = %d\n",minimum);
  46.     holdJ = j;
  47.       }
  48.     }
  49.  
  50.     j = 0;
  51.     if (minimum == INT_MAX){
  52.       printf("no solution\n");
  53.       return cost;
  54.     }
  55.    
  56.     cost += minimum;
  57.     path[count] = holdJ;
  58.     count++;
  59.     setBit(&citiesVisited, holdJ);
  60.     i = holdJ;
  61.   }
  62.   return cost;
  63. }
  64.  
  65. /*void read(FILE * inputFile){
  66.   char line[LINE_LENGTH];
  67.   char *temp;
  68.   unsigned i = 0, j = 0;
  69.   fgets(line, LINE_LENGTH, inputFile); // ignoring first line
  70.   fgets(line, LINE_LENGTH, inputFile); // ignoring second line
  71.   fgets(line, LINE_LENGTH, inputFile);
  72.   // parsing data now
  73.   // while (line){
  74.     temp = strtok(line, "|");
  75.     while (temp){
  76.       temp = strtok(NULL, "|");
  77.       sscanf(temp, "%d", &matrix[i][j]);
  78.       j++;
  79.       printf("%d ", matrix[i][j]);
  80.       //printf("%s",temp);
  81.     }
  82.     putchar('\n');
  83.     /*fgets(line, LINE_LENGTH, inputFile);
  84.     if(line == NULL) printf("line NULL\n");
  85.     }*/ /*
  86. }*/
  87.  
  88. int main(int argc, char **argv){
  89.   /*FILE * in = fopen(argv[1], "r");
  90.   if (in == NULL){
  91.     printf("error opening file\nplease specify the file name or its absolute path"
  92.        " as the first input parameter, thanks.\n");
  93.     return -1;
  94.   }
  95.   read(in);
  96.   fclose(in);*/
  97.   greedy_oriented(1);
  98.   return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement