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.
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 | #include <stdio.h> #include <stdbool.h> // Function to check if a cell (i, j) is within the matrix bounds bool isSafe(int i, int j) { return (i >= 0 && i < 8 && j >= 0 && j < 8); } // Function to check if a matrix is "unholy" bool isUnholy(int matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j] == 1) { // Check for adjacent 1s in cardinal directions if (isSafe(i - 1, j) && matrix[i - 1][j] == 1) continue; if (isSafe(i + 1, j) && matrix[i + 1][j] == 1) continue; if (isSafe(i, j - 1) && matrix[i][j - 1] == 1) continue; if (isSafe(i, j + 1) && matrix[i][j + 1] == 1) continue; // If no adjacent 1s are found, the matrix is holy return false; } } } // No isolated 1s found, the matrix is unholy return true; } int main() { int matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < (1LL << 64); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the generated matrix is "unholy" if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> // Function to check if a cell (i, j) is within the matrix bounds bool isSafe(int i, int j) { return (i >= 0 && i < 8 && j >= 0 && j < 8); } // Function to check if a matrix is "unholy" bool isUnholy(int matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j] == 1) { // Check for adjacent 1s in cardinal directions if (isSafe(i - 1, j) && matrix[i - 1][j] == 1) continue; if (isSafe(i + 1, j) && matrix[i + 1][j] == 1) continue; if (isSafe(i, j - 1) && matrix[i][j - 1] == 1) continue; if (isSafe(i, j + 1) && matrix[i][j + 1] == 1) continue; // If no adjacent 1s are found, the matrix is holy return false; } } } // No isolated 1s found, the matrix is unholy return true; } int main() { int matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < (1LL << 64); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the generated matrix is "unholy" if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> // Function to check if a cell (i, j) is within the matrix bounds bool isSafe(int i, int j) { return (i >= 0 && i < 8 && j >= 0 && j < 8); } // Function to check if a matrix is "unholy" bool isUnholy(int matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j] == 1) { // Check for adjacent 1s in cardinal directions if (isSafe(i - 1, j) && matrix[i - 1][j] == 1) continue; if (isSafe(i + 1, j) && matrix[i + 1][j] == 1) continue; if (isSafe(i, j - 1) && matrix[i][j - 1] == 1) continue; if (isSafe(i, j + 1) && matrix[i][j + 1] == 1) continue; // If no adjacent 1s are found, the matrix is holy return false; } } } // No isolated 1s found, the matrix is unholy return true; } int main() { int matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < (1LL << 64); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the generated matrix is "unholy" if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); return 0; } |
Total number of unholy 8x8 matrices: 0
main.c: In function 'main':
main.c:35:36: warning: left shift count >= width of type [-Wshift-count-overflow]
35 | for (long long i = 0; i < (1LL << 64); i++) {
| ^~
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.
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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is within the matrix bounds bool isValidCell(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a 1 at cell (row, col) is isolated in matrix 'mat' bool isIsolated(int mat[SIZE][SIZE], int row, int col) { int rowOffsets[] = { -1, 1, 0, 0 }; // Offsets for adjacent cells int colOffsets[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowOffsets[i]; int newCol = col + colOffsets[i]; if (isValidCell(newRow, newCol) && mat[newRow][newCol] == 1) { return false; // Not isolated, found an adjacent 1 } } return true; // Isolated, no adjacent 1s found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(int mat[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (mat[i][j] == 1 && isIsolated(mat, i, j)) { return false; // Found an isolated 1, so not unholy } } } return true; // No isolated 1s found, it's unholy } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int mat[SIZE][SIZE]; // Generate all possible combinations using bit manipulation for (unsigned long long num = 0; num < (1ULL << (SIZE * SIZE)); num++) { // Fill the matrix based on the bits of 'num' for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { mat[i][j] = (num >> (i * SIZE + j)) & 1; } } // Check if the current matrix is unholy if (isUnholy(mat)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is within the matrix bounds bool isValidCell(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a 1 at cell (row, col) is isolated in matrix 'mat' bool isIsolated(int mat[SIZE][SIZE], int row, int col) { int rowOffsets[] = { -1, 1, 0, 0 }; // Offsets for adjacent cells int colOffsets[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowOffsets[i]; int newCol = col + colOffsets[i]; if (isValidCell(newRow, newCol) && mat[newRow][newCol] == 1) { return false; // Not isolated, found an adjacent 1 } } return true; // Isolated, no adjacent 1s found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(int mat[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (mat[i][j] == 1 && isIsolated(mat, i, j)) { return false; // Found an isolated 1, so not unholy } } } return true; // No isolated 1s found, it's unholy } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int mat[SIZE][SIZE]; // Generate all possible combinations using bit manipulation for (unsigned long long num = 0; num < (1ULL << (SIZE * SIZE)); num++) { // Fill the matrix based on the bits of 'num' for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { mat[i][j] = (num >> (i * SIZE + j)) & 1; } } // Check if the current matrix is unholy if (isUnholy(mat)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is within the matrix bounds bool isValidCell(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a 1 at cell (row, col) is isolated in matrix 'mat' bool isIsolated(int mat[SIZE][SIZE], int row, int col) { int rowOffsets[] = { -1, 1, 0, 0 }; // Offsets for adjacent cells int colOffsets[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowOffsets[i]; int newCol = col + colOffsets[i]; if (isValidCell(newRow, newCol) && mat[newRow][newCol] == 1) { return false; // Not isolated, found an adjacent 1 } } return true; // Isolated, no adjacent 1s found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(int mat[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (mat[i][j] == 1 && isIsolated(mat, i, j)) { return false; // Found an isolated 1, so not unholy } } } return true; // No isolated 1s found, it's unholy } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int mat[SIZE][SIZE]; // Generate all possible combinations using bit manipulation for (unsigned long long num = 0; num < (1ULL << (SIZE * SIZE)); num++) { // Fill the matrix based on the bits of 'num' for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { mat[i][j] = (num >> (i * SIZE + j)) & 1; } } // Check if the current matrix is unholy if (isUnholy(mat)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); return 0; } |
Total number of unholy 8x8 matrices: 0
main.c: In function 'countUnholyMatrices':
main.c:46:50: warning: left shift count >= width of type [-Wshift-count-overflow]
46 | for (unsigned long long num = 0; num < (1ULL << (SIZE * SIZE)); num++) {
| ^~
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.
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 <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0) && (row < SIZE) && (col >= 0) && (col < SIZE); } // Function to check if a '1' at matrix[row][col] is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { int rowDir[] = { -1, 1, 0, 0 }; // Directions for up, down, left, right int colDir[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowDir[i]; int newCol = col + colDir[i]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { return false; // Not isolated, found an adjacent '1' } } return true; // No adjacent '1' found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(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 false; // Found an isolated '1', not unholy } } } return true; // No isolated '1' found } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int matrix[SIZE][SIZE]; // Iterate through all possible combinations using bit manipulation for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Fill the matrix based on the bits of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { int index = row * SIZE + col; matrix[row][col] = (i >> index) & 1; } } if (isUnholy(matrix)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0) && (row < SIZE) && (col >= 0) && (col < SIZE); } // Function to check if a '1' at matrix[row][col] is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { int rowDir[] = { -1, 1, 0, 0 }; // Directions for up, down, left, right int colDir[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowDir[i]; int newCol = col + colDir[i]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { return false; // Not isolated, found an adjacent '1' } } return true; // No adjacent '1' found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(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 false; // Found an isolated '1', not unholy } } } return true; // No isolated '1' found } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int matrix[SIZE][SIZE]; // Iterate through all possible combinations using bit manipulation for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Fill the matrix based on the bits of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { int index = row * SIZE + col; matrix[row][col] = (i >> index) & 1; } } if (isUnholy(matrix)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 <stdbool.h> #define SIZE 8 // Function to check if a given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0) && (row < SIZE) && (col >= 0) && (col < SIZE); } // Function to check if a '1' at matrix[row][col] is isolated bool isIsolated(int matrix[SIZE][SIZE], int row, int col) { int rowDir[] = { -1, 1, 0, 0 }; // Directions for up, down, left, right int colDir[] = { 0, 0, -1, 1 }; for (int i = 0; i < 4; i++) { int newRow = row + rowDir[i]; int newCol = col + colDir[i]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { return false; // Not isolated, found an adjacent '1' } } return true; // No adjacent '1' found } // Function to check if a matrix is 'unholy' (no isolated 1s) bool isUnholy(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 false; // Found an isolated '1', not unholy } } } return true; // No isolated '1' found } // Function to generate all possible 8x8 binary matrices and count unholy ones int countUnholyMatrices() { int count = 0; int matrix[SIZE][SIZE]; // Iterate through all possible combinations using bit manipulation for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Fill the matrix based on the bits of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { int index = row * SIZE + col; matrix[row][col] = (i >> index) & 1; } } if (isUnholy(matrix)) { count++; } } return count; } int main() { int unholyCount = countUnholyMatrices(); printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); return 0; } |
Total number of unholy 8x8 matrices: 0
main.c: In function 'countUnholyMatrices':
main.c:45:46: warning: left shift count >= width of type [-Wshift-count-overflow]
45 | for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) {
| ^~
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.
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 given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { // If a cell is 1, check if it's isolated if (matrix[i][j]) { bool isolated = true; // Check 4 cardinal directions int directions[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int k = 0; k < 4; k++) { int newRow = i + directions[k][0]; int newCol = j + directions[k][1]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { isolated = false; break; } } if (isolated) { return false; // Found an isolated 1, matrix is not unholy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[SIZE][SIZE]; unsigned long long unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { matrix[row][col] = (i >> (row * SIZE + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy matrices: %llu\n", unholyCount); 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 given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { // If a cell is 1, check if it's isolated if (matrix[i][j]) { bool isolated = true; // Check 4 cardinal directions int directions[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int k = 0; k < 4; k++) { int newRow = i + directions[k][0]; int newCol = j + directions[k][1]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { isolated = false; break; } } if (isolated) { return false; // Found an isolated 1, matrix is not unholy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[SIZE][SIZE]; unsigned long long unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { matrix[row][col] = (i >> (row * SIZE + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy matrices: %llu\n", unholyCount); 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 given cell (row, col) is valid in the matrix bool isValid(int row, int col) { return (row >= 0 && row < SIZE && col >= 0 && col < SIZE); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[SIZE][SIZE]) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { // If a cell is 1, check if it's isolated if (matrix[i][j]) { bool isolated = true; // Check 4 cardinal directions int directions[][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; for (int k = 0; k < 4; k++) { int newRow = i + directions[k][0]; int newCol = j + directions[k][1]; if (isValid(newRow, newCol) && matrix[newRow][newCol]) { isolated = false; break; } } if (isolated) { return false; // Found an isolated 1, matrix is not unholy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[SIZE][SIZE]; unsigned long long unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) { // Generate the matrix from the binary representation of 'i' for (int row = 0; row < SIZE; row++) { for (int col = 0; col < SIZE; col++) { matrix[row][col] = (i >> (row * SIZE + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy matrices: %llu\n", unholyCount); return 0; } |
Total number of unholy matrices: 0
main.c: In function 'main':
main.c:45:46: warning: left shift count >= width of type [-Wshift-count-overflow]
45 | for (unsigned long long i = 0; i < (1ULL << (SIZE * SIZE)); i++) {
| ^~
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.
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 | #include <stdio.h> #include <stdbool.h> #include <math.h> // Function to check if a given cell (row, col) is valid in an 8x8 matrix bool isValid(int row, int col) { return (row >= 0 && row < 8 && col >= 0 && col < 8); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j]) { // If the cell is a 1 // Check for adjacent 1s in cardinal directions if ((isValid(i - 1, j) && matrix[i - 1][j]) || (isValid(i + 1, j) && matrix[i + 1][j]) || (isValid(i, j - 1) && matrix[i][j - 1]) || (isValid(i, j + 1) && matrix[i][j + 1])) { continue; // Not isolated } else { return false; // Isolated 1 found, matrix is holy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < pow(2, 64); i++) { // Convert the binary representation of 'i' into the matrix for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> #include <math.h> // Function to check if a given cell (row, col) is valid in an 8x8 matrix bool isValid(int row, int col) { return (row >= 0 && row < 8 && col >= 0 && col < 8); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j]) { // If the cell is a 1 // Check for adjacent 1s in cardinal directions if ((isValid(i - 1, j) && matrix[i - 1][j]) || (isValid(i + 1, j) && matrix[i + 1][j]) || (isValid(i, j - 1) && matrix[i][j - 1]) || (isValid(i, j + 1) && matrix[i][j + 1])) { continue; // Not isolated } else { return false; // Isolated 1 found, matrix is holy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < pow(2, 64); i++) { // Convert the binary representation of 'i' into the matrix for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); 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 | #include <stdio.h> #include <stdbool.h> #include <math.h> // Function to check if a given cell (row, col) is valid in an 8x8 matrix bool isValid(int row, int col) { return (row >= 0 && row < 8 && col >= 0 && col < 8); } // Function to check if a matrix is "unholy" bool isUnholy(bool matrix[8][8]) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (matrix[i][j]) { // If the cell is a 1 // Check for adjacent 1s in cardinal directions if ((isValid(i - 1, j) && matrix[i - 1][j]) || (isValid(i + 1, j) && matrix[i + 1][j]) || (isValid(i, j - 1) && matrix[i][j - 1]) || (isValid(i, j + 1) && matrix[i][j + 1])) { continue; // Not isolated } else { return false; // Isolated 1 found, matrix is holy } } } } return true; // No isolated 1s found, matrix is unholy } int main() { bool matrix[8][8]; int unholyCount = 0; // Iterate through all possible 8x8 binary matrices for (long long i = 0; i < pow(2, 64); i++) { // Convert the binary representation of 'i' into the matrix for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { matrix[row][col] = (i >> (row * 8 + col)) & 1; } } // Check if the matrix is unholy and increment the count if (isUnholy(matrix)) { unholyCount++; } } printf("Total number of unholy 8x8 matrices: %d\n", unholyCount); return 0; } |
Timeout: function took too long to complete
Testing if the previous output contains the string 1121231537486377866
: False