90 分#include#include using namespace std;int flag[99][99];int s=0;int a[9][9]={ {6,6,6,6, 6,6,6,6,6},{6,7,7,7, 7,7,7,7,6},{6,7,8,8, 8,8,8,7,6},{6,7,8,9, 9,9,8,7,6},{6,7,8,9,10,9,8,7,6},{6,7,8,9, 9,9,8,7,6},{6,7,8,8, 8,8,8,7,6},{6,7,7,7, 7,7,7,7,6},{6,6,6,6, 6,6,6,6,6}};int c[9][9]={ 1,1,1,2,2,2,3,3,3, 1,1,1,2,2,2,3,3,3, 1,1,1,2,2,2,3,3,3, 4,4,4,5,5,5,6,6,6, 4,4,4,5,5,5,6,6,6, 4,4,4,5,5,5,6,6,6, 7,7,7,8,8,8,9,9,9, 7,7,7,8,8,8,9,9,9, 7,7,7,8,8,8,9,9,9,};int b[99][99];int f[10][10];int fh[10][10];int fs[10][10];int max1=-1;int f4;void dfs(int x,int y,int ans){ if(f4==1) return; if(x==0&&y==9) { f4=1; for(int i=0;i<=8;i++) { for(int j=0;j<=8;j++) printf("%d ",b[i][j]); printf("\n"); } //max1=max(ans,max1); // return; } if(!b[x][y]) { for(int i=1;i<=9;i++) if(fh[x][i]==0&&fs[y][i]==0&&f[c[x][y]][i]==0) { b[x][y]=i; fh[x][i]=1; fs[y][i]=1; f[c[x][y]][i]=1; if(x<8) { dfs(x+1,y,ans+a[x][y]*i); } else { dfs(0,y+1,ans+a[x][y]*i); } fh[x][i]=0; fs[y][i]=0; f[c[x][y]][i]=0; b[x][y]=0; } } else { if(x<8) dfs(x+1,y,ans); else dfs(0,y+1,ans); }}int main(){ for(int i=0;i<=8;i++) for(int j=0;j<=8;j++) { scanf("%d",&b[i][j]); if(b[i][j]) { fh[i][b[i][j]]=1; fs[j][b[i][j]]=1; f[c[i][j]][b[i][j]]=1; s+=a[i][j]*b[i][j]; } } dfs(0,0,s); printf("%d",max1); return 0;}
可以解数独游戏了!!!
这个题一开始我是蛇形遍历,有点麻烦 。。。 后来看了xy的便改了sxb大神思路#include#include #include using namespace std;int s=0;int n=0;int can[10][10];//记录能填多少数 int a[9][9]={ {6,6,6,6, 6,6,6,6,6},{6,7,7,7, 7,7,7,7,6},{6,7,8,8, 8,8,8,7,6},{6,7,8,9, 9,9,8,7,6},{6,7,8,9,10,9,8,7,6},{6,7,8,9, 9,9,8,7,6},{6,7,8,8, 8,8,8,7,6},{6,7,7,7, 7,7,7,7,6},{6,6,6,6, 6,6,6,6,6}};//打表 int c[9][9]={ 1,1,1,2,2,2,3,3,3, 1,1,1,2,2,2,3,3,3, 1,1,1,2,2,2,3,3,3, 4,4,4,5,5,5,6,6,6, 4,4,4,5,5,5,6,6,6, 4,4,4,5,5,5,6,6,6, 7,7,7,8,8,8,9,9,9, 7,7,7,8,8,8,9,9,9, 7,7,7,8,8,8,9,9,9,};int b[99][99];int f[10][10];int fh[10][10];int fs[10][10];int max1=-1;int f1(int x,int y,int k){ if(!fh[x][k]&&!fs[y][k]&&!f[c[x][y]][k]) return 1; return 0;}void dfs(int X,int ans){ if(X>(81-n))//已填完 { max1=max(ans,max1); return; } memset(can,0,sizeof(can)); int x,y,mmin=99999999; for(int i=0;i<9;i++) for(int j=0;j<9;j++) if(!b[i][j]) { for(int k=1;k<=9;k++) if ( f1(i,j,k) ) can[i][j]++; if(can[i][j]
这个思路666,直接就a了otc