Advertisement
C0BRA

Victors solution thing

May 13th, 2012
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // stfu intelsense, you're wrong
  5. typedef char bool;
  6. #define false 0
  7. #define true 1
  8.  
  9. int CalcArea(bool* pInputArray, int x, int y, int* endx, int* endy, int MaxWidth, int MaxHeight)
  10. {
  11.     int startx = x;
  12.  
  13.     while(x < MaxWidth && pInputArray[x + y * MaxWidth])
  14.         x++;
  15.  
  16.     *endy = 0;
  17.     *endx = x;
  18.  
  19.     return (x - startx) * (y + 1);
  20. }
  21.  
  22. int main()
  23. {
  24.     bool* pInputArray;
  25.     int Buildings, Altitude, i;
  26.  
  27.     printf("Buildings: ");
  28.     scanf("%i", &Buildings);
  29.     printf("Altitude: ");
  30.     scanf("%i", &Altitude);
  31.     Altitude++;
  32.  
  33.     pInputArray = (bool*)malloc(Buildings*Altitude);
  34.    
  35.     // Construct the array
  36.  
  37.     for(i = 0; i < Buildings*Altitude; i++)
  38.         pInputArray[i] = false;
  39.  
  40.     for(i = 0; i < Buildings; i++)
  41.     {
  42.          int height = 0;
  43.          printf("Building %i's height: ", i);
  44.          scanf("%i", &height);
  45.  
  46.          if(height >= Altitude)
  47.          {
  48.              i--;
  49.              printf("Too high, enter again: ");
  50.              continue;
  51.          }
  52.  
  53.          while(height >= 0)
  54.          {
  55.              pInputArray[i + height * Buildings] = true;
  56.              height--;
  57.          }
  58.     }
  59.  
  60.     // Ok, the array should be constructed, lets just visualize it
  61.     {
  62.         int x, y;
  63.         for(y = Altitude - 1; y >= 0; y--)
  64.         {
  65.             for(x = 0; x < Buildings; x++)
  66.                 printf("%s", pInputArray[x + y * Buildings] ? "|" : " ");
  67.            
  68.             printf("\n");
  69.         }
  70.     }
  71.  
  72.     // Now it's time to find the largest area!
  73.     {
  74.         int x, y, endx, endy;
  75.         int bestx = 0, besty = 0;
  76.         int area = 0, bestarea = 0;
  77.  
  78.         // Brute force it :V
  79.         for(y = 0; y < Altitude; y++)
  80.             for(x = 0; x < Buildings; x++)
  81.             {
  82.                 area = CalcArea(pInputArray, x, y, &endx, &endy, Buildings, Altitude);
  83.                 if(area > bestarea)
  84.                 {
  85.                     bestx = x;
  86.                     besty = y;
  87.                     bestarea = area;
  88.                 }
  89.             }
  90.  
  91.         // The array is generated with the buildings decending (0,0) top left, we need to fix the output...
  92.         besty = Altitude - besty - 1;
  93.        
  94.         printf("\nThe best area was found at %i, %i with an area of %i\n", bestx, besty, bestarea);
  95.     }
  96.  
  97.     free(pInputArray);
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement