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.
# Counting Unholy 8x8 Binary Matrices ## Understanding the Problem An 8x8 binary matrix is considered "unholy" if it has no isolated 1s. A 1 is isolated if there isn't another 1 in any of the four cardinal directions (up, down, left, right). ## Solution Plan 1. **Representation**: Use an unsigned long long to represent each row of the 8x8 matrix (8 bits per row). 2. **Dynamic Programming**: - Define DP[row][prevRow][currRow] where: - row: Number of rows processed (0-8) - prevRow: Configuration of previous row (8 bits) - currRow: Configuration of current row (8 bits) - DP[row][prevRow][currRow] = number of ways to complete the matrix 3. **Transition**: For each possible next row configuration (2^8 = 256 possibilities), check if no 1 in the current row becomes isolated, then update DP[row+1][currRow][nextRow]. 4. **Base Case**: DP[0][0][0] = 1 (one way to have 0 rows filled) 5. **Final Answer**: Sum of DP[8][prevRow][currRow] for all valid prevRow and currRow combinations where no 1 in currRow is isolated. ## 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 65 66 67 68 69 70 71 72 73 74 75 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> // Check if a 1 in current row is isolated int is_isolated(uint8_t prevRow, uint8_t currRow, uint8_t nextRow) { for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left neighbor (if not leftmost column) int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; // Check right neighbor (if not rightmost column) int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; // Check up neighbor int up = (prevRow >> i) & 1; // Check down neighbor int down = (nextRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up || down)) { return 1; // Isolated 1 found } } } return 0; // No isolated 1 found } uint64_t count_unholy_matrices() { // DP[row][prevRow][currRow] = count of valid configurations uint64_t dp[9][256][256]; memset(dp, 0, sizeof(dp)); // Base case: one way to have 0 rows dp[0][0][0] = 1; // Fill dp table for (int row = 0; row < 8; row++) { for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[row][prevRow][currRow] == 0) continue; // Try all possible next rows for (int nextRow = 0; nextRow < 256; nextRow++) { // Check if no 1 in current row is isolated if (!is_isolated(prevRow, currRow, nextRow)) { dp[row+1][currRow][nextRow] += dp[row][prevRow][currRow]; } } } } } // Sum up all valid final states uint64_t result = 0; for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { // For the final row, there's no next row, so we use 0 // But we still need to check that the last row has no isolated 1s if (!is_isolated(prevRow, currRow, 0)) { result += dp[8][prevRow][currRow]; } } } return result; } int main() { uint64_t count = count_unholy_matrices(); printf("Number of unholy 8x8 matrices: %llu\n", count); return 0; } |
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> // Check if a 1 in current row is isolated int is_isolated(uint8_t prevRow, uint8_t currRow, uint8_t nextRow) { for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left neighbor (if not leftmost column) int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; // Check right neighbor (if not rightmost column) int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; // Check up neighbor int up = (prevRow >> i) & 1; // Check down neighbor int down = (nextRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up || down)) { return 1; // Isolated 1 found } } } return 0; // No isolated 1 found } uint64_t count_unholy_matrices() { // DP[row][prevRow][currRow] = count of valid configurations uint64_t dp[9][256][256]; memset(dp, 0, sizeof(dp)); // Base case: one way to have 0 rows dp[0][0][0] = 1; // Fill dp table for rows 0 to 7 for (int row = 0; row < 8; row++) { for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[row][prevRow][currRow] == 0) continue; // Try all possible next rows for (int nextRow = 0; nextRow < 256; nextRow++) { // Check if no 1 in current row is isolated if (!is_isolated(prevRow, currRow, nextRow)) { dp[row+1][currRow][nextRow] += dp[row][prevRow][currRow]; } } } } } // For the 8th row, we need to ensure no 1s are isolated in the last row uint64_t result = 0; for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[8][prevRow][currRow] > 0) { // For the last row, we check if any 1 in currRow is isolated // without considering a next row int isolated = 0; for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left, right, and up neighbors int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; int up = (prevRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up)) { isolated = 1; break; } } } if (!isolated) { result += dp[8][prevRow][currRow]; } } } } return result; } int main() { uint64_t count = count_unholy_matrices(); 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 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> // Check if a 1 in current row is isolated int is_isolated(uint8_t prevRow, uint8_t currRow, uint8_t nextRow) { for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left neighbor (if not leftmost column) int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; // Check right neighbor (if not rightmost column) int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; // Check up neighbor int up = (prevRow >> i) & 1; // Check down neighbor int down = (nextRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up || down)) { return 1; // Isolated 1 found } } } return 0; // No isolated 1 found } uint64_t count_unholy_matrices() { // DP[row][prevRow][currRow] = count of valid configurations uint64_t dp[9][256][256]; memset(dp, 0, sizeof(dp)); // Base case: one way to have 0 rows dp[0][0][0] = 1; // Fill dp table for rows 0 to 7 for (int row = 0; row < 8; row++) { for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[row][prevRow][currRow] == 0) continue; // Try all possible next rows for (int nextRow = 0; nextRow < 256; nextRow++) { // Check if no 1 in current row is isolated if (!is_isolated(prevRow, currRow, nextRow)) { dp[row+1][currRow][nextRow] += dp[row][prevRow][currRow]; } } } } } // For the 8th row, we need to ensure no 1s are isolated in the last row uint64_t result = 0; for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[8][prevRow][currRow] > 0) { // For the last row, we check if any 1 in currRow is isolated // without considering a next row int isolated = 0; for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left, right, and up neighbors int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; int up = (prevRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up)) { isolated = 1; break; } } } if (!isolated) { result += dp[8][prevRow][currRow]; } } } } return result; } int main() { uint64_t count = count_unholy_matrices(); 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 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h> // Check if a 1 in current row is isolated int is_isolated(uint8_t prevRow, uint8_t currRow, uint8_t nextRow) { for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left neighbor (if not leftmost column) int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; // Check right neighbor (if not rightmost column) int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; // Check up neighbor int up = (prevRow >> i) & 1; // Check down neighbor int down = (nextRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up || down)) { return 1; // Isolated 1 found } } } return 0; // No isolated 1 found } uint64_t count_unholy_matrices() { // DP[row][prevRow][currRow] = count of valid configurations uint64_t dp[9][256][256]; memset(dp, 0, sizeof(dp)); // Base case: one way to have 0 rows dp[0][0][0] = 1; // Fill dp table for rows 0 to 7 for (int row = 0; row < 8; row++) { for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[row][prevRow][currRow] == 0) continue; // Try all possible next rows for (int nextRow = 0; nextRow < 256; nextRow++) { // Check if no 1 in current row is isolated if (!is_isolated(prevRow, currRow, nextRow)) { dp[row+1][currRow][nextRow] += dp[row][prevRow][currRow]; } } } } } // For the 8th row, we need to ensure no 1s are isolated in the last row uint64_t result = 0; for (int prevRow = 0; prevRow < 256; prevRow++) { for (int currRow = 0; currRow < 256; currRow++) { if (dp[8][prevRow][currRow] > 0) { // For the last row, we check if any 1 in currRow is isolated // without considering a next row int isolated = 0; for (int i = 0; i < 8; i++) { if ((currRow >> i) & 1) { // If there's a 1 at position i // Check left, right, and up neighbors int left = (i > 0) ? ((currRow >> (i-1)) & 1) : 0; int right = (i < 7) ? ((currRow >> (i+1)) & 1) : 0; int up = (prevRow >> i) & 1; // If there's no neighbor, the 1 is isolated if (!(left || right || up)) { isolated = 1; break; } } } if (!isolated) { result += dp[8][prevRow][currRow]; } } } } return result; } int main() { uint64_t count = count_unholy_matrices(); printf("Number of unholy 8x8 matrices: %llu\n", count); return 0; } |
Number of unholy 8x8 matrices: 1121231537486377866
main.c: In function 'main':
main.c:92:47: warning: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
92 | 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
: True
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.
# Counting Unholy 8x8 Matrices in C I'll create a program to count 8x8 binary matrices that have no isolated 1s, using bit manipulation and dynamic programming. ## Understanding the Problem - An 8x8 binary matrix is represented as 64 bits (can fit in an unsigned long long) - A 1 is "isolated" if it has no 1s in the four adjacent positions (up, down, left, right) - A matrix is "unholy" if it contains no isolated 1s - We'll count all possible unholy 8x8 matrices using dynamic programming ## Approach 1. We'll recurse on rows, building the matrix two rows at a time 2. Each state in our DP table represents the number of unholy matrices with the last two rows in a specific configuration 3. For each valid configuration of the last two rows, we'll try all possible 2^16 configurations for the next two rows 4. A configuration is valid if it doesn't create any isolated 1s ## Implementation Plan 1. Create a function to check if a matrix is unholy 2. Implement a DP solution that builds the matrix row by row 3. Use bit operations for efficiency 4. Calculate the total count of unholy matrices Here's the C 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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #define ROW_SIZE 8 #define MATRIX_ROWS 8 #define MAX_PATTERN (1 << 16) // 2^16 possible patterns for two rows // Function to check if a 1 at position (row, col) is isolated bool is_isolated(uint64_t matrix, int row, int col) { // Check if there's a 1 bit to the left if (col > 0 && (matrix & (1ULL << ((row * ROW_SIZE) + col - 1)))) return false; // Check if there's a 1 bit to the right if (col < ROW_SIZE - 1 && (matrix & (1ULL << ((row * ROW_SIZE) + col + 1)))) return false; // Check if there's a 1 bit above if (row > 0 && (matrix & (1ULL << (((row - 1) * ROW_SIZE) + col)))) return false; // Check if there's a 1 bit below if (row < MATRIX_ROWS - 1 && (matrix & (1ULL << (((row + 1) * ROW_SIZE) + col)))) return false; // If we didn't find any adjacent 1s, the bit is isolated return true; } // Function to check if two rows (current and next) have any isolated 1s bool has_isolated_ones(uint16_t prev_rows, uint16_t curr_rows) { // Extract individual rows uint8_t row1 = (prev_rows >> 8) & 0xFF; // Second row of prev_rows uint8_t row2 = prev_rows & 0xFF; // First row of curr_rows uint8_t row3 = (curr_rows >> 8) & 0xFF; // Second row of curr_rows uint8_t row4 = curr_rows & 0xFF; // First row of next set // Check for isolated 1s in row2 (which is between row1 and row3) for (int col = 0; col < ROW_SIZE; col++) { if ((row2 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; // Check below (row3) else if (row3 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } // Check for isolated 1s in row3 (which is between row2 and row4) for (int col = 0; col < ROW_SIZE; col++) { if ((row3 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row3 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row3 & (1 << (col + 1)))) has_adjacent = true; // Check above (row2) else if (row2 & (1 << col)) has_adjacent = true; // Check below (row4) else if (row4 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } return false; // No isolated 1s found } // Function to count unholy matrices uint64_t count_unholy_matrices() { // DP[r][pattern] = number of unholy matrices with r rows where the last two rows form the pattern uint64_t dp[MATRIX_ROWS/2 + 1][MAX_PATTERN] = {0}; // Base case: Initialize the DP table for the first two rows for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { // Check if the 2-row pattern has any isolated 1s bool isolated = false; uint8_t row1 = (pattern >> 8) & 0xFF; uint8_t row2 = pattern & 0xFF; for (int col = 0; col < ROW_SIZE; col++) { // Check row1 if ((row1 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row1 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row1 & (1 << (col + 1)))) has_adjacent = true; // Check below (row2) else if (row2 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } // Check row2 if ((row2 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } } if (!isolated) dp[1][pattern] = 1; // This 2-row pattern is unholy } // Fill the DP table for (int r = 2; r <= MATRIX_ROWS/2; r++) { for (uint32_t prev_pattern = 0; prev_pattern < MAX_PATTERN; prev_pattern++) { if (dp[r-1][prev_pattern] == 0) continue; // Skip if there are no unholy matrices with this pattern for (uint32_t curr_pattern = 0; curr_pattern < MAX_PATTERN; curr_pattern++) { // Check if adding the next two rows creates any isolated 1s if (!has_isolated_ones(prev_pattern, curr_pattern)) { dp[r][curr_pattern] += dp[r-1][prev_pattern]; } } } } // Sum up all the possible unholy matrices with 8 rows uint64_t total = 0; for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { total += dp[MATRIX_ROWS/2][pattern]; } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("Total 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 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #define ROW_SIZE 8 #define MATRIX_ROWS 8 #define MAX_PATTERN (1 << 16) // 2^16 possible patterns for two rows // Function to check if a 1 at position (row, col) is isolated bool is_isolated(uint64_t matrix, int row, int col) { // Check if there's a 1 bit to the left if (col > 0 && (matrix & (1ULL << ((row * ROW_SIZE) + col - 1)))) return false; // Check if there's a 1 bit to the right if (col < ROW_SIZE - 1 && (matrix & (1ULL << ((row * ROW_SIZE) + col + 1)))) return false; // Check if there's a 1 bit above if (row > 0 && (matrix & (1ULL << (((row - 1) * ROW_SIZE) + col)))) return false; // Check if there's a 1 bit below if (row < MATRIX_ROWS - 1 && (matrix & (1ULL << (((row + 1) * ROW_SIZE) + col)))) return false; // If we didn't find any adjacent 1s, the bit is isolated return true; } // Function to check if two rows (current and next) have any isolated 1s bool has_isolated_ones(uint16_t prev_rows, uint16_t curr_rows) { // Extract individual rows uint8_t row1 = (prev_rows >> 8) & 0xFF; // Second row of prev_rows uint8_t row2 = prev_rows & 0xFF; // First row of curr_rows uint8_t row3 = (curr_rows >> 8) & 0xFF; // Second row of curr_rows uint8_t row4 = curr_rows & 0xFF; // First row of next set // Check for isolated 1s in row2 (which is between row1 and row3) for (int col = 0; col < ROW_SIZE; col++) { if ((row2 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; // Check below (row3) else if (row3 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } // Check for isolated 1s in row3 (which is between row2 and row4) for (int col = 0; col < ROW_SIZE; col++) { if ((row3 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row3 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row3 & (1 << (col + 1)))) has_adjacent = true; // Check above (row2) else if (row2 & (1 << col)) has_adjacent = true; // Check below (row4) else if (row4 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } return false; // No isolated 1s found } // Function to count unholy matrices uint64_t count_unholy_matrices() { // DP[r][pattern] = number of unholy matrices with r rows where the last two rows form the pattern uint64_t dp[MATRIX_ROWS/2 + 1][MAX_PATTERN] = {0}; // Base case: Initialize the DP table for the first two rows for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { // Check if the 2-row pattern has any isolated 1s bool isolated = false; uint8_t row1 = (pattern >> 8) & 0xFF; uint8_t row2 = pattern & 0xFF; for (int col = 0; col < ROW_SIZE; col++) { // Check row1 if ((row1 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row1 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row1 & (1 << (col + 1)))) has_adjacent = true; // Check below (row2) else if (row2 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } // Check row2 if ((row2 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } } if (!isolated) dp[1][pattern] = 1; // This 2-row pattern is unholy } // Fill the DP table for (int r = 2; r <= MATRIX_ROWS/2; r++) { for (uint32_t prev_pattern = 0; prev_pattern < MAX_PATTERN; prev_pattern++) { if (dp[r-1][prev_pattern] == 0) continue; // Skip if there are no unholy matrices with this pattern for (uint32_t curr_pattern = 0; curr_pattern < MAX_PATTERN; curr_pattern++) { // Check if adding the next two rows creates any isolated 1s if (!has_isolated_ones(prev_pattern, curr_pattern)) { dp[r][curr_pattern] += dp[r-1][prev_pattern]; } } } } // Sum up all the possible unholy matrices with 8 rows uint64_t total = 0; for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { total += dp[MATRIX_ROWS/2][pattern]; } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("Total 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 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <stdbool.h> #define ROW_SIZE 8 #define MATRIX_ROWS 8 #define MAX_PATTERN (1 << 16) // 2^16 possible patterns for two rows // Function to check if a 1 at position (row, col) is isolated bool is_isolated(uint64_t matrix, int row, int col) { // Check if there's a 1 bit to the left if (col > 0 && (matrix & (1ULL << ((row * ROW_SIZE) + col - 1)))) return false; // Check if there's a 1 bit to the right if (col < ROW_SIZE - 1 && (matrix & (1ULL << ((row * ROW_SIZE) + col + 1)))) return false; // Check if there's a 1 bit above if (row > 0 && (matrix & (1ULL << (((row - 1) * ROW_SIZE) + col)))) return false; // Check if there's a 1 bit below if (row < MATRIX_ROWS - 1 && (matrix & (1ULL << (((row + 1) * ROW_SIZE) + col)))) return false; // If we didn't find any adjacent 1s, the bit is isolated return true; } // Function to check if two rows (current and next) have any isolated 1s bool has_isolated_ones(uint16_t prev_rows, uint16_t curr_rows) { // Extract individual rows uint8_t row1 = (prev_rows >> 8) & 0xFF; // Second row of prev_rows uint8_t row2 = prev_rows & 0xFF; // First row of curr_rows uint8_t row3 = (curr_rows >> 8) & 0xFF; // Second row of curr_rows uint8_t row4 = curr_rows & 0xFF; // First row of next set // Check for isolated 1s in row2 (which is between row1 and row3) for (int col = 0; col < ROW_SIZE; col++) { if ((row2 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; // Check below (row3) else if (row3 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } // Check for isolated 1s in row3 (which is between row2 and row4) for (int col = 0; col < ROW_SIZE; col++) { if ((row3 & (1 << col))) { // If there's a 1 at this position bool has_adjacent = false; // Check left if (col > 0 && (row3 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row3 & (1 << (col + 1)))) has_adjacent = true; // Check above (row2) else if (row2 & (1 << col)) has_adjacent = true; // Check below (row4) else if (row4 & (1 << col)) has_adjacent = true; if (!has_adjacent) return true; // Found an isolated 1 } } return false; // No isolated 1s found } // Function to count unholy matrices uint64_t count_unholy_matrices() { // DP[r][pattern] = number of unholy matrices with r rows where the last two rows form the pattern uint64_t dp[MATRIX_ROWS/2 + 1][MAX_PATTERN] = {0}; // Base case: Initialize the DP table for the first two rows for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { // Check if the 2-row pattern has any isolated 1s bool isolated = false; uint8_t row1 = (pattern >> 8) & 0xFF; uint8_t row2 = pattern & 0xFF; for (int col = 0; col < ROW_SIZE; col++) { // Check row1 if ((row1 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row1 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row1 & (1 << (col + 1)))) has_adjacent = true; // Check below (row2) else if (row2 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } // Check row2 if ((row2 & (1 << col))) { bool has_adjacent = false; // Check left if (col > 0 && (row2 & (1 << (col - 1)))) has_adjacent = true; // Check right else if (col < ROW_SIZE - 1 && (row2 & (1 << (col + 1)))) has_adjacent = true; // Check above (row1) else if (row1 & (1 << col)) has_adjacent = true; if (!has_adjacent) { isolated = true; break; } } } if (!isolated) dp[1][pattern] = 1; // This 2-row pattern is unholy } // Fill the DP table for (int r = 2; r <= MATRIX_ROWS/2; r++) { for (uint32_t prev_pattern = 0; prev_pattern < MAX_PATTERN; prev_pattern++) { if (dp[r-1][prev_pattern] == 0) continue; // Skip if there are no unholy matrices with this pattern for (uint32_t curr_pattern = 0; curr_pattern < MAX_PATTERN; curr_pattern++) { // Check if adding the next two rows creates any isolated 1s if (!has_isolated_ones(prev_pattern, curr_pattern)) { dp[r][curr_pattern] += dp[r-1][prev_pattern]; } } } } // Sum up all the possible unholy matrices with 8 rows uint64_t total = 0; for (uint32_t pattern = 0; pattern < MAX_PATTERN; pattern++) { total += dp[MATRIX_ROWS/2][pattern]; } return total; } int main() { uint64_t count = count_unholy_matrices(); printf("Total number of unholy 8x8 matrices: %llu\n", count); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False