Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- // stfu intelsense, you're wrong
- typedef char bool;
- #define false 0
- #define true 1
- int CalcArea(bool* pInputArray, int x, int y, int* endx, int* endy, int MaxWidth, int MaxHeight)
- {
- int startx = x;
- while(x < MaxWidth && pInputArray[x + y * MaxWidth])
- x++;
- *endy = 0;
- *endx = x;
- return (x - startx) * (y + 1);
- }
- int main()
- {
- bool* pInputArray;
- int Buildings, Altitude, i;
- printf("Buildings: ");
- scanf("%i", &Buildings);
- printf("Altitude: ");
- scanf("%i", &Altitude);
- Altitude++;
- pInputArray = (bool*)malloc(Buildings*Altitude);
- // Construct the array
- for(i = 0; i < Buildings*Altitude; i++)
- pInputArray[i] = false;
- for(i = 0; i < Buildings; i++)
- {
- int height = 0;
- printf("Building %i's height: ", i);
- scanf("%i", &height);
- if(height >= Altitude)
- {
- i--;
- printf("Too high, enter again: ");
- continue;
- }
- while(height >= 0)
- {
- pInputArray[i + height * Buildings] = true;
- height--;
- }
- }
- // Ok, the array should be constructed, lets just visualize it
- {
- int x, y;
- for(y = Altitude - 1; y >= 0; y--)
- {
- for(x = 0; x < Buildings; x++)
- printf("%s", pInputArray[x + y * Buildings] ? "|" : " ");
- printf("\n");
- }
- }
- // Now it's time to find the largest area!
- {
- int x, y, endx, endy;
- int bestx = 0, besty = 0;
- int area = 0, bestarea = 0;
- // Brute force it :V
- for(y = 0; y < Altitude; y++)
- for(x = 0; x < Buildings; x++)
- {
- area = CalcArea(pInputArray, x, y, &endx, &endy, Buildings, Altitude);
- if(area > bestarea)
- {
- bestx = x;
- besty = y;
- bestarea = area;
- }
- }
- // The array is generated with the buildings decending (0,0) top left, we need to fix the output...
- besty = Altitude - besty - 1;
- printf("\nThe best area was found at %i, %i with an area of %i\n", bestx, besty, bestarea);
- }
- free(pInputArray);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement