2.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. typedef struct {
  6. uint8_t *data;
  7. int rows;
  8. int columns;
  9. int cap;
  10. } Mat;
  11. char buf[4096];
  12. int get(Mat *m, int row, int column) {
  13. return m->data[row * m->columns + column];
  14. }
  15. int score(Mat *m, int i, int j) {
  16. int height = get(m, i, j);
  17. int s = 1;
  18. int count = 0;
  19. for (int p = i - 1; p >= 0; p--) {
  20. count++;
  21. if (get(m, p, j) >= height) break;
  22. }
  23. s *= count;
  24. count = 0;
  25. for (int p = i + 1; p < m->rows; p++) {
  26. count++;
  27. if (get(m, p, j) >= height) break;
  28. }
  29. s *= count;
  30. count = 0;
  31. for (int p = j - 1; p >= 0; p--) {
  32. count++;
  33. if (get(m, i, p) >= height) break;
  34. }
  35. s *= count;
  36. count = 0;
  37. for (int p = j + 1; p < m->columns; p++) {
  38. count++;
  39. if (get(m, i, p) >= height) break;
  40. }
  41. s *= count;
  42. return s;
  43. }
  44. int main() {
  45. Mat m;
  46. m.data = malloc(4096);
  47. m.cap = 4096;
  48. m.rows = 0;
  49. m.columns = 0;
  50. FILE *fp = fopen("input", "r");
  51. while(fgets(buf, 4096, fp)) {
  52. int len = strlen(buf);
  53. if (len <= 1) break;
  54. if (m.columns == 0) m.columns = len - 1;
  55. while (m.cap < (m.rows + 1) * m.columns) {
  56. m.data = realloc(m.data, m.cap * 2);
  57. m.cap *= 2;
  58. }
  59. memcpy(m.data + m.rows * m.columns, buf, m.columns);
  60. m.rows++;
  61. }
  62. int maxscore = -1;
  63. for (int i = 0; i < m.rows; i++) {
  64. for (int j = 0; j < m.columns; j++) {
  65. int s = score(&m, i, j);
  66. if (s > maxscore) maxscore = s;
  67. }
  68. }
  69. printf("%d", maxscore);
  70. return 0;
  71. }