1.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. int get(Mat *m, int row, int column) {
  12. return m->data[row * m->columns + column];
  13. }
  14. void set(Mat *m, int row, int column, uint8_t value) {
  15. m->data[row * m->columns + column] = value;
  16. }
  17. char buf[4096];
  18. int main() {
  19. Mat m;
  20. m.data = malloc(4096);
  21. m.cap = 4096;
  22. m.rows = 0;
  23. m.columns = 0;
  24. FILE *fp = fopen("input", "r");
  25. while(fgets(buf, 4096, fp)) {
  26. int len = strlen(buf);
  27. if (len <= 1) break;
  28. if (m.columns == 0) m.columns = len - 1;
  29. while (m.cap < (m.rows + 1) * m.columns) {
  30. m.data = realloc(m.data, m.cap * 2);
  31. m.cap *= 2;
  32. }
  33. memcpy(m.data + m.rows * m.columns, buf, m.columns);
  34. m.rows++;
  35. }
  36. Mat vis = m;
  37. vis.data = malloc(m.rows * m.columns);
  38. vis.cap = m.rows * m.columns;
  39. memset(vis.data, 0, vis.cap);
  40. // top-down scan
  41. for (int i = 0; i < m.columns; i++) {
  42. int curmax = -1;
  43. for (int j = 0; j < m.rows; j++) {
  44. if (get(&m, j, i) > curmax) {
  45. set(&vis, j, i, 1);
  46. curmax = get(&m, j, i);
  47. }
  48. }
  49. }
  50. // down-top scan
  51. for (int i = 0; i < m.columns; i++) {
  52. int curmax = -1;
  53. for (int j = m.rows - 1; j >= 0; j--) {
  54. if (get(&m, j, i) > curmax) {
  55. set(&vis, j, i, 1);
  56. curmax = get(&m, j, i);
  57. }
  58. }
  59. }
  60. // left-right scan
  61. for (int i = 0; i < m.rows; i++) {
  62. int curmax = -1;
  63. for (int j = 0; j < m.columns; j++) {
  64. if (get(&m, i, j) > curmax) {
  65. set(&vis, i, j, 1);
  66. curmax = get(&m, i, j);
  67. }
  68. }
  69. }
  70. // right-left scan
  71. for (int i = 0; i < m.rows; i++) {
  72. int curmax = -1;
  73. for (int j = m.columns - 1; j >= 0; j--) {
  74. if (get(&m, i, j) > curmax) {
  75. set(&vis, i, j, 1);
  76. curmax = get(&m, i, j);
  77. }
  78. }
  79. }
  80. // count
  81. int count = 0;
  82. for (int i = 0; i < m.columns; i++) {
  83. int curmax = -1;
  84. for (int j = 0; j < m.rows; j++) {
  85. if (get(&vis, j, i) > 0) {
  86. count++;
  87. }
  88. }
  89. }
  90. printf("%d", count);
  91. return 0;
  92. }