首页 技术 正文
技术 2022年11月15日
0 收藏 418 点赞 2,658 浏览 1722 个字

这是一神题!!!

可能是因为我太弱了,题解都看不太懂QWQ

不过感谢wng老师的提醒,我写出了这个样的的代码。

分析:

这道题是一个搜索(dfs)。很神奇很暴力的题

首先,你需要看懂题目。(可以先去玩下正常的数独,然后基本就明白了….)

其次,应题目要求,我们使用三个数组来标记在此位置填数合不合法,然后有一个非常自然的想法,从每个位置来搜,填数后递归,填完后计算出答案,取最大值。

不过你会发现你爆栈了!!!

所以我开三个数组,记录行列以及九宫格的状态,只有在三个数组中都未出现的值才可以被填入,不过我是反着搜的。

CODE:

// luogu-judger-enable-o2
#include<iostream>
#include<cstdio>
#include<algorithm>#define ll long long
#define re register
//define大法好!!!using namespace std;bool line[10][10],roway[10][10],nine[10][10];
int a[10][10],b[10][10];
int got[10][10][10];
int ans = -1, score;inline int math_sign(int x,int y ,int z){
if(x == 5 && y == 5)
return 10 * z;
else if(x >= 4 && x <= 6 && y >= 4 && y <= 6)
return 9 * z;
else if(x >= 3 && x <= 7 && y >= 3 && y <= 7)
return 8 * z;
else if(x >= 2 && x <= 8 && y >= 2 && y <= 8)
return 7 * z;
else return 6 * z;
}//模拟数独的得分方式inline bool get_score(int x,int y,int z){
if(roway[x][z] || line[y][z] || nine[(x - 1) / 3 * 3 + (y - 1) / 3][z])
return 0;
b[x][y] = z;
roway[x][z] = line[y][z] = nine[(x - 1) / 3 * 3 + (y - 1) / 3][z] = 1;
score += got[x][y][z];
return 1;
}inline int _read(){
re int x = 0;
re int flag = true;
re int k = getchar();
while(k != '-' && !isdigit(k))
k = getchar();
if(k == '-'){
k = getchar();
flag = false;
}
while(isdigit(k)){
x = x * 10 + k - '0';
k = getchar();
}
return (flag ? x : -x);
}//读入优化,不会的可以背一下inline void del(int x,int y,int z){
roway[x][z] = line[y][z] = nine[(x - 1) / 3 * 3 + (y - 1)/3][z] = 0;
}void dfs(int x,int y ){
if(x == 10 && y == 1){
ans = max(ans,score);
return;
}
if(b[x][y]){
if(y == 9)
dfs(x + 1, 1);
else
dfs(x,y + 1);
}
else{
for(int i = 1 ;i <= 9 ;i++){
int t = score;
if(get_score(x,y,i)){
if(y == 9)
dfs(x + 1,1);
else
dfs(x,y + 1);
del(x,y,i);
score = t;
}
}
b[x][y] = 0;
}
}int main(){
for(int i = 1; i<= 9; i++)
for(int j = 1; j <= 9 ;j++)
for(int k = 1; k<= 9 ; k++)
got[i][j][k] = math_sign(i,j,k);
for(int i = 9; i > 0 ; i--)
for(int j = 9; j > 0 ; j--){
a[i][j] = _read();
if(a[i][j])
get_score(i,j,a[i][j]);
}
dfs(1,1);
printf("%d",ans);
return 0;
}

没错,我开了O2优化,额————其实是因为之前花式被卡,各种TLE,于是我就打开了氧气罐子优化QAQ。。。

祝大家早日AC,不再TLE。(点个赞吧)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295