Language: C

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>

#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
#define SWAP(type, a, b) { const type tmp = a; a = b; b = tmp; }

int compare_sz_asc(const void* a, const void* b) {
    return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}

int compare_sz_desc(const void* a, const void* b) {
    return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}

int compare_i64_asc(const void* a, const void* b) {
    return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}

int compare_i64_desc(const void* a, const void* b) {
    return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}

int compare_u64_asc(const void* a, const void* b) {
    return *((uint64_t*)a) < *((uint64_t*)b) ? -1 : 1;
}

int compare_u64_desc(const void* a, const void* b) {
    return *((uint64_t*)a) > * ((uint64_t*)b) ? -1 : 1;
}

int compare_c_asc(const void* a, const void* b) {
    return *((char*)a) < *((char*)b) ? -1 : 1;
}

int compare_c_desc(const void* a, const void* b) {
    return *((char*)a) > * ((char*)b) ? -1 : 1;
}

int compare_dp_asc(const void* a, const void* b) {
    return *((double*)a) < *((double*)b) ? -1 : 1;
}

static uint64_t modinvU64(const uint64_t a, const uint64_t m) {
    int64_t aa = (int64_t)a;
    const int64_t mm = (int64_t)m;
    int64_t b = mm;
    int64_t u = 1;
    int64_t v = 0;

    while (b != 0) {
        const int64_t t = aa / b;

        aa -= t * b;
        SWAP(uint64_t, aa, b);
        u -= t * v;
        SWAP(uint64_t, u, v);
    }

    u %= mm;
    if (u < 0) {
        u += m;
    }

    return (uint64_t)u;
}

static size_t powSz(const size_t base, const size_t exp) {
    if (exp == 0) {
        return 1;
    }

    if (exp == 1) {
        return base;
    }

    if (exp % 2 == 0) {
        return powSz(base * base, exp / 2);
    }
    else {
        return base * powSz(base, exp - 1);
    }
}

static uint64_t powU64(const uint64_t base, const uint64_t exp) {
    if (exp == 0) {
        return 1;
    }

    if (exp == 1) {
        return base;
    }

    if (exp % 2 == 0) {
        return powU64(base * base, exp / 2);
    }
    else {
        return base * powU64(base, exp - 1);
    }
}

static uint64_t powU64WMod(const uint64_t base, const uint64_t exp, const uint64_t m) {
    if (exp == 0) {
        return 1;
    }

    if (exp == 1) {
        return base % m;
    }

    if (exp % 2 == 0) {
        return powU64WMod(base * base % m, exp / 2, m);
    }
    else {
        return base * powU64WMod(base, exp - 1, m) % m;
    }
}

static size_t comb(const size_t n, const size_t r) {
    size_t result = 1;

    for (size_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t combU64(const uint64_t n, const uint64_t r) {
    uint64_t result = 1;

    for (uint64_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t combU64WMod(const uint64_t n, const uint64_t r, const uint64_t m) {
    uint64_t result = 1;

    for (uint64_t i = 0; i < r; i++) {
        result = result * (n - i) % m;
        result = result * modinvU64(i + 1, m) % m;
    }

    return result;
}

static uint64_t permU64WMod(const uint64_t n, const uint64_t m) {
    uint64_t result = 1;

    for (uint64_t v = 2; v <= n; v++) {
        result *= v;
        result %= m;
    }

    return result;
}

static size_t gcdZu(size_t m, size_t n) {
    size_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

static uint64_t gcdU64(uint64_t m, uint64_t n)
{
    uint64_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

typedef struct {
    uint64_t val;
}BHeap_Val;

#define BHEAP_VAL_TYPE BHeap_Val

#define BHEAP_INVALID_NODE ((size_t)100000000)

typedef struct {
    BHEAP_VAL_TYPE val;
} BHeap_Node;


struct _BHeap_Ctx {
    size_t size;
    int (*compare)(const BHEAP_VAL_TYPE* parent, const BHEAP_VAL_TYPE* node);
    void(*notifyNodeChange)(const struct _BHeap_Ctx* const ctx, const size_t nodeListSize, const size_t* const nodeList);
    BHeap_Node* node;
};

typedef struct _BHeap_Ctx BHeap_Ctx;

static void bHeap_Init(BHeap_Ctx* const ctx, int(*compare)(const BHEAP_VAL_TYPE* parent, const BHEAP_VAL_TYPE* node), void(*notifyNodeChange)(const BHeap_Ctx* const ctx, const size_t nodeListSize, const size_t* const nodeList), BHeap_Node* const nodes) {
    ctx->size = 0;
    ctx->compare = compare;
    ctx->notifyNodeChange = notifyNodeChange;
    ctx->node = nodes;
}

static void bHeap_Swap(BHeap_Ctx* const ctx, const size_t node0, const size_t node1) {
    const BHEAP_VAL_TYPE tmp = ctx->node[node0].val;
    ctx->node[node0].val = ctx->node[node1].val;
    ctx->node[node1].val = tmp;

    if (ctx->notifyNodeChange != NULL) {
        const size_t nodeList[2] = { node0,node1 };
        ctx->notifyNodeChange(ctx, 2, nodeList);
    }
}

static void bHeap_UpHeap(BHeap_Ctx* const ctx, const size_t nodeIdx) {
    if (nodeIdx == 0) {
        return;
    }

    const size_t parentIdx = (nodeIdx + 1) / 2 - 1;

    const int compareResult = ctx->compare(&(ctx->node[parentIdx].val), &(ctx->node[nodeIdx].val));

    if (0 < compareResult) {
        bHeap_Swap(ctx, nodeIdx, parentIdx);

        if (parentIdx != 0) {
            bHeap_UpHeap(ctx, parentIdx);
        }
    }
}

static void bHeap_DownHeap(BHeap_Ctx* const ctx, const size_t nodeIdx) {
    if (ctx->size - 1 <= nodeIdx) {
        return;
    }

    const size_t leftIdx = 2 * (nodeIdx + 1) - 1;

    if (leftIdx < ctx->size) {
        const int leftResult = ctx->compare(&(ctx->node[nodeIdx].val), &(ctx->node[leftIdx].val));

        size_t swapTarget = leftResult < 0 ? BHEAP_INVALID_NODE : leftIdx;

        const size_t rightIdx = leftIdx + 1;

        if (rightIdx < ctx->size) {
            const int rightResult = ctx->compare(&(ctx->node[nodeIdx].val), &(ctx->node[rightIdx].val));

            if (swapTarget == BHEAP_INVALID_NODE) {
                if (0 < rightResult) {
                    swapTarget = rightIdx;
                }
            }
            else {
                if (0 < rightResult) {
                    const int rightLeftResult = ctx->compare(&(ctx->node[leftIdx].val), &(ctx->node[rightIdx].val));

                    if (0 < rightLeftResult) {
                        swapTarget = rightIdx;
                    }
                }
            }
        }

        if (swapTarget != BHEAP_INVALID_NODE) {
            bHeap_Swap(ctx, nodeIdx, swapTarget);

            bHeap_DownHeap(ctx, swapTarget);
        }
    }
}

static void bHeap_Add(BHeap_Ctx* const ctx, const BHEAP_VAL_TYPE val) {
    ctx->node[ctx->size].val = val;

    ctx->size++;

    if (ctx->notifyNodeChange != NULL) {
        const size_t node = ctx->size - 1;
        ctx->notifyNodeChange(ctx, 1, &node);
    }

    bHeap_UpHeap(ctx, ctx->size - 1);
}

static void bHeap_RemoveTop(BHeap_Ctx* const ctx) {
    if (ctx->size == 0) {
        return;
    }

    if (ctx->size == 1) {
        ctx->size--;
        return;
    }

    ctx->node[0].val = ctx->node[ctx->size - 1].val;

    if (ctx->notifyNodeChange != NULL) {
        const size_t node = 0;
        ctx->notifyNodeChange(ctx, 1, &node);
    }

    ctx->size--;

    bHeap_DownHeap(ctx, 0);
}

static void bHeap_RemoveNode(BHeap_Ctx* const ctx, const size_t node) {
    if (node == ctx->size - 1) {
        ctx->size--;
    }
    else {
        ctx->node[node].val = ctx->node[ctx->size - 1].val;

        ctx->size--;

        if (ctx->notifyNodeChange != NULL) {
            ctx->notifyNodeChange(ctx, 1, &node);
        }

        bHeap_DownHeap(ctx, node);
        bHeap_UpHeap(ctx, node);
    }
}

static BHEAP_VAL_TYPE* bHeap_GetTopVal(const BHeap_Ctx* const ctx) {
    return ctx->size == 0 ? NULL : &(ctx->node[0].val);
}

typedef struct {
    uint64_t cnt;
    size_t* child;
    size_t numChild;
    size_t maxNumChild;
} Tree_Node;

#define TREE_INIT_ALLOC (128)

static Tree_Node tree_Node[200000];

static void tree_Init(const uint64_t N) {
    for (uint64_t i = 0; i < N; i++) {
        tree_Node[i].cnt = 0;
        tree_Node[i].numChild = 0;
        tree_Node[i].child = malloc(sizeof(size_t) * TREE_INIT_ALLOC);
        tree_Node[i].maxNumChild = TREE_INIT_ALLOC;
    }
}

static void tree_AddChild(const size_t parent, const size_t child) {
    if (tree_Node[parent].maxNumChild <= tree_Node[parent].numChild) {
        tree_Node[parent].child = realloc(tree_Node[parent].child, sizeof(size_t) * tree_Node[parent].maxNumChild * 2);
        tree_Node[parent].maxNumChild *= 2;
    }

    tree_Node[parent].child[tree_Node[parent].numChild] = child;
    tree_Node[parent].numChild++;
}

static BHeap_Node BN_Asc[200000];
static BHeap_Node BN_Desc[200000];

static int _bHeap_Compare_Asc(const BHEAP_VAL_TYPE* parent, const BHEAP_VAL_TYPE* node) {
    return parent->val <= node->val ? -1 : 1;
}

static int _bHeap_Compare_Desc(const BHEAP_VAL_TYPE* parent, const BHEAP_VAL_TYPE* node) {
    return parent->val >= node->val ? -1 : 1;
}

static size_t PBHeap_Asc_Idx[200000];
static size_t PBHeap_Desc_Idx[200000];

static void _bHeap_NotifyNodeChange_Asc(const BHeap_Ctx* const ctx, const size_t nodeListSize, const size_t* const nodeList) {
    for (size_t i = 0; i < nodeListSize; i++) {
        PBHeap_Asc_Idx[ctx->node[nodeList[i]].val.val] = nodeList[i];
    }
}

static void _bHeap_NotifyNodeChange_Desc(const BHeap_Ctx* const ctx, const size_t nodeListSize, const size_t* const nodeList) {
    for (size_t i = 0; i < nodeListSize; i++) {
        PBHeap_Desc_Idx[ctx->node[nodeList[i]].val.val] = nodeList[i];
    }
}

#define MOD_VAL (UINT64_C(1000000007))

#define UF_ROOT ((size_t)10000000)

typedef struct {
    size_t parent;
    uint64_t w;
    uint64_t v;
    uint64_t wSum;
    uint64_t vSum;
} UF_Node;

static UF_Node uf_Node[101];

typedef struct {
    char status;
    uint64_t pY;
    uint64_t pX;
} CellInfo;

static CellInfo ci[40][40];

static void uf_GetRoot(const uint64_t y, const uint64_t x, uint64_t* const rY, uint64_t* const rX) {
    if (ci[y][x].pY == UF_ROOT) {
        *rY = y;
        *rX = x;

        return;
    }

    uf_GetRoot(ci[y][x].pY, ci[y][x].pX, rY, rX);

    ci[y][x].pY = *rY;
    ci[y][x].pX = *rX;
}

static void uf_Merge(const uint64_t aY, const uint64_t aX, const uint64_t bY, const uint64_t bX) {
    uint64_t aRootY, aRootX;
    uf_GetRoot(aY, aX, &aRootY, &aRootX);
    uint64_t bRootY, bRootX;
    uf_GetRoot(bY, bX, &bRootY, &bRootX);

    if (aRootY != bRootY || aRootX!=bRootX) {
        ci[aRootY][aRootX].pY = bRootY;
        ci[aRootY][aRootX].pX = bRootX;
    }
}

static void setD(const uint64_t y, const uint64_t x, const uint64_t N) {
    if (ci[y][x].status == CHAR_MAX) {
        if (ci[y][(x + N - 1) % N].status != CHAR_MAX && ci[y][(x + N - 1) % N].status != 'B') {
            ci[y][x].status = 'L';
        }
        else if (ci[(y + N - 1) % N][x].status != CHAR_MAX && ci[(y + N - 1) % N][x].status != 'B') {
            ci[y][x].status = 'U';
        }
        else if (ci[y][(x + 1) % N].status != CHAR_MAX && ci[y][(x + 1) % N].status != 'B') {
            ci[y][x].status = 'R';
        }
        else if (ci[(y + 1) % N][x].status != CHAR_MAX && ci[(y + 1) % N][x].status != 'B') {
            ci[y][x].status = 'D';
        }
    }

    if (ci[y][(x + N - 1) % N].status == CHAR_MAX) {
        setD(y, (x + N - 1) % N, N);
    }
    if (ci[(y + N - 1) % N][x].status == CHAR_MAX) {
        setD((y + N - 1) % N, x, N);
    }
    if (ci[y][(x + 1) % N].status == CHAR_MAX) {
        setD(y, (x + 1) % N, N);
    }
    if (ci[(y + 1) % N][x].status == CHAR_MAX) {
        setD((y + 1) % N, x, N);
    }
}


int main(void) {
    uint64_t N, M, B;
    uint64_t gy, gx;
    size_t rpY[100], rpX[100];
    char rd[100];

    scanf("%"PRIu64"%"PRIu64"%"PRIu64"%"PRIu64"%"PRIu64, &N, &M, &B, &gy, &gx);

    gy--; gx--;

    for (uint64_t y = 0; y < N; y++) {
        for (uint64_t x = 0; x < N; x++) {
            ci[y][x].status = CHAR_MAX;
            ci[y][x].pX = UF_ROOT;
            ci[y][x].pY = UF_ROOT;
        }
    }

    ci[gy][gx].status = 'G';

    for (uint64_t i = 0; i < M; i++) {
        scanf("%zu %zu %c\n", &rpY[i], &rpX[i], &rd[i]);
    }
    for (uint64_t i = 0; i < B; i++) {
        size_t by, bx;
        scanf("%zu %zu", &by, &bx);
        by--; bx--;
        ci[by][bx].status = 'B';
    }

    for (uint64_t y = 0; y < N; y++) {
        for (uint64_t x = 0; x < N; x++) {
            if (ci[y][x].status != 'B') {
                if (ci[y][(x + 1) % N].status != 'B') {
                    uf_Merge(y, x, y, (x + 1) % N);
                }
                if (ci[(y + 1) % N][x].status != 'B') {
                    uf_Merge(y, x, (y + 1) % N, x);
                }
            }
        }
    }

    setD(gy, gx, N);

    uint64_t cnt = 0;

    for (uint64_t y = 0; y < N; y++) {
        for (uint64_t x = 0; x < N; x++) {
            if (ci[y][x].status == 'L' || ci[y][x].status == 'U' || ci[y][x].status == 'R' || ci[y][x].status == 'D') {
                cnt++;
            }
        }
    }

    printf("%"PRIu64"\n", cnt);
    for (uint64_t y = 0; y < N; y++) {
        for (uint64_t x = 0; x < N; x++) {
            if (ci[y][x].status == 'L' || ci[y][x].status == 'U' || ci[y][x].status == 'R' || ci[y][x].status == 'D') {
                printf("%"PRIu64" %"PRIu64" %c\n", y + 1, x + 1, ci[y][x].status);
            }
        }
    }

    return 0;
}


Compiler output:
main.c: In function 'setD':
main.c:462:28: error: 'CHAR_MAX' undeclared (first use in this function)
  462 |     if (ci[y][x].status == CHAR_MAX) {
      |                            ^~~~~~~~
main.c:10:1: note: 'CHAR_MAX' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'
    9 | #include <math.h>
  +++ |+#include <limits.h>
   10 | 
main.c:462:28: note: each undeclared identifier is reported only once for each function it appears in
  462 |     if (ci[y][x].status == CHAR_MAX) {
      |                            ^~~~~~~~
main.c: In function 'main':
main.c:504:31: error: 'CHAR_MAX' undeclared (first use in this function)
  504 |             ci[y][x].status = CHAR_MAX;
      |                               ^~~~~~~~
main.c:504:31: note: 'CHAR_MAX' is defined in header '<limits.h>'; this is probably fixable by adding '#include <limits.h>'