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. JS写一个漂亮的音乐播放器

    先放上效果图: 正如图中所展示的播放器那样,我们用HTML+CSS+JS将这个效果实现出来. HTML页面布局 <div class="music"> <div ...

  2. wdcp升级php5.8到php7.1.12

    php7升级脚本 update_php7.sh #!/bin/bash # PHP update scripts ];then Ver= else Ver=$ fi Debugfile= echo & ...

  3. maven杂碎汇总

        本来是想写一篇关于maven知识点的详细总结的,但需要看一本书或教材,然后再汇总一下,这样做自然是好的,这个在年前争取做完.本文是主要记录在工作和学习遇到关于maven的一些困惑.很乐意看到它 ...

  4. leetcode1161 Maximum Level Sum of a Binary Tree

    """ BFS遍历题,一遍AC Given the root of a binary tree, the level of its root is 1, the leve ...

  5. JAVA 怎么链接 SQL Server 数据库(半详细)

    哇今天闲着没事准备把前几天链接数据库碰到的问题写到博客,写到一半浏览器切出去了,这个浏览器也没保存,晚上又写了一遍,唉 蛋疼呀╮(╯▽╰)╭!! 说一说连接数据库遇到的问题,唉问题繁多,原来自己刷的精 ...

  6. Java For 循环

    章节 Java 基础 Java 简介 Java 环境搭建 Java 基本语法 Java 注释 Java 变量 Java 数据类型 Java 字符串 Java 类型转换 Java 运算符 Java 字符 ...

  7. 51nod 1206:Picture 求覆盖周长

    1206 Picture 题目来源: IOI 1998 基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题  收藏  关注 给出平面上的N个矩形(矩形的边平行于X轴和 ...

  8. 用JS改变embed标签的src属性

    思路: A.先隐藏embed标签 B.清除embed元素 C.为embed重新赋值,加入Html页面中 1.html代码 <object id="forfun" classi ...

  9. 设置gvim的字体大小

    1.临时设置: 进入命令行模式输入: set guifont=Courier\ New:h10 2.永久设置: 打开安装目录找到defaults.vim在最后一行输入: set guifont=Cou ...

  10. 2.13 ViewModel 使数据即使在后台被杀死数据也能存活

    操作程序如下: 主要是创建一个 SavedStateHandle 来缓存上一次的数据,并通过重新读取上一次存储的数据来实现数据的存活 MyVIewModel: package com.example. ...