Untitled
unknown
plain_text
a year ago
1.6 kB
4
Indexable
#include <stdio.h> #define MAX_N 24 #define MAX_K 100 int N, L, K; int board[MAX_N][MAX_N]; int num_solutions = 0; int isSafe(int row, int col) { for (int i = 0; i < N; i++) { if (board[row][i] || board[i][col]) return 0; } return 1; } void solve(int placed, FILE *output) { if (placed == L) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (board[i][j]) { fprintf(output, "(%d,%d)", i, j); if (i != N - 1 || j != N - 1) fprintf(output, " "); } } } fprintf(output, "\n"); num_solutions++; return; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (isSafe(i, j)) { board[i][j] = 1; solve(placed + 1, output); board[i][j] = 0; } } } } int main() { FILE *input = fopen("input.txt", "r"); FILE *output = fopen("output.txt", "w"); if (!input || !output) { printf("Ошибка при открытии файлов.\n"); return 1; } fscanf(input, "%d %d %d", &N, &L, &K); for (int i = 0; i < MAX_N; i++) { for (int j = 0; j < MAX_N; j++) { board[i][j] = 0; } } for (int i = 0; i < K; i++) { int x, y; fscanf(input, "%d %d", &x, &y); board[x][y] = 1; } solve(0, output); if (num_solutions == 0) { fprintf(output, "no solutions\n"); } fclose(input); fclose(output); return 0; }
Editor is loading...
Leave a Comment