CSU 1605 数独
题目大意:
9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1
说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多
位运算加速我确实写不出了,直接用dlx来做这道题目
- #include <iostream>
- #include <cstring>
- #include <cstdio>
- #include <algorithm>
- #include <queue>
- #include <climits>
- #include <cmath>
- using namespace std;
- #define N 1000
- #define MAXNODE 1000000
- const int INF = INT_MAX;
- const double eps = 1e-;
- int a[][];
- char str[];
- int belong[][] = {
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- };
- int sc[][] = {
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- {,,,,,,,,},
- };
- void printM()
- {
- for(int i= ; i< ; i++)
- for(int j= ; j< ; j++){
- if(j<) printf("%d " , a[i][j]);
- else printf("%d\n" , a[i][j]);
- }
- }
- struct DLX{
- int n ,m , size;
- int col[MAXNODE] , row[MAXNODE];
- int U[MAXNODE] , D[MAXNODE] , L[MAXNODE] , R[MAXNODE];
- int cnt_col[N] , first[N];
- int ans[] , minv;
- void init(int _n , int _m)
- {
- n = _n , m = _m;
- size= m ;
- for(int i= ; i<=m ; i++){
- L[i] = i- , R[i] = i+;
- U[i] = D[i] = i;
- }
- L[] = m , R[m] = ;
- for(int i= ; i<=m ; i++) cnt_col[i] = ;
- for(int i= ; i<=n ; i++) first[i] = -;
- minv = ;
- }
- void link(int r , int c)
- {
- ++size;
- U[D[c]] = size , D[size] = D[c];
- U[size] = c , D[c] = size;
- if(first[r]<) L[size]=R[size]=first[r] = size;
- else{
- L[R[first[r]]] = size , R[size] = R[first[r]];
- L[size] = first[r] , R[first[r]] = size;
- }
- row[size] = r , col[size] = c , cnt_col[c]++;
- }
- void Remove(int c)
- {
- L[R[c]] = L[c] , R[L[c]] = R[c];
- for(int i=D[c] ; i!=c ; i=D[i]){
- for(int j=R[i] ; j!=i ; j=R[j]){
- U[D[j]] = U[j] , D[U[j]] = D[j];
- cnt_col[col[j]]--;
- }
- }
- }
- void Resume(int c)
- {
- for(int i=U[c] ; i!=c ; i=U[i]){
- for(int j=L[i] ; j!=i ; j=L[j]){
- U[D[j]] = D[U[j]] = j;
- cnt_col[col[j]]++;
- }
- }
- // printM();
- L[R[c]] = R[L[c]] = c;
- }
- void Dance(int d)
- {
- if(!R[]){
- int v = ;
- for(int i= ; i<d ; i++){
- int r = (ans[i]-)/;
- int c = ((ans[i]-)%)/;
- a[r][c] = ((ans[i]-)%)+;
- v+=a[r][c]*sc[r][c];
- }
- minv=max(minv , v);
- return;
- }
- int st=R[];
- for(int i=R[] ; i!= ; i=R[i])
- if(cnt_col[i]<cnt_col[st])
- st = i;
- Remove(st);
- for(int i=D[st] ; i!=st ; i=D[i]){
- ans[d] = row[i];
- for(int j=R[i] ; j!=i ; j=R[j]) Remove(col[j]);
- Dance(d+);
- for(int j=L[i] ; j!=i ; j=L[j]) Resume(col[j]);
- }
- Resume(st);
- return ;
- }
- }dlx;
- int main()
- {
- // freopen("a.in" , "r" , stdin);
- int T;
- scanf("%d" , &T);
- while(T--)
- {
- for(int i= ; i< ; i++){
- for(int j= ; j< ; j++){
- scanf("%d" , &a[i][j]);
- }
- }
- dlx.init( , );
- for(int i= ; i< ; i++)
- for(int j= ; j< ; j++)
- {
- if(a[i][j]){
- dlx.link((i*+j)*+a[i][j] , i*+a[i][j]);
- dlx.link((i*+j)*+a[i][j] , +j*+a[i][j]);
- dlx.link((i*+j)*+a[i][j] , +(belong[i][j]-)*+a[i][j]);
- dlx.link((i*+j)*+a[i][j] , +i*+j+);
- }
- else{
- for(int k= ; k<= ; k++){
- dlx.link((i*+j)*+k , i*+k);
- dlx.link((i*+j)*+k , +j*+k);
- dlx.link((i*+j)*+k , +(belong[i][j]-)*+k);
- dlx.link((i*+j)*+k , +i*+j+);
- }
- }
- }
- dlx.Dance();
- if(!dlx.minv) puts("-1");
- else printf("%d\n" , dlx.minv);
- }
- return ;
- }
CSU 1605 数独的更多相关文章
- LintCode389.判断数独是否合法
LintCode简单题:判断数独是否合法 问题描述: 请判定一个数独是否有效. 该数独可能只填充了部分数字,其中缺少的数字用 . 表示. 注意事项: 一个合法的数独(仅部分填充)并不一定是可解的.我们 ...
- [LeetCode] Sudoku Solver 求解数独
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] Valid Sudoku 验证数独
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 数独 JAVA实现
数独游戏的规则从很久之前就知道,但是一直都没怎么玩过,然后到了大学,大一下学期自己学dfs的时候,刚刚好碰到了一个数独的题目,做出来后,感觉还是挺有成就感的 然后大二学了JAVA,看了下那个一些有关于 ...
- 用C++实现的解数独(Sudoku)程序
我是一个C++初学者,控制台实现了一个解数独的小程序. 代码如下: //"数独游戏"V1.0 //李国良于2016年11月11日编写完成 #include <iostream ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- codevs 2924 数独挑战
2924 数独挑战 http://codevs.cn/problem/2924/ 题目描述 Description "芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而 ...
- 用html5 canvas和JS写个数独游戏
为啥要写这个游戏? 因为我儿子二年级数字下册最后一章讲到了数独.他想玩儿. 因为我也想玩有提示功能的数独. 因为我也正想决定要把HTML5和JS搞搞熟.熟悉一个编程平台,最好的办法,就是了解其原理与思 ...
随机推荐
- Outlook读取奇妙清单Wunderlist日历失败的解决办法
错误: Outlook.com日历订阅奇妙清单的日历链接时报错 This calendar wasn't updated because of a problem with the publisher ...
- CF765C Table Tennis Game 2
题意: Misha and Vanya have played several table tennis sets. Each set consists of several serves, each ...
- 【学习笔记】深入理解js原型和闭包(9)—— 简述【执行上下文】下
继续上一篇文章(https://www.cnblogs.com/lauzhishuai/p/10078231.html)的内容. 上一篇我们讲到在全局环境下的代码段中,执行上下文环境中有如何数据: 变 ...
- 让WPS10显示为offic97效果
让WPS10显示为offic97效果2019/1/26 22:02 OS:win7 64位使用的WPS_10.1.0.5603_setup.1460689247.exe 衣不如旧,人不如新.最开始接触 ...
- PHP实现远程图片下载
/** * 文件下载 * @param string $url */ public function download() { $url = $this->input->get_post( ...
- js 逻辑运算符、等号运算符
1 逻辑运算符 逻辑运算只有2个结果,一个为true,一个为false. 1.且&& ★ 两个表达式为true的时候,结果为true. ------------------------ ...
- promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示
promise 里面的 console.info 打印信息 并不准确,后期有修改对象数据,会覆盖,影响之前的显示
- iview table 普通表格样式
iview table 普通表格样式 https://run.iviewui.com/UvLFPMb0 <template> <table> <thead> < ...
- The MySQL server is running with the –secure-file-priv
show variables like '%secure%'; 将文件导出路径更改为查询到的secure-file-priv路径下 select * from table where column = ...
- viewport移动端适配,读文笔记
文章地址: viewport移动端适配 笔记: 移动端适配目的: 希望在屏幕尺寸大小不同的手机上进行访问页面时,页面显示的效果能合理的展示,我们期望的是在手机屏幕较大时显示的内容比较大一些,手机屏幕小 ...