#includeiostream#includestdio.h#includestdlib.h#includestring.h#includetime.husingnamespacestd;#defineMAP_SIZE180#defineMAP_WIDTH18#defineMAP_HEIGHT10#defineROW_TOP1#defineROW_BOTTOM8#defineCOL_LEFT1#defineCOL_RIGHT16#defineTYPE8inlineintROW(intsq){returnsq/MAP_WIDTH;}inlineintCOL(intsq){returnsq%MAP_WIDTH;}inlineintSQ(intr,intc){returnr*MAP_WIDTH+c;}inlineboolSAME_ROW(intsq1,intsq2){returnROW(sq1)==ROW(sq2);}inlineboolSAME_COL(intsq1,intsq2){returnCOL(sq1)==COL(sq2);}inlineintMIN_ROW(intsq1,intsq2){returnROW(sq1)ROW(sq2)?ROW(sq1):ROW(sq2);}inlineintMAX_ROW(intsq1,intsq2){returnROW(sq1)ROW(sq2)?ROW(sq2):ROW(sq1);}inlineintMIN_COL(intsq1,intsq2){returnCOL(sq1)COL(sq2)?COL(sq1):COL(sq2);}inlineintMAX_COL(intsq1,intsq2){returnCOL(sq1)COL(sq2)?COL(sq2):COL(sq1);}voidsort(int*data,int*id,intn){inti,j;inttd,tid;for(i=0;in-1;i++){for(j=i;jn;j++){if(data[i]data[j]){td=data[i];data[i]=data[j];data[j]=td;tid=id[i];id[i]=id[j];id[j]=tid;}}}}intmap[MAP_SIZE];constintin_map[MAP_SIZE]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};intpic[TYPE][(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1)/TYPE];inttotpic=(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1);voidInitMap(){srand((unsigned)time(NULL));intrnd[(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1)];intid[(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1)];inti,n=(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1);for(i=0;in;i++){rnd[i]=rand();id[i]=i;}sort(rnd,id,n);intoffset=SQ(ROW_TOP,COL_LEFT);intr,c,p,pp[TYPE]={0};;n=0;for(r=ROW_TOP;r=ROW_BOTTOM;r++){for(c=COL_LEFT;c=COL_RIGHT;c++){p=id[n++]%TYPE;map[SQ(r,c)]=p+1;//1+id[n++]%TYPE;pic[p][pp[p]++]=SQ(r,c);}}}voidDelPic(intsq){if(sq0||sq=MAP_SIZE)return;if(in_map[sq]!=1)return;intp=map[sq],i;intn=(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1)/TYPE;for(i=0;in;i++){if(pic[p-1][i]==sq){pic[p-1][i]=0;map[sq]=0;totpic--;break;}}return;}boolline1(intsq1,intsq2){intr,c,r1,r2,c1,c2;boolflag=false;r1=MIN_ROW(sq1,sq2);r2=MAX_ROW(sq1,sq2);c1=MIN_COL(sq1,sq2);c2=MAX_COL(sq1,sq2);if(SAME_ROW(sq1,sq2)){flag=false;for(c=c1+1;cc2;c++)flag=flag||map[SQ(r1,c)];if(!flag)returntrue;}if(SAME_COL(sq1,sq2)){flag=false;for(r=r1+1;rr2;r++)flag=flag||map[SQ(r,c1)];if(!flag)returntrue;}returnfalse;}boolline2(intsq1,intsq2){intsq;sq=SQ(ROW(sq1),COL(sq2));if(map[sq]==0&&line1(sq,sq1)&&line1(sq,sq2))returntrue;sq=SQ(ROW(sq2),COL(sq1));if(map[sq]==0&&line1(sq,sq1)&&line1(sq,sq2))returntrue;returnfalse;}boolline3(intsq1,intsq2){intsq,edge;sq=sq1-1;//leftedge=SQ(ROW(sq1),0);while(map[sq]==0&&sq=edge){if(line2(sq,sq2))returntrue;elsesq--;}sq=sq1+1;//rightedge=SQ(ROW(sq1),MAP_WIDTH-1);while(map[sq]==0&&sq=edge){if(line2(sq,sq2))returntrue;elsesq++;}sq=sq1-MAP_WIDTH;//upedge=SQ(0,COL(sq1));while(map[sq]==0&&sq=edge){if(line2(sq,sq2))returntrue;elsesq-=MAP_WIDTH;}sq=sq1+MAP_WIDTH;//downedge=SQ(MAP_HEIGHT-1,COL(sq1));while(map[sq]==0&&sq=edge){if(line2(sq,sq2))returntrue;elsesq+=MAP_WIDTH;}returnfalse;}boolMatch(intsq1,intsq2){if(line1(sq1,sq2))returntrue;if(line2(sq1,sq2))returntrue;if(line3(sq1,sq2))returntrue;returnfalse;}intPrompt(int*sq){inttype,n,i,j,sq1,sq2;n=(ROW_BOTTOM-ROW_TOP+1)*(COL_RIGHT-COL_LEFT+1)/TYPE;for(type=1;type=TYPE;type++){for(i=0;in;i++){sq1=pic[type-1][i];if(sq1==0)continue;for(j=i+1;jn;j++){sq2=pic[type-1][j];if(sq2==0)continue;if(Match(sq1,sq2)){sq[0]=sq1;sq[1]=sq2;returntype;}}}}return-1;}intmain(){inti,j;InitMap();//map[38]=map[0]=0;//memcpy(map,in_map,MAP_SIZE*sizeof(int));for(i=0;iMAP_SIZE;i++){printf(%2d,map[i]);if(i%MAP_WIDTH==MAP_WIDTH-1)printf(\n);}printf(\n);for(i=0;iTYPE;i++){for(j=0;j16;j++){printf(%-3d,pic[i][j]);}printf(\n);}printf(%d%d\n,Match(20,23),Match(24,27));printf(%d%d\n,Match(57,94),Match(155,159));intsq[2],type;type=Prompt(sq);while(type!=-1){printf(%-2d:%-3d,%-3d:%3d\n,type,sq[0],sq[1],totpic);DelPic(sq[0]);DelPic(sq[1]);type=Prompt(sq);}printf(totpic:%d\n,totpic);return0;}