操作系统概念第四章编程项目-数独解决方案验证器

数独解决方案验证器

数独内容对应实验报告已给出的代码

#include <iostream>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

#define NUMBER_OF_THREADS     11
#define PUZZLE_SIZE         9

void *column_worker(void *param);     /* thread that checks columns */
void *row_worker(void *param);         /* thread that checks rows */
void *subfield_worker(void *param);     /* thread that checks subfields */

int judge = 2;

/* example puzzle */
int puzzle[PUZZLE_SIZE+1][PUZZLE_SIZE+1] = {
            {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
            {-1,5,3,4,6,7,8,9,1,2},
            {-1,6,7,2,1,9,5,3,4,8},
            {-1,1,9,8,3,4,2,5,6,7},
            {-1,8,5,9,7,6,1,4,2,3},
            {-1,4,2,6,8,5,3,7,9,1},
            {-1,7,1,3,9,2,4,8,5,6},
            {-1,9,6,1,5,3,7,2,8,4},
            {-1,2,8,7,4,1,9,6,3,5},
            {-1,3,4,5,2,8,6,1,7,9}
        };

int status_map[NUMBER_OF_THREADS] = {0};

/* data structure for passing data to threads */
typedef struct
{
    int row;
    int column;
} parameters;

int main(int argc, char *argv[])
{
    parameters *data = (parameters *)malloc(sizeof(parameters));
    data->row = 1;
    data->column = 1;
    
    pthread_t tid_row;
    pthread_t tid_column;
    pthread_t tid_subfield;
    
    pthread_create(&tid_row,NULL,row_worker,(void*)data);
    pthread_create(&tid_column,NULL,column_worker,(void*)data);
    pthread_create(&tid_subfield,NULL,subfield_worker,(void*)data);
    
    pthread_join(tid_row,NULL);
    pthread_join(tid_column,NULL);
    pthread_join(tid_subfield,NULL);
    
    if(status_map[0] == 0 && status_map[1] == 0 && status_map[2] == 0 && status_map[3] == 0 && status_map[4] == 0 && status_map[5] == 0 && status_map[6] == 0 && status_map[7] == 0 && status_map[8] == 0 && status_map[9] == 0 && status_map[10] == 0){
        printf("数独成立\n");
    }else{
        printf("数独不成立\n");
    }
    
    return 0;
}

void *row_worker(void *params){
    
    parameters *temp_row;
    temp_row = (parameters*)params;
    
    for(int i = temp_row->row;i <= 9;i++){
        int flag[10] = {0,0,0,0,0,0,0,0,0,0};
        for(int j = temp_row->column;j <= 9;j++){
            if(flag[puzzle[i][j]] == 0){
                flag[puzzle[i][j]] = 1;
            }else if(flag[puzzle[i][j]] == 1){
                printf("第%d行不满足\n",i);
                status_map[0]++;
            }
        }
    }
    pthread_exit(0);
}

void *column_worker(void *params){
    parameters *temp_column;
    temp_column = (parameters*)params;
    for(int i = temp_column->column;i <= 9;i++){
        int flag[10] = {0,0,0,0,0,0,0,0,0,0};
        for(int j = temp_column->row;j <= 9;j++){
            if(flag[puzzle[j][i]] == 0){
                flag[puzzle[j][i]] = 1;
            }else if(flag[puzzle[i][j]] == 1){
                printf("第%d列不满足\n",i);
                status_map[1]++;
            }
        }
    }
    pthread_exit(0);
}

void *subfield_worker(void *params){
    parameters *temp_sub;
    temp_sub = (parameters*)params;
    for (int i = temp_sub->row;i <= 9;i += 3){
        for (int j = temp_sub->column;j <= 9;j += 3){
            int flag[10] = {0,0,0,0,0,0,0,0,0,0};
            for (int a = 0;a < 3;a++){
                for (int b = 1;b < 3;b++){
                    if (flag[puzzle[i + a][j + b]] != 1){
                        flag[puzzle[i + a][j + b]] = 1;
                        judge++;
                    }else  if(flag[puzzle[i + a][j + b]] == 1){
                        status_map[judge++]++;
                    }
                }
            }
        }
    }
    pthread_exit(0);
}


基本功能可以实现,但有一些小bug,加油。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值