본문 바로가기

IT

스도쿠 판별 // 이게 완성본이 아닌건 함정

#include <stdlib.h>

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

#define MAX_SIZE 10


typedef struct

{

int row;

int column;

} parameters;


int success[15]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};


void *threading(void *data)

{

}


void *hor_test(int a[MAX_SIZE][MAX_SIZE], int start_point, int p)

{

int i = 0, j = 0, sum, check, y = 0, count, test=1;

for (i = start_point; i < start_point + 3; i++)

{

sum = 0;

for (j = 1; j < MAX_SIZE; j++)

{

sum+=a[i][j];

if(a[i][j]<=0)

test=0;

}


if(sum != 45)

success[p]=0;

}

for(i=start_point;i<start_point+3;i++)

{

count = 0;

for(y=1;y<MAX_SIZE;y++)

{

check = a[i][y];

for(j=1;j<MAX_SIZE;j++)

{

if(check==a[i][j])

count++;

}

}

if(count!=9)

success[p]=0;

}

if(test==0) 

success[p]=0;

}


void *vert_test(int a[MAX_SIZE][MAX_SIZE], int start_point, int p)

{

int i = 0, j = 0, sum, check, count, x=0, test=1;


for (j = start_point; j < start_point + 3; j++)

{

sum = 0;

for (i = 1; i < MAX_SIZE; i++)

{

sum+=a[i][j];

if(a[i][j]<=0)

test =0;

}

if (sum != 45)

success[p]=0;

}

for(j=start_point;j<start_point+3;j++)

{

count=0;

for(x=1;x<MAX_SIZE;x++)

{

check = a[x][j];

for(i=1;i<MAX_SIZE;i++)

{

if(check==a[i][j])

count++;

}

}

if(count!=9)

success[p]=0;

}

if(test==0)

success[p]=0;

}


void *cube_test(int a[MAX_SIZE][MAX_SIZE], int start_row, int start_col, int p)

{

int i = 0, j = 0, sum = 0, check, count = 0, x = 0, y = 0, test =1;


for(i = start_row; i < start_row+3 ; i++)

{

for(j = start_col; j < start_col+3; j++)

{

sum += a[i][j];

if(a[i][j]<=0) test=0;

}

}


for(x = 0; x < 3 ; x++)

{

for(y = 0; y < 3; y++)

{

check = a[start_row+x][start_col+y];


for(i = start_row; i < start_row+3 ; i++)

{

for(j = start_col; j < start_col+3; j++)

{

if(check == a[i][j])

count++;

}

}

}

}


if((sum != 45)||(count!=9)||(test==0))

{

success[p] = 0;

}

}


int main(int argc, char* argv[])

{

int a, b, d = 0;

int sudoku[MAX_SIZE][MAX_SIZE];

parameters *data;

FILE *file;

pthread_t tid[15];

int thread_id;


if(argc != 2)

{

fprintf(stderr,"err");

exit(1);

}


if (!(file = fopen(argv[1], "r")))

{

fprintf(stderr, "no file\n");

exit(1);

}

for (a = 0; a < MAX_SIZE ; a++)

{

for (b = 0; b < MAX_SIZE ; b++)

{

fscanf(file,"%d ",&sudoku[a][b]);

}

}

for (a = 0; a < MAX_SIZE ; a++)

{

for (b = 0; b < MAX_SIZE ; b++)

{

printf("%d ", sudoku[a][b]);

}

printf("\n");

}


printf("\n");


data = (parameters *)malloc(sizeof(parameters));

data->row = 1;

data->column = 1;


thread_id = pthread_create(&tid[0], NULL, threading, hor_test(sudoku, data->row, 0));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[1], NULL, threading, vert_test(sudoku, data->column, 3));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[2], NULL, threading, cube_test(sudoku, data->row, data->column, 6));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 1;

data->column = 4;


thread_id = pthread_create(&tid[3], NULL, threading, vert_test(sudoku, data->column,4));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[4], NULL, threading, cube_test(sudoku, data->row, data->column,7));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 1;

data->column = 7;


thread_id = pthread_create(&tid[5], NULL, threading, vert_test(sudoku, data->column,5));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[6], NULL, threading, cube_test(sudoku, data->row, data->column,8));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 4;

data->column = 1;


thread_id = pthread_create(&tid[7], NULL, threading, hor_test(sudoku, data->row,1));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[8], NULL, threading, cube_test(sudoku, data->row, data->column,9));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 7;

data->column = 1;


thread_id = pthread_create(&tid[9], NULL, threading, hor_test(sudoku, data->row,2));

if (thread_id<0) { perror("thread create err"); exit(0); }


thread_id = pthread_create(&tid[10], NULL, threading, cube_test(sudoku, data->row, data->column,10));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 4;

data->column = 4;


thread_id = pthread_create(&tid[11], NULL, threading, cube_test(sudoku, data->row, data->column,11));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 4;

data->column = 7;


thread_id = pthread_create(&tid[12], NULL, threading, cube_test(sudoku, data->row, data->column,12));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 7;

data->column = 4;


thread_id = pthread_create(&tid[13], NULL, threading, cube_test(sudoku, data->row, data->column,13));

if (thread_id<0) { perror("thread create err"); exit(0); }


data = (parameters *)malloc(sizeof(parameters));

data->row = 7;

data->column = 7;


thread_id = pthread_create(&tid[14], NULL, threading, cube_test(sudoku, data->row, data->column,14));

if (thread_id<0) { perror("thread create err"); exit(0); }


for (d = 0; d < sizeof(tid); d++)

{

pthread_join(tid[d],NULL);

}

for(d=0;d<(sizeof(success)/sizeof(int));d++)

{

if(success[d]==0)

printf("%d thread is invalid\n",d);

else if(success[d]==1)

printf("%d thread is valid!\n",d);

}


free(data);

fclose(file);

}



'IT' 카테고리의 다른 글

test3 .java  (0) 2014.05.17
ListFrame .java  (0) 2014.05.17
파일 복사하기  (0) 2014.05.14
heap?  (0) 2014.05.14
winner tree  (0) 2014.05.14