题目大意:

9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1

说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多

位运算加速我确实写不出了,直接用dlx来做这道题目

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <climits>
  7. #include <cmath>
  8.  
  9. using namespace std;
  10. #define N 1000
  11. #define MAXNODE 1000000
  12. const int INF = INT_MAX;
  13. const double eps = 1e-;
  14.  
  15. int a[][];
  16. char str[];
  17.  
  18. int belong[][] = {
  19. {,,,,,,,,},
  20. {,,,,,,,,},
  21. {,,,,,,,,},
  22. {,,,,,,,,},
  23. {,,,,,,,,},
  24. {,,,,,,,,},
  25. {,,,,,,,,},
  26. {,,,,,,,,},
  27. {,,,,,,,,},
  28. };
  29. int sc[][] = {
  30. {,,,,,,,,},
  31. {,,,,,,,,},
  32. {,,,,,,,,},
  33. {,,,,,,,,},
  34. {,,,,,,,,},
  35. {,,,,,,,,},
  36. {,,,,,,,,},
  37. {,,,,,,,,},
  38. {,,,,,,,,},
  39. };
  40.  
  41. void printM()
  42. {
  43. for(int i= ; i< ; i++)
  44. for(int j= ; j< ; j++){
  45. if(j<) printf("%d " , a[i][j]);
  46. else printf("%d\n" , a[i][j]);
  47. }
  48. }
  49.  
  50. struct DLX{
  51. int n ,m , size;
  52. int col[MAXNODE] , row[MAXNODE];
  53. int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE];
  54. int cnt_col[N] , first[N];
  55. int ans[] , minv;
  56.  
  57. void init(int _n , int _m)
  58. {
  59. n = _n , m = _m;
  60. size= m ;
  61. for(int i= ; i<=m ; i++){
  62. L[i] = i- , R[i] = i+;
  63. U[i] = D[i] = i;
  64. }
  65. L[] = m , R[m] = ;
  66. for(int i= ; i<=m ; i++) cnt_col[i] = ;
  67. for(int i= ; i<=n ; i++) first[i] = -;
  68. minv = ;
  69. }
  70.  
  71. void link(int r , int c)
  72. {
  73. ++size;
  74. U[D[c]] = size , D[size] = D[c];
  75. U[size] = c , D[c] = size;
  76.  
  77. if(first[r]<) L[size]=R[size]=first[r] = size;
  78. else{
  79. L[R[first[r]]] = size , R[size] = R[first[r]];
  80. L[size] = first[r] , R[first[r]] = size;
  81. }
  82. row[size] = r , col[size] = c , cnt_col[c]++;
  83. }
  84.  
  85. void Remove(int c)
  86. {
  87. L[R[c]] = L[c] , R[L[c]] = R[c];
  88. for(int i=D[c] ; i!=c ; i=D[i]){
  89. for(int j=R[i] ; j!=i ; j=R[j]){
  90. U[D[j]] = U[j] , D[U[j]] = D[j];
  91. cnt_col[col[j]]--;
  92. }
  93. }
  94. }
  95.  
  96. void Resume(int c)
  97. {
  98. for(int i=U[c] ; i!=c ; i=U[i]){
  99. for(int j=L[i] ; j!=i ; j=L[j]){
  100. U[D[j]] = D[U[j]] = j;
  101. cnt_col[col[j]]++;
  102. }
  103. }
  104. // printM();
  105. L[R[c]] = R[L[c]] = c;
  106. }
  107.  
  108. void Dance(int d)
  109. {
  110. if(!R[]){
  111. int v = ;
  112. for(int i= ; i<d ; i++){
  113. int r = (ans[i]-)/;
  114. int c = ((ans[i]-)%)/;
  115. a[r][c] = ((ans[i]-)%)+;
  116. v+=a[r][c]*sc[r][c];
  117. }
  118. minv=max(minv , v);
  119. return;
  120. }
  121. int st=R[];
  122. for(int i=R[] ; i!= ; i=R[i])
  123. if(cnt_col[i]<cnt_col[st])
  124. st = i;
  125. Remove(st);
  126. for(int i=D[st] ; i!=st ; i=D[i]){
  127. ans[d] = row[i];
  128. for(int j=R[i] ; j!=i ; j=R[j]) Remove(col[j]);
  129. Dance(d+);
  130. for(int j=L[i] ; j!=i ; j=L[j]) Resume(col[j]);
  131. }
  132. Resume(st);
  133. return ;
  134. }
  135.  
  136. }dlx;
  137.  
  138. int main()
  139. {
  140. // freopen("a.in" , "r" , stdin);
  141. int T;
  142. scanf("%d" , &T);
  143. while(T--)
  144. {
  145. for(int i= ; i< ; i++){
  146. for(int j= ; j< ; j++){
  147. scanf("%d" , &a[i][j]);
  148. }
  149. }
  150. dlx.init( , );
  151. for(int i= ; i< ; i++)
  152. for(int j= ; j< ; j++)
  153. {
  154. if(a[i][j]){
  155. dlx.link((i*+j)*+a[i][j] , i*+a[i][j]);
  156. dlx.link((i*+j)*+a[i][j] , +j*+a[i][j]);
  157. dlx.link((i*+j)*+a[i][j] , +(belong[i][j]-)*+a[i][j]);
  158. dlx.link((i*+j)*+a[i][j] , +i*+j+);
  159. }
  160. else{
  161. for(int k= ; k<= ; k++){
  162. dlx.link((i*+j)*+k , i*+k);
  163. dlx.link((i*+j)*+k , +j*+k);
  164. dlx.link((i*+j)*+k , +(belong[i][j]-)*+k);
  165. dlx.link((i*+j)*+k , +i*+j+);
  166. }
  167. }
  168. }
  169. dlx.Dance();
  170. if(!dlx.minv) puts("-1");
  171. else printf("%d\n" , dlx.minv);
  172. }
  173.  
  174. return ;
  175. }

CSU 1605 数独的更多相关文章

  1. LintCode389.判断数独是否合法

    LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...

  2. [LeetCode] Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. [LeetCode] Valid Sudoku 验证数独

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  4. 数独 JAVA实现

    数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...

  5. 用C++实现的解数独(Sudoku)程序

    我是一个C++初学者,控制台实现了一个解数独的小程序. 代码如下: //"数独游戏"V1.0 //李国良于2016年11月11日编写完成 #include <iostream ...

  6. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  7. ACM: ICPC/CCPC Sudoku DFS - 数独

    Sudoku Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/65535K (Java/Other) Total Submis ...

  8. codevs 2924 数独挑战

    2924 数独挑战 http://codevs.cn/problem/2924/ 题目描述 Description "芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而 ...

  9. 用html5 canvas和JS写个数独游戏

    为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...

随机推荐

  1. Outlook读取奇妙清单Wunderlist日历失败的解决办法

    错误: Outlook.com日历订阅奇妙清单的日历链接时报错 This calendar wasn't updated because of a problem with the publisher ...

  2. CF765C Table Tennis Game 2

    题意: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ...

  3. 【学习笔记】深入理解js原型和闭包(9)—— 简述【执行上下文】下

    继续上一篇文章(https://www.cnblogs.com/lauzhishuai/p/10078231.html)的内容. 上一篇我们讲到在全局环境下的代码段中,执行上下文环境中有如何数据: 变 ...

  4. 让WPS10显示为offic97效果

    让WPS10显示为offic97效果2019/1/26 22:02 OS:win7 64位使用的WPS_10.1.0.5603_setup.1460689247.exe 衣不如旧,人不如新.最开始接触 ...

  5. PHP实现远程图片下载

    /** * 文件下载 * @param string $url */ public function download() { $url = $this->input->get_post( ...

  6. js 逻辑运算符、等号运算符

    1 逻辑运算符 逻辑运算只有2个结果,一个为true,一个为false. 1.且&& ★ 两个表达式为true的时候,结果为true. ------------------------ ...

  7. promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示

    promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示

  8. iview table 普通表格样式

    iview table 普通表格样式 https://run.iviewui.com/UvLFPMb0 <template> <table> <thead> < ...

  9. The MySQL server is running with the –secure-file-priv

    show variables like '%secure%'; 将文件导出路径更改为查询到的secure-file-priv路径下 select * from table where column = ...

  10. viewport移动端适配,读文笔记

    文章地址: viewport移动端适配 笔记: 移动端适配目的: 希望在屏幕尺寸大小不同的手机上进行访问页面时,页面显示的效果能合理的展示,我们期望的是在手机屏幕较大时显示的内容比较大一些,手机屏幕小 ...