Sudoku
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 15830   Accepted: 7737   Special Judge

Description

Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Figure. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with
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

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty
it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

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 数独的更多相关文章

  1. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  2. 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 ...

  3. 深搜+回溯 POJ 2676 Sudoku

    POJ 2676 Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17627   Accepted: 8538 ...

  4. ACM : POJ 2676 SudoKu DFS - 数独

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

  5. POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]

    题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...

  6. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  7. POJ 2676 Sudoku

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12005   Accepted: 5984   Special ...

  8. poj 2676 Sudoku ( dfs )

    dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...

  9. POJ 2676 Sudoku(深搜)

    Sudoku Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submi ...

  10. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

随机推荐

  1. 学习Linux让我进入了知名企业

    说起我学习Linux的原因是多方面的,大学时我学的是物理学师范专业,有部分计算机课程,但我觉得这些课程没什么实际作用,我自己对计算机比较感兴趣,我利用业余时间学习了很多计算机技术.在大学期间我参加了很 ...

  2. HiBench成长笔记——(9) 分析源码monitor.py

    monitor.py 是主监控程序,将监控数据写入日志,并统计监控数据生成HTML统计展示页面: #!/usr/bin/env python2 # Licensed to the Apache Sof ...

  3. Compile-kernel-module

    Compile-kernel-module 1. 内核模块编程1.1 简介1.2 加载内核模块1.3 最简单的模块1.4 模块必要信息1.4.1 内核模块必须至少包含的头文件:1.4.2 内核模块必须 ...

  4. python 字符串实例:检查并判断密码字符串的安全强度

    检查并判断密码字符串的安全强度 import string def check(pwd): #密码必须至少包含六个字符 if not isinstance(pwd,str) or len(pwd)&l ...

  5. Netty 异步模型

    简介 Netty中的 I/O 操作是异步的, 包括 Bind.Write.Connect 等操作会简单的返回一个ChannelFuture. 调用者不能立刻获得结果, 而是通过Future-Liste ...

  6. docker-compose(grafana influxdb) + telegraf 快速搭建简单监控

     灵活实现方案:   1:     telegraf 为go 语言写得占用内存小 收集主机各项监控数据 定时写入 时序DB   influxdb ------------------------&qu ...

  7. pacificrack 控制面板登录不上的问题

    我今天又试了一下: https://master-stack01.pacificrack.com还是登不上(这个一键烦恼了我一个星期了,但是我今天百度出来了解决办法) 然后用这个就可以了  https ...

  8. P 1035 插入与归并

    转跳点 :

  9. C语言备忘录——运算符优先级

    丢脸啊,今天写一道算法题,第一次没写对.改了半天愣是没看出来错哪,后面说出了一下过程,突然发现是运算符优先级惹得祸 if (!num % 2){ …… },!的运算优先级高于%,啊啊啊,丧心病狂我找了 ...

  10. Exchange Server备份与恢复

    本文档描述了Exchange 2003.Exchange Server 2007/2010的备份与恢复操作,涉及的内容包括: 1.使用NTBackup 备份与恢复Exchange 2007/2003 ...