2 条题解
-
2赵青海 (huhe) LV 7 SU @ 2021-8-7 21:07:53
C++ :
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> using namespace std; bool h[9][9],l[9][9],q[10][9]; int s[9][9]; struct C{ int no; int num; }c[9]; int ans = 0; bool cmp(C a,C b){ return a.num < b.num; } int count(int x, int y){ if(x==4&&y==4) return 10; else if(x>=3&&x<=5&&y>=3&&y<=5) return 9; else if(x>=2&&x<=6&&y>=2&&y<=6) return 8; else if(x>=1&&x<=7&&y>=1&&y<=7) return 7; else if(x>=0&&x<=8&&y>=0&&y<=8) return 6; } int space(int x,int y) { if(x>=0&&x<3&&y>=0&&y<3) return 1; if(x>=0&&x<3&&y>=3&&y<6) return 2; if(x>=0&&x<3&&y>=6&&y<9) return 3; if(x>=3&&x<6&&y>=0&&y<3) return 4; if(x>=3&&x<6&&y>=3&&y<6) return 5; if(x>=3&&x<6&&y>=6&&y<9) return 6; if(x>=6&&x<9&&y>=0&&y<3) return 7; if(x>=6&&x<9&&y>=3&&y<6) return 8; if(x>=6&&x<9&&y>=6&&y<9) return 9; } int tot(){ int sum = 0; for(int i=0;i<9;i++) for(int j=0;j<9;j++){ sum += s[i][j]*count(i,j); } return sum ; } void dfs(int x,int y) { int xx = c[x].no; if(s[xx][y]!=0) { if(x==8&&y==8) { int sum = tot(); if(sum>ans) ans = sum; return ; } if(y==8) dfs(x+1,0); else dfs(x,y+1); } else { for(int i=1; i<=9; i++) { if(!h[xx][i]&&!l[y][i]&&!q[space(xx,y)][i]) { s[xx][y] = i; h[xx][i] = 1; l[y][i] = 1; q[space(xx,y)][i] = 1; dfs(x,y); s[xx][y] = 0; h[xx][i] = 0; l[y][i] = 0; q[space(xx,y)][i] = 0; } } } } int main() { int points = 0; for(int i=0; i<9; i++) for(int j=0; j<9; j++) { cin>>s[i][j]; if(s[i][j]!=0) { h[i][s[i][j]] = 1; l[j][s[i][j]] = 1; q[space(i,j)][s[i][j]] = 1; } else{ c[i].num++ ; c[i].no = i; } } sort(c,c+9,cmp); dfs(0,0); if(ans) cout<<ans<<endl; else cout<<-1<<endl; return 0; }
-
02023-5-24 19:18:31@
建议使用该AC题解
#include <bits/stdc++.h> using namespace std; int num,ans=-1,cur; int G[10][10],rec[512],power[512]; int row[10],col[10],grid[10]; const int grade[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 g(int x,int y) { return x/3*3+y/3; } void flip(int x,int y,int val) { row[x]^=1<<(val-1); col[y]^=1<<(val-1); grid[g(x,y)]^=1<<(val-1); } void dfs(int now,int sum) { if(sum+now*9*10<=ans) return; if(now==0) { ans=max(ans,sum); return; } int minn=10,x,y; for(int i=0;i<9;i++) { for(int j=0;j<9;j++) { if(G[i][j]) continue; int val=row[i]&col[j]&grid[g(i,j)]; if(rec[val]<minn) minn=rec[val],x=i,y=j; } } int val=row[x]&col[y]&grid[g(x,y)]; for(;val;val-=val&-val) { int k=power[val&-val]; G[x][y]=k; flip(x,y,k); dfs(now-1,sum+k*grade[x][y]); G[x][y]=0; flip(x,y,k); } } int main() { for(int i=1;i<1<<9;i++) for(int j=i;j;j-=j&-j) rec[i]++; for(int i=0;i<9;i++) { row[i]=col[i]=grid[i]=(1<<9)-1; power[1<<i]=i+1; } for(int i=0;i<9;i++) { for(int j=0;j<9;j++) { scanf("%d",&G[i][j]); if(G[i][j]==0) num++; else flip(i,j,G[i][j]),cur+=grade[i][j]*G[i][j]; } } dfs(num,cur); printf("%d\n",ans); return 0; }
- 1
信息
- ID
- 94
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 120
- 已通过
- 33
- 上传者