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.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
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.To solve this question: - Use bit arithmetic and an unsigned long to represent the matrix. - Write a function is_unholy that tests if a matrix is unholy by bit-shifting and masking. Use a dynamic program to count the number of unholy matricies. - Recurse on the number of rows the matrix has. - Enumerate all 2^16 next two rows. This is enough to completely isolate the prior solutions from the next. Write out a plan for the program, and then implement the plan in C.
**Program Plan:** ### Step 1: Represent the Matrix * Use an unsigned long to represent each row of the matrix. * Use bit arithmetic to manipulate the matrix. ### Step 2: Implement the `is_unholy` Function * Write a function `is_unholy` that takes a matrix as input and returns 1 if the matrix is unholy, 0 otherwise. * Use bit-shifting and masking to check for isolated 1s in the matrix. ### Step 3: Implement the Dynamic Programming Approach * Write a recursive function to count the number of unholy matrices with a given number of rows. * Recurse on the number of rows, starting from 1 and going up to 8. * Enumerate all 2^16 possible next two rows and check if the resulting matrix is unholy. * Use memoization to store intermediate results and avoid redundant computation. ### Step 4: Implement the Main Function * Call the recursive function with the initial number of rows (8) and print the result. **Implementation:**
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 | #include <stdio.h> #include <stdint.h> #include <stdbool.h> // Define the size of the matrix #define MATRIX_SIZE 8 // Define the mask for the first row #define FIRST_ROW_MASK 0xFF // Define the mask for the second row #define SECOND_ROW_MASK 0xFF00 // Function to check if a matrix is unholy bool is_unholy(uint64_t matrix) { // Iterate over each row for (int i = 0; i < MATRIX_SIZE; i++) { // Get the current row uint64_t row = (matrix >> (i * 8)) & 0xFF; // Check for isolated 1s in the current row for (int j = 0; j < MATRIX_SIZE; j++) { // Check if the current bit is 1 if ((row >> j) & 1) { // Check if there is no adjacent 1 in the same row or in the adjacent rows if (!(row >> (j + 1) & 1) && !(row >> (j - 1) & 1) && !(i > 0 && (matrix >> ((i - 1) * 8 + j)) & 1) && !(i < MATRIX_SIZE - 1 && (matrix >> ((i + 1) * 8 + j)) & 1)) { return false; } } } } return true; } // Recursive function to count the number of unholy matrices uint64_t count_unholy_matrices(int rows, uint64_t prev_rows) { // Base case: if there are no more rows to add, check if the matrix is unholy if (rows == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Initialize the count of unholy matrices uint64_t count = 0; // Enumerate all possible next two rows for (uint16_t next_two_rows = 0; next_two_rows < (1 << 16); next_two_rows++) { // Add the next two rows to the previous rows uint64_t new_rows = (prev_rows << 16) | next_two_rows; // Recurse on the number of rows count += count_unholy_matrices(rows - 2, new_rows); } return count; } int main() { // Call the recursive function with the initial number of rows and print the result uint64_t count = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <stdio.h> #include <stdint.h> #include <stdbool.h> // Define the size of the matrix #define MATRIX_SIZE 8 // Define the mask for the first row #define FIRST_ROW_MASK 0xFF // Define the mask for the second row #define SECOND_ROW_MASK 0xFF00 // Function to check if a matrix is unholy bool is_unholy(uint64_t matrix) { // Iterate over each row for (int i = 0; i < MATRIX_SIZE; i++) { // Get the current row uint64_t row = (matrix >> (i * 8)) & 0xFF; // Check for isolated 1s in the current row for (int j = 0; j < MATRIX_SIZE; j++) { // Check if the current bit is 1 if ((row >> j) & 1) { // Check if there is no adjacent 1 in the same row or in the adjacent rows if (!(row >> (j + 1) & 1) && !(row >> (j - 1) & 1) && !(i > 0 && (matrix >> ((i - 1) * 8 + j)) & 1) && !(i < MATRIX_SIZE - 1 && (matrix >> ((i + 1) * 8 + j)) & 1)) { return false; } } } } return true; } // Recursive function to count the number of unholy matrices uint64_t count_unholy_matrices(int rows, uint64_t prev_rows) { // Base case: if there are no more rows to add, check if the matrix is unholy if (rows == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Initialize the count of unholy matrices uint64_t count = 0; // Enumerate all possible next two rows for (uint16_t next_two_rows = 0; next_two_rows < (1 << 16); next_two_rows++) { // Add the next two rows to the previous rows uint64_t new_rows = (prev_rows << 16) | next_two_rows; // Recurse on the number of rows count += count_unholy_matrices(rows - 2, new_rows); } return count; } int main() { // Call the recursive function with the initial number of rows and print the result uint64_t count = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | #include <stdio.h> #include <stdint.h> #include <stdbool.h> // Define the size of the matrix #define MATRIX_SIZE 8 // Define the mask for the first row #define FIRST_ROW_MASK 0xFF // Define the mask for the second row #define SECOND_ROW_MASK 0xFF00 // Function to check if a matrix is unholy bool is_unholy(uint64_t matrix) { // Iterate over each row for (int i = 0; i < MATRIX_SIZE; i++) { // Get the current row uint64_t row = (matrix >> (i * 8)) & 0xFF; // Check for isolated 1s in the current row for (int j = 0; j < MATRIX_SIZE; j++) { // Check if the current bit is 1 if ((row >> j) & 1) { // Check if there is no adjacent 1 in the same row or in the adjacent rows if (!(row >> (j + 1) & 1) && !(row >> (j - 1) & 1) && !(i > 0 && (matrix >> ((i - 1) * 8 + j)) & 1) && !(i < MATRIX_SIZE - 1 && (matrix >> ((i + 1) * 8 + j)) & 1)) { return false; } } } } return true; } // Recursive function to count the number of unholy matrices uint64_t count_unholy_matrices(int rows, uint64_t prev_rows) { // Base case: if there are no more rows to add, check if the matrix is unholy if (rows == 0) { return is_unholy(prev_rows) ? 1 : 0; } // Initialize the count of unholy matrices uint64_t count = 0; // Enumerate all possible next two rows for (uint16_t next_two_rows = 0; next_two_rows < (1 << 16); next_two_rows++) { // Add the next two rows to the previous rows uint64_t new_rows = (prev_rows << 16) | next_two_rows; // Recurse on the number of rows count += count_unholy_matrices(rows - 2, new_rows); } return count; } int main() { // Call the recursive function with the initial number of rows and print the result uint64_t count = count_unholy_matrices(MATRIX_SIZE, 0); printf("Total number of unholy matrices: %llu\n", count); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False