A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
This is a complex problem that can be solved using dynamic programming and bitmasking. Here's a possible solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK ((1 << N) - 1) #define NUM_PATTERNS (1 << (N * N)) uint64_t count_unholy(int r, int ones, int left, int up_left, int up, int dp[][2][MASK + 1]) { if (r == N) { return !ones; } if (dp[r][ones][left] != -1) { return dp[r][ones][left]; } uint64_t total = 0; for (int i = 0; i < 2; i++) { int new_ones = ones + i; int new_left = (left << 1) | i; int new_up_left = up_left; int new_up = up; if (r > 0) { new_up_left = (up_left << 1) | (i >> 1); new_up = (up << 1) | i; } if (i && !(up | up_left | left)) { continue; } total += count_unholy(r + 1, new_ones, new_left, new_up_left, new_up, dp); } dp[r][ones][left] = total; return total; } uint64_t count_unholy_matrices() { uint64_t total = 0; uint64_t dp[N][2][MASK + 1]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < 2; i++) { total += count_unholy(0, i, i, 0, 0, dp); } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("The total number of unholy 8x8 matrices is: %llu\n", count); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK ((1 << N) - 1) #define NUM_PATTERNS (1 << (N * N)) uint64_t count_unholy(int r, int ones, int left, int up_left, int up, int dp[][2][MASK + 1]) { if (r == N) { return !ones; } if (dp[r][ones][left] != -1) { return dp[r][ones][left]; } uint64_t total = 0; for (int i = 0; i < 2; i++) { int new_ones = ones + i; int new_left = (left << 1) | i; int new_up_left = up_left; int new_up = up; if (r > 0) { new_up_left = (up_left << 1) | (i >> 1); new_up = (up << 1) | i; } if (i && !(up | up_left | left)) { continue; } total += count_unholy(r + 1, new_ones, new_left, new_up_left, new_up, dp); } dp[r][ones][left] = total; return total; } uint64_t count_unholy_matrices() { uint64_t total = 0; uint64_t dp[N][2][MASK + 1]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < 2; i++) { total += count_unholy(0, i, i, 0, 0, dp); } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("The total number of unholy 8x8 matrices is: %llu\n", count); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK ((1 << N) - 1) #define NUM_PATTERNS (1 << (N * N)) uint64_t count_unholy(int r, int ones, int left, int up_left, int up, int dp[][2][MASK + 1]) { if (r == N) { return !ones; } if (dp[r][ones][left] != -1) { return dp[r][ones][left]; } uint64_t total = 0; for (int i = 0; i < 2; i++) { int new_ones = ones + i; int new_left = (left << 1) | i; int new_up_left = up_left; int new_up = up; if (r > 0) { new_up_left = (up_left << 1) | (i >> 1); new_up = (up << 1) | i; } if (i && !(up | up_left | left)) { continue; } total += count_unholy(r + 1, new_ones, new_left, new_up_left, new_up, dp); } dp[r][ones][left] = total; return total; } uint64_t count_unholy_matrices() { uint64_t total = 0; uint64_t dp[N][2][MASK + 1]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < 2; i++) { total += count_unholy(0, i, i, 0, 0, dp); } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("The total number of unholy 8x8 matrices is: %llu\n", count); return 0; } |
The total number of unholy 8x8 matrices is: 1
main.c: In function 'count_unholy_matrices':
main.c:38:5: warning: implicit declaration of function 'memset' [-Wimplicit-function-declaration]
38 | memset(dp, -1, sizeof(dp));
| ^~~~~~
main.c:4:1: note: include '<string.h>' or provide a declaration of 'memset'
3 | #include <stdint.h>
+++ |+#include <string.h>
4 |
main.c:38:5: warning: incompatible implicit declaration of built-in function 'memset' [-Wbuiltin-declaration-mismatch]
38 | memset(dp, -1, sizeof(dp));
| ^~~~~~
main.c:38:5: note: include '<string.h>' or provide a declaration of 'memset'
main.c:40:46: warning: passing argument 6 of 'count_unholy' from incompatible pointer type [-Wincompatible-pointer-types]
40 | total += count_unholy(0, i, i, 0, 0, dp);
| ^~
| |
| uint64_t (*)[2][256] {aka long unsigned int (*)[2][256]}
main.c:9:75: note: expected 'int (*)[2][256]' but argument is of type 'uint64_t (*)[2][256]' {aka 'long unsigned int (*)[2][256]'}
9 | uint64_t count_unholy(int r, int ones, int left, int up_left, int up, int dp[][2][MASK + 1]) {
| ~~~~^~~~~~~~~~~~~~~~~
main.c: In function 'main':
main.c:47:60: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
47 | printf("The total number of unholy 8x8 matrices is: %llu\n", count);
| ~~~^ ~~~~~
| | |
| | uint64_t {aka long unsigned int}
| long long unsigned int
| %lu
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
This is a complex problem that involves combinatorics and bit manipulation. Here's a high-level approach to solve it: 1. Generate all possible 8x8 binary matrices. There are 2^(8*8) = 2^64 of them. This is too large to enumerate in a reasonable amount of time, so we need to generate only the unholy matrices. 2. Define a function that checks if a matrix is unholy. This function can be implemented using bitwise operations to check if there are any isolated 1s in the matrix. 3. To generate only the unholy matrices, we can use a recursive function that generates all possible binary strings of length 8, and then combines them to form an 8x8 matrix. At each step, we check if the current string can be extended to an unholy matrix by checking if there are any isolated 1s in the remaining columns. 4. Count the number of unholy matrices generated by the recursive function. Here's some sample code that implements this approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK(i) (1ULL << (i)) // Check if a matrix has any isolated 1s int is_unholy(uint64_t matrix[N]) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; for (int j = 0; j < N; j++) { if (row & MASK(j)) { if (!((row & MASK(j-1)) || (row & MASK(j+1)) || (matrix[i-1] & MASK(j)) || (matrix[i+1] & MASK(j)))) { return 0; } } } } return 1; } // Recursive function to generate unholy matrices int generate_unholy(uint64_t matrix[N], int col) { if (col == N) { // Check if the matrix is unholy if (is_unholy(matrix)) { // Increment the count of unholy matrices count++; } return 0; } // Try all possible values for the current column for (int bit = 0; bit < 2; bit++) { // Set the current column matrix[col] = bit; // Check if the current column can be extended to an unholy matrix if (!has_isolated_ones(matrix, col)) { // Recurse on the remaining columns generate_unholy(matrix, col+1); } } return 0; } // Check if the current column has any isolated 1s int has_isolated_ones(uint64_t matrix[N], int col) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; if (row & MASK(col)) { if (!((row & MASK(col-1)) || (row & MASK(col+1)) || (matrix[i-1] & MASK(col)) || (matrix[i+1] & MASK(col)))) { return 1; } } } return 0; } int main() { uint64_t matrix[N] = {0}; int count = 0; // Generate all unholy matrices generate_unholy(matrix, 0); // Print the count of unholy matrices printf("Number of unholy matrices: %d\n", count); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK(i) (1ULL << (i)) // Check if a matrix has any isolated 1s int is_unholy(uint64_t matrix[N]) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; for (int j = 0; j < N; j++) { if (row & MASK(j)) { if (!((row & MASK(j-1)) || (row & MASK(j+1)) || (matrix[i-1] & MASK(j)) || (matrix[i+1] & MASK(j)))) { return 0; } } } } return 1; } // Recursive function to generate unholy matrices int generate_unholy(uint64_t matrix[N], int col) { if (col == N) { // Check if the matrix is unholy if (is_unholy(matrix)) { // Increment the count of unholy matrices count++; } return 0; } // Try all possible values for the current column for (int bit = 0; bit < 2; bit++) { // Set the current column matrix[col] = bit; // Check if the current column can be extended to an unholy matrix if (!has_isolated_ones(matrix, col)) { // Recurse on the remaining columns generate_unholy(matrix, col+1); } } return 0; } // Check if the current column has any isolated 1s int has_isolated_ones(uint64_t matrix[N], int col) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; if (row & MASK(col)) { if (!((row & MASK(col-1)) || (row & MASK(col+1)) || (matrix[i-1] & MASK(col)) || (matrix[i+1] & MASK(col)))) { return 1; } } } return 0; } int main() { uint64_t matrix[N] = {0}; int count = 0; // Generate all unholy matrices generate_unholy(matrix, 0); // Print the count of unholy matrices printf("Number of unholy matrices: %d\n", count); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define N 8 #define MASK(i) (1ULL << (i)) // Check if a matrix has any isolated 1s int is_unholy(uint64_t matrix[N]) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; for (int j = 0; j < N; j++) { if (row & MASK(j)) { if (!((row & MASK(j-1)) || (row & MASK(j+1)) || (matrix[i-1] & MASK(j)) || (matrix[i+1] & MASK(j)))) { return 0; } } } } return 1; } // Recursive function to generate unholy matrices int generate_unholy(uint64_t matrix[N], int col) { if (col == N) { // Check if the matrix is unholy if (is_unholy(matrix)) { // Increment the count of unholy matrices count++; } return 0; } // Try all possible values for the current column for (int bit = 0; bit < 2; bit++) { // Set the current column matrix[col] = bit; // Check if the current column can be extended to an unholy matrix if (!has_isolated_ones(matrix, col)) { // Recurse on the remaining columns generate_unholy(matrix, col+1); } } return 0; } // Check if the current column has any isolated 1s int has_isolated_ones(uint64_t matrix[N], int col) { for (int i = 0; i < N; i++) { uint64_t row = matrix[i]; if (row & MASK(col)) { if (!((row & MASK(col-1)) || (row & MASK(col+1)) || (matrix[i-1] & MASK(col)) || (matrix[i+1] & MASK(col)))) { return 1; } } } return 0; } int main() { uint64_t matrix[N] = {0}; int count = 0; // Generate all unholy matrices generate_unholy(matrix, 0); // Print the count of unholy matrices printf("Number of unholy matrices: %d\n", count); return 0; } |
main.c: In function 'generate_unholy':
main.c:32:13: error: 'count' undeclared (first use in this function)
32 | count++;
| ^~~~~
main.c:32:13: note: each undeclared identifier is reported only once for each function it appears in
main.c:43:14: warning: implicit declaration of function 'has_isolated_ones' [-Wimplicit-function-declaration]
43 | if (!has_isolated_ones(matrix, col)) {
| ^~~~~~~~~~~~~~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
This is a complex problem that involves generating all possible 8x8 binary matrices, checking if they are 'unholy', and counting the ones that are. Here's a possible solution in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define N (1 << (SIZE * SIZE)) uint64_t count_unholy(void) { uint64_t count = 0; uint64_t matrix; for (matrix = 0; matrix < N; matrix++) { int i, j, k, l; int found_isolated = 0; for (i = 0; i < SIZE && !found_isolated; i++) { for (j = 0; j < SIZE && !found_isolated; j++) { int idx = i * SIZE + j; if (((matrix >> idx) & 1) && (!((matrix >> (idx - SIZE)) & 1) || // above !((matrix >> (idx + SIZE)) & 1) || // below !((matrix >> (idx - 1)) & 1) || // left !((matrix >> (idx + 1)) & 1))) { // right found_isolated = 1; } } } if (!found_isolated) { count++; } } return count; } int main(void) { uint64_t count = count_unholy(); printf("Number of unholy 8x8 matrices: %llu\n", count); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define N (1 << (SIZE * SIZE)) uint64_t count_unholy(void) { uint64_t count = 0; uint64_t matrix; for (matrix = 0; matrix < N; matrix++) { int i, j, k, l; int found_isolated = 0; for (i = 0; i < SIZE && !found_isolated; i++) { for (j = 0; j < SIZE && !found_isolated; j++) { int idx = i * SIZE + j; if (((matrix >> idx) & 1) && (!((matrix >> (idx - SIZE)) & 1) || // above !((matrix >> (idx + SIZE)) & 1) || // below !((matrix >> (idx - 1)) & 1) || // left !((matrix >> (idx + 1)) & 1))) { // right found_isolated = 1; } } } if (!found_isolated) { count++; } } return count; } int main(void) { uint64_t count = count_unholy(); printf("Number of unholy 8x8 matrices: %llu\n", count); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #include <stdio.h> #include <stdint.h> #include <string.h> #define SIZE 8 #define N (1 << (SIZE * SIZE)) uint64_t count_unholy(void) { uint64_t count = 0; uint64_t matrix; for (matrix = 0; matrix < N; matrix++) { int i, j, k, l; int found_isolated = 0; for (i = 0; i < SIZE && !found_isolated; i++) { for (j = 0; j < SIZE && !found_isolated; j++) { int idx = i * SIZE + j; if (((matrix >> idx) & 1) && (!((matrix >> (idx - SIZE)) & 1) || // above !((matrix >> (idx + SIZE)) & 1) || // below !((matrix >> (idx - 1)) & 1) || // left !((matrix >> (idx + 1)) & 1))) { // right found_isolated = 1; } } } if (!found_isolated) { count++; } } return count; } int main(void) { uint64_t count = count_unholy(); printf("Number of unholy 8x8 matrices: %llu\n", count); return 0; } |
Number of unholy 8x8 matrices: 0
main.c: In function 'count_unholy':
main.c:7:14: warning: left shift count >= width of type [-Wshift-count-overflow]
7 | #define N (1 << (SIZE * SIZE))
| ^~
main.c:14:31: note: in expansion of macro 'N'
14 | for (matrix = 0; matrix < N; matrix++) {
| ^
main.c: In function 'main':
main.c:44:47: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
44 | printf("Number of unholy 8x8 matrices: %llu\n", count);
| ~~~^ ~~~~~
| | |
| | uint64_t {aka long unsigned int}
| long long unsigned int
| %lu
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
This is a complex problem that involves combinatorics and programming. Here's one possible approach: 1. Generate all 8x8 binary matrices. There are 2^64 of them, but we can reduce this number by not considering matrices that have isolated 1s. 2. Check if a matrix is 'unholy' by scanning through the matrix and checking if there are any isolated 1s. If there are, then the matrix is not 'unholy'. 3. Count the number of 'unholy' matrices. Here's some sample C code that implements this approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK(x, y) (1ULL << ((x) * SIZE + (y))) uint64_t count_unholy(uint64_t mask) { int x, y; uint64_t count = 0; for (x = 0; x < SIZE - 1; x++) { for (y = 0; y < SIZE - 1; y++) { uint64_t left = mask & MASK(x, y + 1); uint64_t right = mask & MASK(x, y - 1); uint64_t above = mask & MASK(x + 1, y); uint64_t below = mask & MASK(x - 1, y); if ((mask & MASK(x, y)) && !(left || right || above || below)) { return 0; } } } for (y = 0; y < SIZE; y++) { uint64_t left = mask & MASK(SIZE - 1, y); uint64_t right = mask & MASK(0, y); uint64_t above = mask & MASK(SIZE - 1, y); if ((mask & MASK(SIZE - 1, y)) && !(left || right || above)) { return 0; } } for (x = 0; x < SIZE; x++) { uint64_t above = mask & MASK(x, SIZE - 1); uint64_t below = mask & MASK(x, 0); uint64_t left = mask & MASK(x, SIZE - 1); if ((mask & MASK(x, 0)) && !(left || above || below)) { return 0; } } uint64_t corner = mask & MASK(SIZE - 1, SIZE - 1); if (corner && !(mask & MASK(SIZE - 1, SIZE - 2))) { return 0; } count = 1; for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { if ((mask & MASK(x, y)) == 0) { uint64_t new_mask = mask | MASK(x, y); count += count_unholy(new_mask); } } } return count; } int main() { uint64_t total = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { uint64_t mask = MASK(i, j); total += count_unholy(mask); } } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK(x, y) (1ULL << ((x) * SIZE + (y))) uint64_t count_unholy(uint64_t mask) { int x, y; uint64_t count = 0; for (x = 0; x < SIZE - 1; x++) { for (y = 0; y < SIZE - 1; y++) { uint64_t left = mask & MASK(x, y + 1); uint64_t right = mask & MASK(x, y - 1); uint64_t above = mask & MASK(x + 1, y); uint64_t below = mask & MASK(x - 1, y); if ((mask & MASK(x, y)) && !(left || right || above || below)) { return 0; } } } for (y = 0; y < SIZE; y++) { uint64_t left = mask & MASK(SIZE - 1, y); uint64_t right = mask & MASK(0, y); uint64_t above = mask & MASK(SIZE - 1, y); if ((mask & MASK(SIZE - 1, y)) && !(left || right || above)) { return 0; } } for (x = 0; x < SIZE; x++) { uint64_t above = mask & MASK(x, SIZE - 1); uint64_t below = mask & MASK(x, 0); uint64_t left = mask & MASK(x, SIZE - 1); if ((mask & MASK(x, 0)) && !(left || above || below)) { return 0; } } uint64_t corner = mask & MASK(SIZE - 1, SIZE - 1); if (corner && !(mask & MASK(SIZE - 1, SIZE - 2))) { return 0; } count = 1; for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { if ((mask & MASK(x, y)) == 0) { uint64_t new_mask = mask | MASK(x, y); count += count_unholy(new_mask); } } } return count; } int main() { uint64_t total = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { uint64_t mask = MASK(i, j); total += count_unholy(mask); } } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <stdio.h> #include <stdint.h> #define SIZE 8 #define MASK(x, y) (1ULL << ((x) * SIZE + (y))) uint64_t count_unholy(uint64_t mask) { int x, y; uint64_t count = 0; for (x = 0; x < SIZE - 1; x++) { for (y = 0; y < SIZE - 1; y++) { uint64_t left = mask & MASK(x, y + 1); uint64_t right = mask & MASK(x, y - 1); uint64_t above = mask & MASK(x + 1, y); uint64_t below = mask & MASK(x - 1, y); if ((mask & MASK(x, y)) && !(left || right || above || below)) { return 0; } } } for (y = 0; y < SIZE; y++) { uint64_t left = mask & MASK(SIZE - 1, y); uint64_t right = mask & MASK(0, y); uint64_t above = mask & MASK(SIZE - 1, y); if ((mask & MASK(SIZE - 1, y)) && !(left || right || above)) { return 0; } } for (x = 0; x < SIZE; x++) { uint64_t above = mask & MASK(x, SIZE - 1); uint64_t below = mask & MASK(x, 0); uint64_t left = mask & MASK(x, SIZE - 1); if ((mask & MASK(x, 0)) && !(left || above || below)) { return 0; } } uint64_t corner = mask & MASK(SIZE - 1, SIZE - 1); if (corner && !(mask & MASK(SIZE - 1, SIZE - 2))) { return 0; } count = 1; for (x = 0; x < SIZE; x++) { for (y = 0; y < SIZE; y++) { if ((mask & MASK(x, y)) == 0) { uint64_t new_mask = mask | MASK(x, y); count += count_unholy(new_mask); } } } return count; } int main() { uint64_t total = 0; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { uint64_t mask = MASK(i, j); total += count_unholy(mask); } } printf("The total number of unholy 8x8 matrices is: %llu\n", total); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
A binary 8x8 matrix is 'unholy' if there are no isolated 1s. A 1 is isolated if there isn't another 1 in any of the 4 direct cardinal directions. Write a C program that counts the total number of unholy 8x8 matricies.
This problem can be solved using dynamic programming. We can count the number of ways to form an unholy binary matrix row by row, and use the results from previous rows to calculate the number of ways for the current row. Here's a C program that counts the total number of unholy 8x8 matrices:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h> #define SIZE 8 #define MOD 1000000007 long long dp[SIZE][1 << SIZE]; long long count_unholy(int row, int prev_state) { if (row == SIZE) { return (prev_state == 0); } if (dp[row][prev_state] != -1) { return dp[row][prev_state]; } long long ways = 0; for (int curr_state = 0; curr_state < (1 << SIZE); curr_state++) { int can_add = 1; for (int i = 0; i < SIZE; i++) { int is_set_in_prev = (prev_state & (1 << i)) != 0; int is_set_in_curr = (curr_state & (1 << i)) != 0; if (is_set_in_curr && (!is_set_in_prev && i > 0 || is_set_in_prev && i < SIZE - 1 || (is_set_in_prev && (i > 1 && (prev_state & (1 << (i - 2))) == 0)) || (is_set_in_prev && (i < SIZE - 2 && (prev_state & (1 << (i + 2))) == 0)))) { can_add = 0; break; } } if (can_add) { ways = (ways + count_unholy(row + 1, curr_state)) % MOD; } } return dp[row][prev_state] = ways; } int main() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < (1 << SIZE); j++) { dp[i][j] = -1; } } long long result = 0; for (int i = 0; i < (1 << SIZE); i++) { result = (result + count_unholy(1, i)) % MOD; } printf("Number of unholy 8x8 matrices: %lld\n", result); return 0; } |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h> #define SIZE 8 #define MOD 1000000007 long long dp[SIZE][1 << SIZE]; long long count_unholy(int row, int prev_state) { if (row == SIZE) { return (prev_state == 0); } if (dp[row][prev_state] != -1) { return dp[row][prev_state]; } long long ways = 0; for (int curr_state = 0; curr_state < (1 << SIZE); curr_state++) { int can_add = 1; for (int i = 0; i < SIZE; i++) { int is_set_in_prev = (prev_state & (1 << i)) != 0; int is_set_in_curr = (curr_state & (1 << i)) != 0; if (is_set_in_curr && (!is_set_in_prev && i > 0 || is_set_in_prev && i < SIZE - 1 || (is_set_in_prev && (i > 1 && (prev_state & (1 << (i - 2))) == 0)) || (is_set_in_prev && (i < SIZE - 2 && (prev_state & (1 << (i + 2))) == 0)))) { can_add = 0; break; } } if (can_add) { ways = (ways + count_unholy(row + 1, curr_state)) % MOD; } } return dp[row][prev_state] = ways; } int main() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < (1 << SIZE); j++) { dp[i][j] = -1; } } long long result = 0; for (int i = 0; i < (1 << SIZE); i++) { result = (result + count_unholy(1, i)) % MOD; } printf("Number of unholy 8x8 matrices: %lld\n", result); return 0; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <stdio.h> #define SIZE 8 #define MOD 1000000007 long long dp[SIZE][1 << SIZE]; long long count_unholy(int row, int prev_state) { if (row == SIZE) { return (prev_state == 0); } if (dp[row][prev_state] != -1) { return dp[row][prev_state]; } long long ways = 0; for (int curr_state = 0; curr_state < (1 << SIZE); curr_state++) { int can_add = 1; for (int i = 0; i < SIZE; i++) { int is_set_in_prev = (prev_state & (1 << i)) != 0; int is_set_in_curr = (curr_state & (1 << i)) != 0; if (is_set_in_curr && (!is_set_in_prev && i > 0 || is_set_in_prev && i < SIZE - 1 || (is_set_in_prev && (i > 1 && (prev_state & (1 << (i - 2))) == 0)) || (is_set_in_prev && (i < SIZE - 2 && (prev_state & (1 << (i + 2))) == 0)))) { can_add = 0; break; } } if (can_add) { ways = (ways + count_unholy(row + 1, curr_state)) % MOD; } } return dp[row][prev_state] = ways; } int main() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < (1 << SIZE); j++) { dp[i][j] = -1; } } long long result = 0; for (int i = 0; i < (1 << SIZE); i++) { result = (result + count_unholy(1, i)) % MOD; } printf("Number of unholy 8x8 matrices: %lld\n", result); return 0; } |
Number of unholy 8x8 matrices: 5440
Testing if the previous output contains the string 1121231537486377866
: False