Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define ABS(x) ((x) < 0 ? (-1 * x) : x)
- #define MAX_MATR_SIZE 20000
- typedef char MatrT[MAX_MATR_SIZE * 2 / 4]; // x2 to store one cell as 2 bits
- static MatrT matrixes[4];
- struct cell {
- char *matr;
- int byteNum;
- int bitNum;
- };
- static char *getMatr(int x, int y) {
- if (x < 0 && y < 0)
- return matrixes[0];
- if (x < 0 && y > 0)
- return matrixes[1];
- if (x > 0 && y < 0)
- return matrixes[2];
- return matrixes[3];
- }
- static struct cell getCell(int x, int y) {
- char *m = getMatr(x, y);
- x = ABS(x);
- y = ABS(y);
- // 1 byte contain 4 cells
- // Find cell index
- int cell = x * 4 + y * 2;
- // Find a byte and bif for specific cell
- int byteNum = cell / 8;
- int bitNum = cell - byteNum * 8;
- struct cell c = {
- .matr = m,
- .byteNum = byteNum,
- .bitNum = bitNum,
- };
- return c;
- }
- #define EMPTY 0 // 00
- #define FILLED 1 // 01
- #define WALL 2 // 10
- #define CELL_MASK 3 // 11
- static void setCell(int x, int y, int value) {
- struct cell c = getCell(x, y);
- c.matr[c.byteNum] |= value << c.bitNum;
- }
- static void setWall(int x, int y) {
- setCell(x, y, WALL);
- }
- static void setFilled(int x, int y) {
- setCell(x, y, FILLED);
- }
- static int getValue(int x, int y) {
- struct cell c = getCell(x, y);
- int value = c.matr[c.byteNum] & (CELL_MASK << c.bitNum);
- return value >> c.bitNum;
- }
- int main() {
- struct val {
- int x;
- int y;
- };
- struct val tests[] = {
- { 0, 0 },
- { -10, 0 },
- { 0, -10 },
- { 10, 0 },
- { 0, 10 },
- { 1, 1 },
- { 10, -1 },
- { -10, 23 },
- { -47, -53 },
- { 1, 1 },
- { 2, 2 },
- { 3, 3 },
- { 4, 4 },
- { 5, 5 },
- { 6, 6 },
- { 7, 7 },
- { 8, 8 },
- { 9, 9 },
- { 10, 10 },
- { 11, 11 },
- { 12, 12 },
- { 13, 13 },
- { 14, 14 },
- { 15, 15 },
- { 16, 16 },
- { 17, 17 },
- { 18, 18 },
- { 19, 19 },
- { 20, 20 },
- { 21, 21 },
- { 22, 22 },
- { 23, 23 },
- { 24, 24 },
- { 25, 25 },
- { 26, 26 },
- { 27, 27 },
- { 28, 28 },
- { 29, 29 },
- { 30, 30 },
- { 31, 31 },
- { 32, 32 },
- { 33, 33 },
- { 34, 34 },
- { 35, 35 },
- { 36, 36 },
- { 37, 37 },
- { 38, 38 },
- { 39, 39 },
- { 40, 40 },
- { -41, 41 },
- { -42, 42 },
- { -43, 43 },
- { -44, 44 },
- { -45, 45 },
- { -46, 46 },
- { -47, 47 },
- { -48, 48 },
- { -49, 49 },
- { -50, 50 },
- { -51, 51 },
- { -52, 52 },
- { -53, 53 },
- { -54, 54 },
- { -55, 55 },
- { -56, 56 },
- { -57, 57 },
- { -58, 58 },
- { -59, 59 },
- { -60, 60 },
- { -61, 61 },
- { -62, 62 },
- { -63, 63 },
- { -64, 64 },
- { -65, 65 },
- { -66, 66 },
- { -67, 67 },
- { -68, 68 },
- { -69, 69 },
- { -70, 70 },
- { -71, 71 },
- { -72, 72 },
- { -73, 73 },
- { -74, 74 },
- { -75, 75 },
- { -76, 76 },
- { -77, 77 },
- { -78, 78 },
- { -79, 79 },
- { -80, 80 },
- { -81, 81 },
- { -82, 82 },
- { -83, 83 },
- { -84, 84 },
- { -85, 85 },
- { -86, 86 },
- { -87, 87 },
- { -88, 88 },
- { -89, 89 },
- { -90, 90 },
- { -91, 91 },
- { -92, 92 },
- { -93, 93 },
- { -94, 94 },
- { -95, 95 },
- { -96, 96 },
- { -97, 97 },
- { -98, 98 },
- { -99, 99 },
- { -100, 100 },
- };
- printf("\n\nFILLING\n\n");
- for (int i = 0; i < sizeof(tests) / sizeof(*tests); ++i) {
- if (i % 2) {
- printf("% 4d x % 4d is filled\n", tests[i].x, tests[i].y);
- setFilled(tests[i].x, tests[i].y);
- } else {
- printf("% 4d x % 4d is a wall\n", tests[i].x, tests[i].y);
- setWall(tests[i].x, tests[i].y);
- }
- }
- printf("\n\nTESTING\n\n");
- for (int i = 0; i < sizeof(tests) / sizeof(*tests); ++i) {
- int value = getValue(tests[i].x, tests[i].y);
- const char *type = "UNKNOWN";
- if (value == WALL)
- type = "a wall";
- else if (value == FILLED)
- type = "filled";
- else if (value == EMPTY)
- type = "empty";
- printf("% 4d x % 4d is %s\n", tests[i].x, tests[i].y, type);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement