#includeiostream#includestdio.h//±ê×¼ÊäÈëÊä³öÍ·Îļþ#includeconio.h//°üº¬getch()µÄÍ·Îļþ#includestdlib.h//°üº¬rand()µÄÍ·Îļþ#includeassert.h//°üº¬assert()µÄÍ·Îļþ#includetime.h//°üº¬srand()µÄÍ·Îļþ#includewindows.husingnamespacestd;//Îå¸öÈ«¾Ö±äÁ¿Êý×éinta[10][10];//ÓÃÀ´½ÓÊÕÊäÈëÊý¾ÝµÄÊý×éintsd[82];//´¦ÀíÌâÄ¿ÒÔ¼°±£´æ×îÖÕ½á¹ûintfix[82];//¼Ç¼ÄÄЩλÖÃÊÇÈ·¶¨µÄ£¬È·¶¨Îª1£¬·ñÔòΪ0intpossible[82][10];//¼Ç¼ËùÓÐδȷ¶¨Êý×ֵĿÉÄÜÐÔintstack[82];//ÓÃÀ´·ÅÖÃÌîÈëµÄÊýµÄÕ»inthidden[82];//´æ´¢ÄĸöλÖõÄÊý±»Òþ²Øvoidclssd();//³õʼ»¯º¯Êý£¬ËùÓÐλÖÃÉèΪ0voidrandom();//Ìî9¸öËæ»úÖµÌîÈësd[1]~sd[9],b[10]voidtransform_to_a();//sd[i]-a[line][row]µÄת»»inttransform_to_line(inti);//ʵÏÖsd[i]-a[line][row]Ö®¼äµÄת»»inttransform_to_row(inti);//ʵÏÖsd[i]-a[line][row]Ö®¼äµÄת»»intcalculate();//Ëã³ö´ð°¸voidpreDo();//Ô¤´¦ÀívoidsetPb();//¿ØÖÆpossibleÊý×évoidhide();//ÕÚÕÖ£¬½«´ð°¸ÖÐһЩÊýÖµÕÚסvoidqishi();voidshow();voidinput();voidcheck();voidmake_problem();voidprintAll();voidclssd()//³õʼ»¯º¯Êý£¬ËùÓÐλÖÃÉèΪ0{inti,j,k;for(i=0;i9;i++)for(j=0;j9;j++)a[i][j]=0;for(k=1;k=81;k++)sd[k]=0;}intline(intline,intvalue)//¼ìÑéÐÐ{inti;for(i=1;i=9;i++){if(a[line][i]==value)return0;}return1;}introw(introw,intvalue)//¼ìÑéÁÐ{inti;for(i=1;i=9;i++){if(a[i][row]==value)return0;}return1;}intsquare(intline,introw,intvalue)//¼ìÑé3*3µÄ¾Å¹¬{intL,R,i,j;L=(line%3!=0)+line/3;//L±íʾËùÔھŹ¬µÄÐÐÊýR=(row%3!=0)+row/3;//R±íʾËùÔھŹ¬µÄÁÐÊýfor(i=(L-1)*3+1;i=L*3;i++){for(j=(R-1)*3+1;j=R*3;j++)if(a[i][j]==value)return0;}return1;}//Ëĸöת»»º¯Êýinttransform_to_line(inti)//ʵÏÖsd[i]-a[line][row]Ö®¼äµÄת»»{intline;line=i/9+(i%9!=0);returnline;}inttransform_to_row(inti)//ʵÏÖsd[i]-a[line][row]Ö®¼äµÄת»»{introw;row=i%9+9*(i%9==0);returnrow;}voidtransform_to_a(inti)//sd[i]-a[line][row]µÄת»»{intl,r;l=transform_to_line(i);r=transform_to_row(i);a[l][r]=sd[i];}//Èý¸öÖØÒªº¯ÊýboolbeExist(inti,intj)//ÅжÏsdÊý×éÖÐλÖÃΪiµÄÔªËØÊÇ·ñ´æÔÚ{intl,r;l=transform_to_line(i);r=transform_to_row(i);if(line(l,j)*row(r,j)*square(l,r,j)==0)return1;elsereturn0;}voidsetPb()//¿ØÖÆpossibleÊý×é{inti,j;for(i=1;i=81;i++){for(j=1;j=9;j++){if(sd[i]!=0)possible[i][j]=-1;//Èç¹ûsd[i]ΪÒÑÖªÊäÈëÊý¾Ý£¬Ôò½«¿ÉÄÜÖµÉèΪ-1elseif(beExist(i,j)){possible[i][j]=-1;}//ÈôÔÚÐС¢ÁС¢¾Å¹¬ÄÚ£¬´æÔÚÏàͬÊýÖµÔòpossibleÉèΪ-1else{possible[i][j]=j;}//ÆäËûÇé¿ö¿ÉÄÜÖµ±£´æ½øpossibleÊý×é}}}boolfixAll(intsd[],intfix[],intpossible[82][10])//ÓÃk±ê¼Çÿһ¸öûÌîµÄ¿Õ¸ñÉÏÓм¸¸ö¿ÉÄܵÄÖµ£¬Èç¹û¶¼ÎªÒ»¸öµÄ»°£¬¾ÍÊÇÊý¶À½á¹û{inti,j;intk[82];for(i=1;i=81;i++)//½«ÒѾ´æÔÚµÄÊý¾Ý¶ÔÓ¦fixÊý×éÖеÄÖµÉèΪ1£¬²»´æÔÚÉèΪ0{if(sd[i]!=0){fix[i]=1;}else{fix[i]=0;}}for(i=1;i=81;i++)//ÅжÏδȷ¶¨¿Õ¸ñ´¦ÖµµÄ¿ÉÄÜÐÔ{if(sd[i]!=0)continue;if(fix[i]==0){for(j=1;j=9;j++)if(possible[i][j]!=-1)//¿ÉÄÜÌîÈëk[i]++;}if(k[i]==1)//Èç¹û´æÔÚÖ»ÓÐÒ»ÖÖ¿ÉÄܵÄλÖÃÔò½«fix[i]¸ÄΪ1fix[i]=1;sd[i]=possible[i][j];}for(i=1;i=81;i++)//ÅжÏÊÇ·ñ´æÔÚÖ»ÓÐÒ»ÖÖ¿ÉÄܵÄλÖã¬ÈôûÓзµ»Ø0{if(k[i]==1)return1;elsereturn0;}}//ÅжÏÊÇ·ñÍê³ÉintisFull(intsd[]){inti;for(i=1;i=81;i++)if(sd[i]==0)return0;return1;}voidpreDo()//Ô¤´¦Àí{while(1){setPb();if(!fixAll(sd,fix,possible))//¼´²»´æÔÚÖ»ÓÐÒ»ÖÖ¿ÉÄÜÐÔµÄλÖÃbreak;if(isFull(sd))//ÈôÒѾÍƳöÈ«²¿½á¹ûbreak;}}intcalculate()//ÌîÊý¶À{preDo();//Ô¤´¦Àí,ÕÒ³öËùÓеÄλÖõĿÉÄÜÊýÖµif(isFull(sd))return1;inttop=0;//½«ËùÓÐΪ0µÄλÖÃÈëÕ»for(inti=1;i82;i++){if(fix[i]==0)stack[top++]=i;}intmax=top;//¼Ç¼×î´óÊýÄ¿¼Ó1top=0;//Ö¸ÏòÕ»¶¥inttemp;boolflag=true;//¸Ã±ê־λ˵Ã÷Á˵±Ç°ÊÇÕý³£ÈëÕ»while(1){assert(top=0);//ºê¶¨Ò壬µ÷ÊÔ³ÌÐòÖ®ÓÃif(flag){intj;for(j=1;j=9;j++)if(possible[stack[top]][j]!=-1&&!beExist(stack[top],j))//ÈôtopËùʾµÄλÖÃÉÏ£¬¿ÉÒÔ°²²åjÕâ¸öÊýÖµ£¬Ôò{fix[stack[top]]=1;sd[stack[top]]=j;transform_to_a(stack[top]);//ʵÏÖa[line][row]Óësd[i]ͬ²½±ä»¯++top;if(top=max)return1;break;}if(j9)//¸Ã¿ÕËùÓпÉÄÜÖµ¶¼²»¿ÉÒÔ£¬ÔòÍËÒ»¸ñ{--top;if(top0){printAll();return0;}flag=false;}}else{if(sd[stack[top]]==9)//˵Ã÷µ±Ç°Õâ¸ötopҲûÓпÉÒÔÑ¡ÔñµÄÊý£¬¼ÌÐø»ØÍË{fix[stack[top]]=0;sd[stack[top]]=0;transform_to_a(stack[top]);--top;if(top0){printAll();return0;}}else{temp=sd[stack[top]];temp++;while(possible[stack[top]][temp]==-1||beExist(stack[top],temp)){temp++;if(temp9)break;}if(temp9)//µ±Ç°½ÚµãûÓиü»»µÄ¿ÉÄÜÐÔ£¬¼ÌÐøÍË»Ø{fix[stack[top]]=0;sd[stack[top]]=0;transform_to_a(stack[top]);--top;if(top0){printAll();return0;}}else{sd[stack[top]]=temp;transform_to_a(stack[top]);++top;flag=true;//ÖØÐÂÉ趨flag}}}}}voidrandom()//´Ó1-9ÖÐËæ»ú²úÉú¼¸¸öÖµÊäÈësd[1]ÖÁsd[9]{inti;for(i=1;i10;i++){sd[i]=i;transform_to_a(i);}}//Êä³öº¯ÊývoidprintAll(){intjishu=1;system(cls);//ÇåÆÁ×¢Ò⣺ÌâÄ¿ÖÐ0´ú±í´ýÌîÊý¾Ýcout123456789endl;cout_____________________________________________endl;for(inti=1;i=81;i++){if(i%9==1){if(jishu==2)couta-ÊäÈëÖ¸¶¨Î»ÖÃjishu++©¦;elseif(jishu==3)coutb-¼ì²é½á¹ûjishu++©¦;elseif(jishu==4)coutc-²é¿´½á¹ûjishu++©¦;elseif(jishu==5)coutd-ÔÙÀ´Ò»¾Öjishu++©¦;elseif(jishu==6)coute-·µ»Øjishu++©¦;elsecoutjishu++©¦;}coutsd[i]©¦;if(i%9==0){coutendl;cout_____________________________________________endl;}}}voidshowhidden(){intjishu=1;system(cls);//ÇåÆÁ×¢Ò⣺ÌâÄ¿ÖÐ0´ú±í´ýÌîÊý¾Ý