#include #include #include #include struct result_cell { int32_t fst; int32_t snd; }; struct stack_cell { char c; int32_t p; }; struct stack { struct stack_cell stk[10]; int32_t height; }; struct stack init_stack() { struct stack result; result.height = 0; return result; } void stack_push(struct stack* stk, struct stack_cell v) { stk->stk[stk->height] = v; stk->height++; } void stack_pop(struct stack *stk) { stk->height--; } struct stack_cell stack_top(struct stack *stk) { return stk->stk[stk->height - 1]; } bool stack_empty(struct stack *stk) { return stk->height == 0; } void analyze_row( char grid[], bool result_visible[], int32_t result_fwd[], int32_t result_bwd[], int32_t row, int32_t w, int32_t xs, int32_t ys) { struct stack stk = init_stack(); struct stack_cell top; int32_t j = 0; char cur; size_t pos; stack_push(&stk, (struct stack_cell) { '9' + 1, 0}); //sentinel for (; j < w; j++) { pos = row*xs+j*ys; cur = grid[pos]; while (stk.height > 0 && (top = stack_top(&stk)).c < cur) { result_fwd[row*xs+(top.p)*ys] = j - top.p; stack_pop(&stk); } if (stk.height == 1) { result_visible[row*xs+j*ys] = true; } result_bwd[pos] = j - top.p; if (top.c == cur) { result_fwd[row*xs+(top.p)*ys] = j - top.p; stack_pop(&stk); } stack_push(&stk, (struct stack_cell) { cur, j}); } while (stk.height > 1) { top = stack_top(&stk); result_fwd[row*xs+(top.p)*ys] = w - top.p - 1; result_visible[row*xs+(top.p)*ys] = true; stack_pop(&stk); } } void analyze( char grid[], bool result_visible[], int32_t result_fwd[], int32_t result_bwd[], int32_t h, int32_t w, int32_t xs, int32_t ys) { for (int32_t i = 0; i < h; i++) { analyze_row(grid, result_visible, result_fwd, result_bwd, i, w, xs, ys); } } // void analyze_horizontal(char grid[], int32_t result_fwd[], int32_t result_bwd[], int32_t height, int32_t width) // { // analyze(grid, result_fwd, result_bwd, height, width, width, 1); // } // void analyze_vertical(char grid[], int32_t result_fwd[], int32_t result_bwd[], int32_t height, int32_t width) // { // analyze(grid, result_fwd, result_bwd, height, width, 1, height); // } // void print_grid(char grid[], int h, int w) { // for (int i = 0; i < h; i++) { // for (int j = 0; j < w; j++) { // putchar(grid[i*w + j]); // putchar(' '); // } // putchar('\n'); // } // } // void print_result(int grid[], int h, int w) { // for (int i = 0; i < h; i++) { // for (int j = 0; j < w; j++) { // printf("%d ", grid[i*w+j]); // } // putchar('\n'); // } // } // int main() // { // FILE *fp = fopen("./data/08.in", "r"); // // Determine file size // fseek(fp, 0L, SEEK_END); // int fpsize = ftell(fp); // fseek(fp, 0L, SEEK_SET); // rewind(fp); // char *buf = malloc(fpsize); // char *grid = malloc(fpsize); // fread(buf, 1, fpsize, fp); // char *grid_ptr = grid; // for (int i = 0; i < fpsize; i++) { // *grid_ptr = buf[i]; // if (buf[i] != '\n') { // grid_ptr++; // } // } // int H = 5; // int W = 5; // print_grid(grid, H, W); // int* result_fwd = malloc(fpsize * sizeof(int)); // int* result_bwd = malloc(fpsize * sizeof(int)); // analyze_vertical(grid, result_fwd, result_bwd, H, W); // printf("\n D \n"); // print_result(result_fwd, H, W); // printf("\n U \n"); // print_result(result_bwd, H, W); // analyze_horizontal(grid, result_fwd, result_bwd, H, W); // printf("\n R \n"); // print_result(result_fwd, H, W); // printf("\n L \n"); // print_result(result_bwd, H, W); // }