```c #include #include #include #include #include #define NUM_THREADS 13 #define SIZE 9 #define SUBGRID_SIZE 3 pthread_t checkers[NUM_THREADS]; typedef struct { int a[SIZE][SIZE]; } SudokuBoard; typedef struct { int x, y; SudokuBoard* board; } ThreadParam; typedef struct { int diag_mode; SudokuBoard* board; } DiagThreadParam; SudokuBoard board = {{{0}}}; void* check_column(void* param) { int vis[SIZE + 1]; int res = 1; SudokuBoard* b = (SudokuBoard*)param; for (int j = 0; j < SIZE; ++ j) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < SIZE; ++ i) { ++ vis[b->a[i][j]]; } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { return (void*)(intptr_t)0; } } } return (void*)(intptr_t)res; } void* check_row(void* param) { int vis[SIZE + 1]; int res = 1; SudokuBoard* b = (SudokuBoard*)param; for (int i = 0; i < SIZE; ++ i) { memset(vis, 0, sizeof(vis)); for (int j = 0; j < SIZE; ++ j) { ++ vis[b->a[i][j]]; } for (int k = 1; k <= SIZE; ++ k) { if (vis[k] != 1) { return (void*)(intptr_t)0; } } } return (void*)(intptr_t)res; } void* check_block(void* param) { ThreadParam* p = (ThreadParam*)param; int vis[SIZE + 1] = {0}; int res = 1; for (int i = p->x; i < p->x + SUBGRID_SIZE; ++ i) { for (int j = p->y; j < p->y + SUBGRID_SIZE; ++ j) { ++ vis[p->board->a[i][j]]; } } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { res = 0; break; } } free(p); return (void*)(intptr_t)res; } void* check_diag(void* param) { DiagThreadParam* diag_param = (DiagThreadParam*)param; int res = 1; int vis[SIZE + 1] = {0}; for (int i = 0; i < SIZE; ++ i) { int j = (diag_param->diag_mode) ? (SIZE - i - 1) : i; ++ vis[board.a[i][j]]; } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { res = 0; break; } } return (void*)(intptr_t)res; } int main(int argc, char* argv[]) { FILE* file = fopen(argv[1], "r"); if (file == NULL) { fprintf(stderr, "You need to provide a sudoku data file!\n"); fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } if (argc != 2) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } // Read input for (int i = 0; i < SIZE; ++ i) { for (int j = 0; j < SIZE; ++ j) { if (fscanf(file, "%d,", &board.a[i][j]) != 1) { fprintf(stderr, "Invalid input format\n"); fclose(file); return 1; } } } fclose(file); // Output the input data for (int i = 0; i < SIZE; ++ i) { for (int j = 0; j < SIZE; ++ j) { printf("%d ", board.a[i][j]); } printf("\n"); } // Create threads void* result; int final_result = 1; int thread_index = 0; // Check columns and rows pthread_create(&checkers[thread_index ++], NULL, check_column, &board); pthread_create(&checkers[thread_index ++], NULL, check_row, &board); // Check 3 x 3 subgrids for (int i = 0; i < SIZE; i += SUBGRID_SIZE) { for (int j = 0; j < SIZE; j += SUBGRID_SIZE) { ThreadParam* param = malloc(sizeof(ThreadParam)); param->x = i; param->y = j; param->board = &board; pthread_create(&checkers[thread_index ++], NULL, check_block, param); } } // Check diag int diag_mode = 0; DiagThreadParam* param = malloc(sizeof(DiagThreadParam)); param->diag_mode = diag_mode; param->board = &board; pthread_create(&checkers[thread_index ++], NULL, check_diag, ¶m); param->diag_mode = 1; pthread_create(&checkers[thread_index ++], NULL, check_diag, ¶m); // Wait for all threads to finish for (int i = 0; i < thread_index; ++ i) { pthread_join(checkers[i], &result); final_result &= (intptr_t)result; } printf("\n%s\n", final_result ? "This sudoku solution is valid" : "invalid sudoku solution"); return 0; } ``` Loading... ```c #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <string.h> #include <stdint.h> #define NUM_THREADS 13 #define SIZE 9 #define SUBGRID_SIZE 3 pthread_t checkers[NUM_THREADS]; typedef struct { int a[SIZE][SIZE]; } SudokuBoard; typedef struct { int x, y; SudokuBoard* board; } ThreadParam; typedef struct { int diag_mode; SudokuBoard* board; } DiagThreadParam; SudokuBoard board = {{{0}}}; void* check_column(void* param) { int vis[SIZE + 1]; int res = 1; SudokuBoard* b = (SudokuBoard*)param; for (int j = 0; j < SIZE; ++ j) { memset(vis, 0, sizeof(vis)); for (int i = 0; i < SIZE; ++ i) { ++ vis[b->a[i][j]]; } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { return (void*)(intptr_t)0; } } } return (void*)(intptr_t)res; } void* check_row(void* param) { int vis[SIZE + 1]; int res = 1; SudokuBoard* b = (SudokuBoard*)param; for (int i = 0; i < SIZE; ++ i) { memset(vis, 0, sizeof(vis)); for (int j = 0; j < SIZE; ++ j) { ++ vis[b->a[i][j]]; } for (int k = 1; k <= SIZE; ++ k) { if (vis[k] != 1) { return (void*)(intptr_t)0; } } } return (void*)(intptr_t)res; } void* check_block(void* param) { ThreadParam* p = (ThreadParam*)param; int vis[SIZE + 1] = {0}; int res = 1; for (int i = p->x; i < p->x + SUBGRID_SIZE; ++ i) { for (int j = p->y; j < p->y + SUBGRID_SIZE; ++ j) { ++ vis[p->board->a[i][j]]; } } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { res = 0; break; } } free(p); return (void*)(intptr_t)res; } void* check_diag(void* param) { DiagThreadParam* diag_param = (DiagThreadParam*)param; int res = 1; int vis[SIZE + 1] = {0}; for (int i = 0; i < SIZE; ++ i) { int j = (diag_param->diag_mode) ? (SIZE - i - 1) : i; ++ vis[board.a[i][j]]; } for (int i = 1; i <= SIZE; ++ i) { if (vis[i] != 1) { res = 0; break; } } return (void*)(intptr_t)res; } int main(int argc, char* argv[]) { FILE* file = fopen(argv[1], "r"); if (file == NULL) { fprintf(stderr, "You need to provide a sudoku data file!\n"); fprintf(stderr, "Usage: %s <filename>\n", argv[0]); return 1; } if (argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[0]); return 1; } // Read input for (int i = 0; i < SIZE; ++ i) { for (int j = 0; j < SIZE; ++ j) { if (fscanf(file, "%d,", &board.a[i][j]) != 1) { fprintf(stderr, "Invalid input format\n"); fclose(file); return 1; } } } fclose(file); // Output the input data for (int i = 0; i < SIZE; ++ i) { for (int j = 0; j < SIZE; ++ j) { printf("%d ", board.a[i][j]); } printf("\n"); } // Create threads void* result; int final_result = 1; int thread_index = 0; // Check columns and rows pthread_create(&checkers[thread_index ++], NULL, check_column, &board); pthread_create(&checkers[thread_index ++], NULL, check_row, &board); // Check 3 x 3 subgrids for (int i = 0; i < SIZE; i += SUBGRID_SIZE) { for (int j = 0; j < SIZE; j += SUBGRID_SIZE) { ThreadParam* param = malloc(sizeof(ThreadParam)); param->x = i; param->y = j; param->board = &board; pthread_create(&checkers[thread_index ++], NULL, check_block, param); } } // Check diag int diag_mode = 0; DiagThreadParam* param = malloc(sizeof(DiagThreadParam)); param->diag_mode = diag_mode; param->board = &board; pthread_create(&checkers[thread_index ++], NULL, check_diag, ¶m); param->diag_mode = 1; pthread_create(&checkers[thread_index ++], NULL, check_diag, ¶m); // Wait for all threads to finish for (int i = 0; i < thread_index; ++ i) { pthread_join(checkers[i], &result); final_result &= (intptr_t)result; } printf("\n%s\n", final_result ? "This sudoku solution is valid" : "invalid sudoku solution"); return 0; } ``` 最后修改:2025 年 03 月 23 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏