POJ 2676:Sudoku 数独
| Time Limit: 2000MS | Memory Limit: 65536K | |||
| Total Submissions: 15830 | Accepted: 7737 | Special Judge | ||
Description
decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

Input
it is represented by 0.
Output
Sample Input
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
数独填数,深搜+暴力。
自己也优化了很多,结果一直TLE。当然还没有优化够,比方说按照可供选择的多少排序,从少的开始深搜。但觉得太麻烦了。看得TLE快绝望了,结果看discuss要从后面搜,果然。。。
但是这个题目的数据给得也是够了,没有道理前面搜TLE,后面搜16ms的啊。。。真的是
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; char chess[15][15];
char test[15][15];
int m,n,flag;
vector<char>kefang[15][15]; bool pend(int row,int col,char value)
{
int i,j,temp1,temp2;
if(row%3==0)
temp1=row/3;
else
temp1=row/3+1;
if(col%3==0)
temp2=col/3;
else
temp2=col/3+1;
for(i=(temp1-1)*3+1;i<=temp1*3;i++)
{
for(j=(temp2-1)*3+1;j<=temp2*3;j++)
{
if(i==row&&j==col)continue;
if(value==test[i][j])return false;
}
} for(i=1;i<=9;i++)
{
if( i!=col && value==test[row][i])
return false;
}
for(i=1;i<=9;i++)
{
if( i!=row && value==test[i][col])
return false;
} return true;
} void find(int i_r,int j_r)
{
if(j_r==1)
{
for(m=i_r-1;m>=1;m--)
{
for(n=9;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
}
}
else
{
m=i_r;
for(n=j_r-1;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
for(m=i_r-1;m>=1;m--)
{
for(n=9;n>=1;n--)
{
if(chess[m][n]=='0')
return;
}
}
}
m=0;
n=0;
} void dfs(int i,int j,char u)
{
if(flag)
return;
test[i][j]=u; if(i<=1&&j<=1)
{
int h,k;
for(h=1;h<=9;h++)
{
for(k=1;k<=9;k++)
{
chess[h][k]=test[h][k];
}
}
flag=1;
return;
} find(i,j);
char temp_c;
int m_temp=m;
int n_temp=n;
int size=kefang[m_temp][n_temp].size();
int xu;
for(xu=0;xu<size;xu++)
{
temp_c=kefang[m_temp][n_temp][xu];
if(pend(m_temp,n_temp,temp_c))
{
dfs(m_temp,n_temp,temp_c);
}
}
if(m==0&&n==0)
{
int h,k;
for(h=1;h<=9;h++)
{
for(k=1;k<=9;k++)
{
chess[h][k]=test[h][k];
}
}
flag=1;
return;
} test[i][j]='0';
} void solve()
{
char temp_c;
int i,j;
for(i=9;i>=1;i--)
{
for(j=9;j>=1;j--)
{
if(chess[i][j]=='0')
{
int size=kefang[i][j].size();
int xu;
for(xu=0;xu<size;xu++)
{
temp_c=kefang[i][j][xu];
if(pend(i,j,temp_c))
{
dfs(i,j,temp_c);
if(flag)
return;
}
}
}
}
} } void init()
{
int g,d;
for(g=1;g<=9;g++)
{
for(d=1;d<=9;d++)
{
char temp_c;
for(temp_c='1';temp_c<='9';temp_c++)
{
if(pend(g,d,temp_c))
{
kefang[g][d].push_back(temp_c);
}
}
}
}
} int main()
{
int Test,i,j;
scanf("%d",&Test); while(Test--)
{
for(i=1;i<=9;i++)
{
scanf("%s",chess[i]+1);
for(j=1;j<=9;j++)
{
test[i][j]=chess[i][j];
}
}
for(i=1;i<=9;i++)
for(j=1;j<=9;j++)
kefang[i][j].clear();
flag=0;
init();
solve();
for(i=1;i<=9;i++)
{
cout<<chess[i]+1<<endl;
}
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2676:Sudoku 数独的更多相关文章
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜
Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- ACM : POJ 2676 SudoKu DFS - 数独
SudoKu Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu POJ 2676 Descr ...
- POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- POJ 2676 Sudoku
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 12005 Accepted: 5984 Special ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- POJ 2676 Sudoku(深搜)
Sudoku Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submi ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
随机推荐
- crackme---攻防世界
首先下载附件之后,查壳 虽然什么也没有发现,但是看一下区段就知道,这个是北斗的壳.所以我们首先载入od开始把壳脱掉 这里面也可以看到pushfd和pushad是北斗壳的特征 这里面我使用是esp定律脱 ...
- C++面试常见问题——14内存管理
内存管理 内存管理由三种方式: 自动存储 静态存储 动态存储 自动存储 对于函数的形参.函数内部变量.和结构体变量等,编译器在函数运行过程中在栈中自动对其分配内存,调用结束后对其进行销毁.变量的声明周 ...
- 动态设置html根字体大小(随着设备屏幕的大小而变化,从而实现响应式)
代码如下:如果设置了根字体大小,font-size必须是rem var html =document.querySelector('html'); html.style.fontSize = docu ...
- epoll源码分析(基于linux-5.1.4)
API epoll提供给用户进程的接口有如下四个,本文基于linux-5.1.4源码详细分析每个API具体做了啥工作,通过UML时序图理清内核内部的函数调用关系. int epoll_create1( ...
- loadBeanDefinitions方法源码跟踪(三)
因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/46e27afd7d96 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 4.parseCus ...
- phpstrom+win10拼音输入法不跟随情况
PHPSTORM拼中文时悬浮框一直在右下角,真是逼死强迫症的操作! 最好解决方案: 下载讯飞输入法,虽然有点不习惯,用着用着就行了 等待官方修复着bug吧; 网上说的直接下载jre64覆盖原来的版本也 ...
- PL/SQL 找到某列都为空的列名
DECLARE CURSOR temp IS SELECT COLUMN_NAME FROM ALL_TAB_COLUMNS WHERE TABLE_NAME=Upper('xxx'); v_num ...
- 自己写个tween
public Vector3 begin,end;//起始终止坐标 public float BtoE_time;//用时 float timer,lerp;//计时器和进度值 void Update ...
- 解决fedora28桌面图标问题
正文 在fedora28中默认是没有桌面图标的,对于那些习惯使用桌面的图标的人来说使用有点不适应. 替代方法是: 下载nemo,在终端内输入sudo dnf install nemo 创建~/.con ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-signal
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...