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.
Here's a C program that counts unholy 8x8 binary matrices. We'll use backtracking to generate all possible matrices and check each one for the unholy condition:
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; int matrix[SIZE][SIZE]; // Check if a 1 at position (row, col) is isolated bool isIsolated(int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Check if the matrix is unholy (no isolated 1s) bool isUnholy() { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(i, j)) { return false; } } } return true; } // Recursive function to generate all possible matrices void generateMatrices(int row, int col) { if(row == SIZE) { if(isUnholy()) { count++; } return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; generateMatrices(nextRow, nextCol); // Try placing 1 matrix[row][col] = 1; generateMatrices(nextRow, nextCol); } int main() { // Initialize matrix to 0 for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { matrix[i][j] = 0; } } generateMatrices(0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; int matrix[SIZE][SIZE]; bool canBeNonIsolated(int row, int col) { // Check if there's potential for this 1 to be non-isolated // by checking if there's space for adjacent 1s if (row > 0 || row < SIZE-1 || col > 0 || col < SIZE-1) { return true; } return false; } bool isIsolatedSoFar(int row, int col) { // Only check positions that have already been filled // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // For completed rows, also check down and right if (row < SIZE-1 && matrix[row+1][col] == 1) return false; if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } void generateMatrices(int row, int col) { if(row == SIZE) { count++; return; } int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; generateMatrices(nextRow, nextCol); // Try placing 1 matrix[row][col] = 1; // Only continue if this 1 isn't definitely isolated if (!isIsolatedSoFar(row, col) || canBeNonIsolated(row, col)) { generateMatrices(nextRow, nextCol); } } int main() { generateMatrices(0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; int matrix[SIZE][SIZE]; // Check if a 1 at position (row, col) is isolated bool isIsolated(int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Check if the matrix is unholy (no isolated 1s) bool isUnholy() { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(i, j)) { return false; } } } return true; } // Recursive function to generate all possible matrices void generateMatrices(int row, int col) { if(row == SIZE) { if(isUnholy()) { count++; } return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; generateMatrices(nextRow, nextCol); // Try placing 1 matrix[row][col] = 1; generateMatrices(nextRow, nextCol); } int main() { // Initialize matrix to 0 for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { matrix[i][j] = 0; } } generateMatrices(0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; int matrix[SIZE][SIZE]; // Check if a 1 at position (row, col) is isolated bool isIsolated(int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Check if the matrix is unholy (no isolated 1s) bool isUnholy() { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(i, j)) { return false; } } } return true; } // Recursive function to generate all possible matrices void generateMatrices(int row, int col) { if(row == SIZE) { if(isUnholy()) { count++; } return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; generateMatrices(nextRow, nextCol); // Try placing 1 matrix[row][col] = 1; generateMatrices(nextRow, nextCol); } int main() { // Initialize matrix to 0 for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { matrix[i][j] = 0; } } generateMatrices(0, 0); printf("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
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.
Here's a C program that counts unholy 8x8 binary matrices using backtracking:
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check four cardinal directions int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; if (matrix[row][col] != 1) return false; for (int i = 0; i < 4; i++) { int newRow = row + dx[i]; int newCol = col + dy[i]; if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE && matrix[newRow][newCol] == 1) { return false; } } return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long generateMatrices(int matrix[SIZE][SIZE], int row, int col) { if (col == SIZE) { row++; col = 0; } if (row == SIZE) { return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try placing 0 matrix[row][col] = 0; count += generateMatrices(matrix, row, col + 1); // Try placing 1 matrix[row][col] = 1; count += generateMatrices(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = generateMatrices(matrix, 0, 0); printf("Total number of unholy 8x8 matrices: %llu\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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check four cardinal directions int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; if (matrix[row][col] != 1) return false; for (int i = 0; i < 4; i++) { int newRow = row + dx[i]; int newCol = col + dy[i]; if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE && matrix[newRow][newCol] == 1) { return false; } } return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long generateMatrices(int matrix[SIZE][SIZE], int row, int col) { if (col == SIZE) { row++; col = 0; } if (row == SIZE) { return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try placing 0 matrix[row][col] = 0; count += generateMatrices(matrix, row, col + 1); // Try placing 1 matrix[row][col] = 1; count += generateMatrices(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = generateMatrices(matrix, 0, 0); printf("Total number of unholy 8x8 matrices: %llu\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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check four cardinal directions int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; if (matrix[row][col] != 1) return false; for (int i = 0; i < 4; i++) { int newRow = row + dx[i]; int newCol = col + dy[i]; if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE && matrix[newRow][newCol] == 1) { return false; } } return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long generateMatrices(int matrix[SIZE][SIZE], int row, int col) { if (col == SIZE) { row++; col = 0; } if (row == SIZE) { return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try placing 0 matrix[row][col] = 0; count += generateMatrices(matrix, row, col + 1); // Try placing 1 matrix[row][col] = 1; count += generateMatrices(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = generateMatrices(matrix, 0, 0); printf("Total number of unholy 8x8 matrices: %llu\n", result); 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.
Here's a C program that counts the total number of unholy 8x8 matrices using backtracking:
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a cell has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // For all filled positions up to current position for(int i = 0; i <= currentRow; i++) { int colLimit = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= colLimit; j++) { // If we find a 1, it must have an adjacent 1 if(matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if(row == SIZE) { // Check if the final matrix is valid if(isValid(matrix, SIZE-1, SIZE-1)) { count++; } return; } // Calculate next position int nextRow = col == SIZE-1 ? row + 1 : row; int nextCol = col == SIZE-1 ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a cell has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // For all filled positions up to current position for(int i = 0; i <= currentRow; i++) { int colLimit = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= colLimit; j++) { // If we find a 1, it must have an adjacent 1 if(matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if(row == SIZE) { // Check if the final matrix is valid if(isValid(matrix, SIZE-1, SIZE-1)) { count++; } return; } // Calculate next position int nextRow = col == SIZE-1 ? row + 1 : row; int nextCol = col == SIZE-1 ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a cell has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // For all filled positions up to current position for(int i = 0; i <= currentRow; i++) { int colLimit = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= colLimit; j++) { // If we find a 1, it must have an adjacent 1 if(matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if(row == SIZE) { // Check if the final matrix is valid if(isValid(matrix, SIZE-1, SIZE-1)) { count++; } return; } // Calculate next position int nextRow = col == SIZE-1 ? row + 1 : row; int nextCol = col == SIZE-1 ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if(isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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
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.
Here's a C program that counts unholy 8x8 binary matrices using backtracking:
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long countUnholy(int matrix[SIZE][SIZE], int row, int col) { if(col == SIZE) { col = 0; row++; } if(row == SIZE) { // If we've filled the matrix, check if it's unholy return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try putting 0 matrix[row][col] = 0; count += countUnholy(matrix, row, col + 1); // Try putting 1 matrix[row][col] = 1; count += countUnholy(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = countUnholy(matrix, 0, 0); printf("Number of unholy 8x8 matrices: %llu\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 55 56 57 58 59 60 61 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long countUnholy(int matrix[SIZE][SIZE], int row, int col) { if(col == SIZE) { col = 0; row++; } if(row == SIZE) { // If we've filled the matrix, check if it's unholy return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try putting 0 matrix[row][col] = 0; count += countUnholy(matrix, row, col + 1); // Try putting 1 matrix[row][col] = 1; count += countUnholy(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = countUnholy(matrix, 0, 0); printf("Number of unholy 8x8 matrices: %llu\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 55 56 57 58 59 60 61 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a 1 at position (row, col) is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return false; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return false; // Check left if (col > 0 && matrix[row][col-1] == 1) return false; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return false; return true; } // Function to check if matrix has any isolated 1s bool hasIsolatedOnes(int matrix[SIZE][SIZE]) { for(int i = 0; i < SIZE; i++) { for(int j = 0; j < SIZE; j++) { if(matrix[i][j] == 1 && isIsolated(matrix, i, j)) { return true; } } } return false; } // Recursive function to generate all possible matrices unsigned long long countUnholy(int matrix[SIZE][SIZE], int row, int col) { if(col == SIZE) { col = 0; row++; } if(row == SIZE) { // If we've filled the matrix, check if it's unholy return !hasIsolatedOnes(matrix); } unsigned long long count = 0; // Try putting 0 matrix[row][col] = 0; count += countUnholy(matrix, row, col + 1); // Try putting 1 matrix[row][col] = 1; count += countUnholy(matrix, row, col + 1); return count; } int main() { int matrix[SIZE][SIZE] = {0}; unsigned long long result = countUnholy(matrix, 0, 0); printf("Number of unholy 8x8 matrices: %llu\n", result); 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.
Here's a C program that counts unholy 8x8 binary matrices using backtracking:
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a position has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // Check all cells up to current position for(int i = 0; i <= currentRow; i++) { int maxCol = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= maxCol; j++) { if (matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if (row == SIZE) { count++; return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a position has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // Check all cells up to current position for(int i = 0; i <= currentRow; i++) { int maxCol = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= maxCol; j++) { if (matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if (row == SIZE) { count++; return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 unsigned long long count = 0; // Check if a position has any adjacent 1s in cardinal directions bool hasAdjacent(int matrix[SIZE][SIZE], int row, int col) { // Check up if (row > 0 && matrix[row-1][col] == 1) return true; // Check down if (row < SIZE-1 && matrix[row+1][col] == 1) return true; // Check left if (col > 0 && matrix[row][col-1] == 1) return true; // Check right if (col < SIZE-1 && matrix[row][col+1] == 1) return true; return false; } // Check if current partial matrix is valid bool isValid(int matrix[SIZE][SIZE], int currentRow, int currentCol) { // Check all cells up to current position for(int i = 0; i <= currentRow; i++) { int maxCol = (i == currentRow) ? currentCol : SIZE-1; for(int j = 0; j <= maxCol; j++) { if (matrix[i][j] == 1 && !hasAdjacent(matrix, i, j)) { return false; } } } return true; } void generateMatrices(int matrix[SIZE][SIZE], int row, int col) { // If we've filled the entire matrix if (row == SIZE) { count++; return; } // Calculate next position int nextRow = (col == SIZE-1) ? row + 1 : row; int nextCol = (col == SIZE-1) ? 0 : col + 1; // Try placing 0 matrix[row][col] = 0; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } // Try placing 1 matrix[row][col] = 1; if (isValid(matrix, row, col)) { generateMatrices(matrix, nextRow, nextCol); } } int main() { int matrix[SIZE][SIZE] = {0}; generateMatrices(matrix, 0, 0); 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