/*------------------------------------------------*/ /* Copyright(C) T.Nakamura 2001 */ /* E-Mail: nakamura (at) tokyo.email.ne.jp */ /*------------------------------------------------*/ /* pentomino */ /* */ /* usage : pento [ { "6*10" | "10*6" | */ /* "5*12" | "12*5" | */ /* "4*15" | "15*4" | */ /* "3*20" | "20*3" | */ /* "8*8" } ] */ /* */ /* default ... "6*10" */ /* */ /*------------------------------------------------*/ #include #include #include /*------------------------*/ /* define constant */ /*------------------------*/ #define PIECE_NO 12 #define PIECE_CELL_NO 5 #define PIECE_DIRECTION 8 #define UNUSE 0 #define USE 1 #define INHIBIT -1 #define VACANT 0 #define VACANT_FLAG 99999 #define MAX_X 22 #define MAX_Y 15 #define GOOD 0 #define NG 1 /*------------------------*/ /* external variable */ /*------------------------*/ int board[MAX_X][MAX_Y]; int use_piece[PIECE_NO]; int seq_no; int size_x; int size_y; int hsize_x; int hsize_y; /*------------------------*/ void display(); struct piece_s { int x; int y; }; struct piece_org_s { struct piece_s piece[PIECE_CELL_NO]; }; void copy_piece(struct piece_org_s *target, struct piece_org_s *source) { int i; for (i=0; ipiece[i].x = source->piece[i].x; target->piece[i].y = source->piece[i].y; } } void mirror_piece(struct piece_org_s *target, struct piece_org_s *source) { int i; for (i=0; ipiece[i].x = source->piece[i].x; target->piece[i].y = -(source->piece[i].y); } } void rotate_piece(struct piece_org_s *target) { int i; int tmp; for (i=0; ipiece[i].x; target->piece[i].x = -(target->piece[i].y); target->piece[i].y = tmp; } } int sort_piece(struct piece_s *a, struct piece_s *b) { if (a->x > b->x) return(1); else if (a->x == b->x) { if (a->y > b->y) return(1); } return(-1); } void origin_piece(struct piece_org_s *target) { int i; qsort(&(target->piece[0]),PIECE_CELL_NO,sizeof(struct piece_s), sort_piece); for (i=PIECE_CELL_NO-1; i>=0; i--) { target->piece[i].x -= target->piece[0].x; target->piece[i].y -= target->piece[0].y; } } struct piece_x { int no; struct { int x[PIECE_CELL_NO-1]; int y[PIECE_CELL_NO-1]; } type[PIECE_DIRECTION]; } piece[PIECE_NO]; void entry_piece(int piece_no,struct piece_org_s *target) { int i; int same; int no; origin_piece(target); /*** DEBUG printf("%d :",i); for (i=0; ipiece[i].x,target->piece[i].y); } printf("\n"); ***/ same = 0; for (i=0; ipiece[1].x && piece[piece_no].type[i].y[0] == target->piece[1].y && piece[piece_no].type[i].x[1] == target->piece[2].x && piece[piece_no].type[i].y[1] == target->piece[2].y && piece[piece_no].type[i].x[2] == target->piece[3].x && piece[piece_no].type[i].y[2] == target->piece[3].y && piece[piece_no].type[i].x[3] == target->piece[4].x && piece[piece_no].type[i].y[3] == target->piece[4].y) { same = 1; break; } } if (same == 0) { no = piece[piece_no].no++; for (i=0; ipiece[1+i].x; piece[piece_no].type[no].y[i] = target->piece[1+i].y; } } } void dump_piece() { int i,j,k,m; int posx,posy; size_x = 9; size_y = 12; /*** init board ***/ for (i=0; i0) rotate_piece(&piece_tmp); entry_piece(i,&piece_tmp); } } } /*** Dump for DEBUG ***/ /*** for (i=0; i0) printf("%s\n",lp1); printf("%s\n",lp2); } printf("\n\n"); fflush(stdout); } int blank_no; /*------------------------*/ /* check_blank_area */ /*------------------------*/ void search_blank(int x,int y) { board[x][y] = VACANT_FLAG; blank_no++; if (board[x][y+1]==VACANT) search_blank(x,y+1); if (board[x+1][y]==VACANT) search_blank(x+1,y); if (board[x-1][y]==VACANT) search_blank(x-1,y); if (board[x][y-1]==VACANT) search_blank(x,y-1); } void search_blank_clear(int x,int y) { while(x<=size_x) { while(y<=size_y) { if (board[x][y]==VACANT_FLAG) board[x][y] = VACANT; y++; } y = 1; x++; } } /*------------------------*/ /* check_board */ /*------------------------*/ int check_board(int x,int y) { if (board[x+1][y] !=VACANT && board[x][y+1] !=VACANT) return NG; if (board[x][y+2] !=VACANT && board[x+1][y+1]!=VACANT && board[x+1][y] !=VACANT) return NG; if (board[x][y+1] !=VACANT && board[x+1][y+1]!=VACANT && board[x+2][y] !=VACANT && board[x+1][y-1]!=VACANT) return NG; blank_no =0; search_blank(x,y); search_blank_clear(x,y); if (blank_no%PIECE_CELL_NO!=0) return NG; return GOOD; } void dump_usepiece() { int i; printf("use_piece\n"); for (i=0;isize_y) { posy = 1; posx++; } } /** check board **/ if (check_board(posx, posy)) return; for (i=1; ihsize_y) { posy = 2; posx++; } } } /*------------------------*/ /* main */ /*------------------------*/ main(int argc, char **argv) { clock_t ts,time; ts = clock(); init_piece(); init_board(argc,argv); display(); search_start(); time = clock() - ts; printf("Time=%f\n",(float)time/(float)CLOCKS_PER_SEC); } /*------------------------------------------------*/ /* end of file */ /*------------------------------------------------*/