记录编号 459354 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [NOIP 2009]靶形数独 最终得分 100
用户昵称 Gravatarzeppoe 是否通过 通过
代码语言 C++ 运行时间 3.701 s
提交时间 2017-10-12 21:57:53 内存使用 0.29 MiB
显示代码纯文本
  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. int n,ans,rank[6],map[10][10];
  5. bool r[10][10],l[10][10],p[10][10];
  6. //手动笑哭,数组是从第零位开始存的,noip很玄啊。。。
  7. int g[10][10]={
  8. 0,0,0,0,0,0,0,0,0,0,
  9. 0,1,1,1,2,2,2,3,3,3,
  10. 0,1,1,1,2,2,2,3,3,3,
  11. 0,1,1,1,2,2,2,3,3,3,
  12. 0,4,4,4,5,5,5,6,6,6,
  13. 0,4,4,4,5,5,5,6,6,6,
  14. 0,4,4,4,5,5,5,6,6,6,
  15. 0,7,7,7,8,8,8,9,9,9,
  16. 0,7,7,7,8,8,8,9,9,9,
  17. 0,7,7,7,8,8,8,9,9,9
  18. };
  19.  
  20. int ra[10][10]={
  21. 0,0,0,0,0,0,0,0,0,0,
  22. 0,1,1,1,1,1,1,1,1,1,
  23. 0,1,2,2,2,2,2,2,2,1,
  24. 0,1,2,3,3,3,3,3,2,1,
  25. 0,1,2,3,4,4,4,3,2,1,
  26. 0,1,2,3,4,5,4,3,2,1,
  27. 0,1,2,3,4,4,4,3,2,1,
  28. 0,1,2,3,3,3,3,3,2,1,
  29. 0,1,2,2,2,2,2,2,2,1,
  30. 0,1,1,1,1,1,1,1,1,1
  31. };
  32.  
  33. int val;
  34.  
  35. int mod[100],div[100];
  36.  
  37. void dfs(int k){
  38. if(k>81) return void(val>ans?ans=val:0);
  39. //if(x) 即x不为0,0 false 1 true 手动笑哭
  40. int x,y;
  41. if (mod[k]) x=mod[k],y=div[k]+1;else x=9,y=div[k];
  42. //从左上角开始搜,先行后列
  43. if(map[x][y]) return dfs(k+1);
  44. bool *R=r[x],*L=l[y],*P=p[g[x][y]];
  45. int i,RA=ra[x][y];
  46. i=1;
  47. if((!R[i])&&(!L[i])&&(!P[i])){
  48. R[i]=L[i]=P[i]=1;
  49. val+=RA*i;
  50. dfs(k+1);
  51. R[i]=L[i]=P[i]=0;
  52. val-=RA*i;
  53. }
  54. i=2;
  55. if((!R[i])&&(!L[i])&&(!P[i])){
  56. R[i]=L[i]=P[i]=1;
  57. val+=RA*i;
  58. dfs(k+1);
  59. R[i]=L[i]=P[i]=0;
  60. val-=RA*i;
  61. }
  62. i=3;
  63. if((!R[i])&&(!L[i])&&(!P[i])){
  64. R[i]=L[i]=P[i]=1;
  65. val+=RA*i;
  66. dfs(k+1);
  67. R[i]=L[i]=P[i]=0;
  68. val-=RA*i;
  69. }
  70. i=4;
  71. if((!R[i])&&(!L[i])&&(!P[i])){
  72. R[i]=L[i]=P[i]=1;
  73. val+=RA*i;
  74. dfs(k+1);
  75. R[i]=L[i]=P[i]=0;
  76. val-=RA*i;
  77. }
  78. i=5;
  79. if((!R[i])&&(!L[i])&&(!P[i])){
  80. R[i]=L[i]=P[i]=1;
  81. val+=RA*i;
  82. dfs(k+1);
  83. R[i]=L[i]=P[i]=0;
  84. val-=RA*i;
  85. }
  86. i=6;
  87. if((!R[i])&&(!L[i])&&(!P[i])){
  88. R[i]=L[i]=P[i]=1;
  89. val+=RA*i;
  90. dfs(k+1);
  91. R[i]=L[i]=P[i]=0;
  92. val-=RA*i;
  93. }
  94. i=7;
  95. if((!R[i])&&(!L[i])&&(!P[i])){
  96. R[i]=L[i]=P[i]=1;
  97. val+=RA*i;
  98. dfs(k+1);
  99. R[i]=L[i]=P[i]=0;
  100. val-=RA*i;
  101. }
  102. i=8;
  103. if((!R[i])&&(!L[i])&&(!P[i])){
  104. R[i]=L[i]=P[i]=1;
  105. val+=RA*i;
  106. dfs(k+1);
  107. R[i]=L[i]=P[i]=0;
  108. val-=RA*i;
  109. }
  110. i=9;
  111. if((!R[i])&&(!L[i])&&(!P[i])){
  112. R[i]=L[i]=P[i]=1;
  113. val+=RA*i;
  114. dfs(k+1);
  115. R[i]=L[i]=P[i]=0;
  116. val-=RA*i;
  117. }
  118. }
  119. int main(){
  120. freopen("sudoku.in","r",stdin);
  121. freopen("sudoku.out","w",stdout);
  122. for (int i=0;i<=81;i++) mod[i]=i%9,div[i]=i/9;
  123. for (int i=1;i<=9;i++)
  124. for (int j=1;j<=9;j++)
  125. ra[i][j]+=5;
  126. for(int i=1;i<=9;i++)
  127. for(int j=1;j<=9;j++){
  128. scanf("%d",&n);
  129. if(n){
  130. r[i][n]=1;
  131. l[j][n]=1;
  132. p[g[i][j]][n]=1;
  133. val+=ra[i][j]*n;
  134. }
  135. map[i][j]=n;
  136. }
  137. dfs(1);
  138. if(!ans) printf("-1");else printf("%d",ans);
  139. return 0;
  140. }