Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #define LAST_COL 4
- int n = 4, m = 4;
- int matrix[111][111];
- bool valid(int row, int col, int color) {
- if ((row > 0 && col > 0 && matrix[row - 1][col - 1] == color) ||
- (row > 0 && col < m - 1 && matrix[row - 1][col + 1] == color) ||
- (row < n - 1 && col > 0 && matrix[row + 1][col - 1] == color) ||
- (row < n - 1 && col < m - 1 && matrix[row + 1][col + 1] == color)) {
- return false;
- }
- return true;
- }
- void printMatrix() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- printf("%d ", matrix[i][j]);
- }
- puts("");
- }
- }
- void bktr(int i, int j) {
- if (i == n) {
- printMatrix();
- puts("----------------------------------");
- return;
- }
- for (int c = 1; c <= LAST_COL; c++) {
- if (valid(i, j, c)) {
- matrix[i][j] = c;
- int nxtJ = j == m ? 0 : j + 1;
- int nxtI = j == m ? i + 1 : i;
- bktr(nxtI, nxtJ);
- }
- }
- }
- int main(void) {
- bktr(0, 0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement