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 ...
随机推荐
- 【pwnable.kr】 brainfuck
pwnable.kr第二关第一题: ========================================= Download : http://pwnable.kr/bin/bfDownl ...
- linux服务器常用操作和命令
1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...
- LabVIEW面向对象的ActorFramework(1)
本系列文章主要阐述以下几个问题: (1)什么是面向对象编程? (2)为什么要学习面向编程? (3)LabVIEW面向编程学习为什么有点难? (4)LabVIEW面向对象的编程架构:Actor Fram ...
- Python在运行中发生错误怎么正确处理方法,案例详解!
在程序运行的过程中,如果发生了错误,可以事先约定返回一个错误代码,这样,就可以知道是否有错,以及出错的原因.在操作系统提供的调用中,返回错误码非常常见.比如打开文件的函数open(),成功时返回文件描 ...
- MQTT 协议学习:004-MQTT建立通信与 CONNECT 、CONNACK 报文
背景 上一讲 MQTT 协议学习:通信报文的构成介绍了在MQTT通信中,各报文的通信流程:从本讲开始,我们开始介绍实际中使用的报文,以及它们的组成. CONNECT - 连接请求 报文 客户端到服务端 ...
- JumpServer简单使用
笔记内容:简单使用jumpserver笔记日期:2018-01-22 23.9 创建jumpserver普通用户 23.10 添加机器 23.11 添加系统用户并授权 23.12 添加授权规则 23. ...
- springcloud--zuul(过滤器)
在zuul添加过滤器 新建类继承ZuulFilter类. public class MyFilter extends ZuulFilter{ //是否需要过滤 @Override public boo ...
- 剑指offer_2.3_Day_6
大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). n<=39 public class Solution { public int Fibo ...
- python3调用微软js引擎ChakraCore
有关ChakraCore介绍请移步:https://github.com/Microsoft/ChakraCore 使用案例GitHub源码:https://github.com/pyAppman/C ...
- 利用 Python 破解 ZIP 或 RAR 文件密码
我们经常会从网络上下载一些带密码的压缩包,想要获取里面的内容,往往就要给提供商支付一些费用.想要白嫖其中的内容,常见的做法是百度搜索一些压缩包密码破解软件,但后果相信体验过的人都知道.本文将会利用 P ...