http://blog.sina.com.cn/s/blog_51cea4040100gxn1.html 下面内容是此链接的作者发给我的,可能是ccy自己写的也可能是她的朋友写的。
QQ及邮箱:1 4 2 3 1 7 3 7 8 3 @qq.com 欢迎吹毛求疵。
//fenlan那题约19ms
#include <iostream>
#include<time.h>
#include<cstdlib>
#define s 3
#define ss s*s
using namespace std;
int h[ss][ss],ans=0;
bool b[ss][ss+1],c[ss][ss+1],d[ss][ss+1];
int a[9][9]={
8,0,0,0,0,0,0,0,0,
0,0,3,6,0,0,0,0,0,
0,7,0,0,9,0,2,0,0,
0,5,0,0,0,7,0,0,0,
0,0,0,0,4,5,7,0,0,
0,0,0,1,0,0,0,3,0,
0,0,1,0,0,0,0,6,8,
0,0,8,5,0,0,0,1,0,
0,9,0,0,0,0,4,0,0};
void make(int x,int y,int i,bool z)
{
b[x][i]=z;
c[y][i]=z;
d[x/s*s+y/s][i]=z;
}
bool work(int x,int y,int z)
{
int i;
if (y==9)
x++,y=0;
if (x==9)
{
ans=z;
return(true);
}
if (a[x][y])
{
if(work(x,y+1,z+a[x][y]*h[x][y]))
return(true);
else return(false);
}
else
{
for (i=1;i<=ss;i++)
if (!b[x][i]&&!c[y][i]&&!d[x/s*s+y/s][i])
{
a[x][y]=i;
make(x,y,i,1);
if(work(x,y+1,z+i*h[x][y]))
return(true);
make(x,y,i,0);
}
a[x][y]=0;
return(false);
}
}
void score()
{
int i,j;
for (i=0;i<ss;i++)
for (j=0;j<ss;j++)
{
if (i==0||j==0||i==8||j==8)
h[i][j]=6;
else if (i==1||j==1||i==7||j==7)
h[i][j]=7;
else if (i==2||j==2||i==6||j==6)
h[i][j]=8;
else if (i==3||j==3||i==5||j==5)
h[i][j]=9;
else
h[i][j]=10;
}
}
int main()
{
long time1,time2;
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
memset(d,0,sizeof(d));
score();
time1=clock();
for (int i=0;i<ss;i++)
for (int j=0;j<ss;j++)
{
//scanf("%d",&a[i][j]);
if (a[i][j])
make(i,j,a[i][j],1);
}
work(0,0,0);
time2=clock();
if (!ans)
printf("No answer!\n");
else
printf("%d\n",ans);
printf("%d\n",time2-time1);;
system("pause");
}