poj2676——dfs

POJ 2676  dfs

Sudoku

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 14640   Accepted: 7217   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

题意:填充方格,使每一行每一列每一个9宫格的数都含有1-9思路:dfs,从第一行第一个数字开始,然后第二个,第三个...第二行第一个...一直到最后一个,出现冲突时回溯;利用row,col,grid对每一行每一列每一个9宫格进行判重干了一晚上总算是AC了。。

//poj2676_dfs 391ms
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>

using namespace std;

const int maxn=10;

int T;
int ch[maxn][maxn];

bool row[maxn][maxn]; //row[i][k] 判断第i行是否有k
bool col[maxn][maxn]; //判断第i列是否有k
bool grid[maxn][maxn]; //判断第i个9宫格是否有k
bool flag=0;

int ans[maxn][maxn]; //记录答案

void dfs(int x,int y)
{
    if(flag) return;
    if(x==9&&y==0){ //直接精确判断最后一格,不要用judge判断,若每次用judge判断时间浪费太多
        flag=1;
        memcpy(ans,ch,sizeof(ch)); //dfs记录路径答案最好直接拷贝数组,慎用原数组,因为若题目要求搜所以情况且过程用贪心时原数组最终会随着dfs的结束而清零
        return;
    }
    if(ch[x][y]==0){ //空格
        int k=x/3*3+y/3; //第k个9宫格
        for(int i=1;i<=9;i++){
            if(row[x][i]||col[y][i]||grid[k][i]) continue;
            ch[x][y]=i; //放置
            row[x][i]=col[y][i]=grid[k][i]=1;
            if(y+1<9) dfs(x,y+1);
            else dfs(x+1,0);
            ch[x][y]=0; //还原
            row[x][i]=col[y][i]=grid[k][i]=0;
        }
    }
    else{ //非空格
        if(y+1<9) dfs(x,y+1);
        else dfs(x+1,0);
    }
}

int main()
{
    cin>>T;
    while(T--){
        memset(row,0,sizeof(row));
        memset(col,0,sizeof(col));
        memset(grid,0,sizeof(grid));
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                scanf("%1d",&ch[i][j]);
                row[i][ch[i][j]]=1;
                col[j][ch[i][j]]=1;
                grid[i/3*3+j/3][ch[i][j]]=1;
            }
        }
        flag=0;
        dfs(0,0);
        for(int i=0;i<9;i++){
            for(int j=0;j<9;j++){
                printf("%d",ans[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}

poj2676_dfs

时间: 2024-10-08 08:58:56

poj2676——dfs的相关文章

poj2676 Sudoku(DFS)

做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 bool row_f[9][

POJ2676 Sudoku - DFS

POJ2676 Sudoku 传送门 题意: 填充未完成的数独...(就这么简单.... 思路: 爆搜即可. 可行性剪枝:用三个\(bool\)数组分别记录行.列.\(3*3\)的块中,\(9\)种数字的使用情况 AC Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=9+10; char s[N][N];int mp[N][N];

POJ--2676&amp;HDU--1421(数独,dfs)

Sudoku Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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

[DFS]poj2676(2918)

题意: 数独问题,给出一些点,然后要求填满格子.每行没列1-9不能重复,每个3*3的小格子也不能重复. 分析: 暴力搜索,那么主要的问题就是每次如何判断是否冲突.行和列的比较好想,row[i][j]=1表示第i行的j已经被占了,col[i][j]=1表示第i列的j已经被占了,那么每个小格子呢? 首先,每行每列都是012.345.678那么每个j/3表示它在这以行的第j/3个格子里,i/3表示在第i/3行的格子,那么i/3*3+j/3就表示该点处于那个小格子里,同理g[i][j]表示第i个格子里的

POJ2676 Sudoku [数独]

好题,也很实用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programing写错了,,,本来那个memset想写成Generic Programing的,,,然后,永远只有第一组结果对 不说了,泪哈,,, #include <cstdio> #include <cstring> #include <iostream> #include <cs

Poj 2676 Sudoku[dfs]

题目大意: 九宫格问题,也有人叫数独问题 把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子网格内都只能使用一次1~9中的一个数字,即每行.每列.每个子网格内都不允许出现相同的数字. 0是待填位置,其他均为已填入的数字. 要求填完九宫格并输出(如果有多种结果,则只需输出其中一种) 如果给定的九宫格无法按要求填出来,则输出原来所输入的未填的九宫格 思路: DFS 深搜 char map[10][10];/*数据存储*/bool row[10][10];/*行存在数*/bool

解救小哈——DFS算法举例

一.问题引入 有一天,小哈一个人去玩迷宫.但是方向感不好的小哈很快就迷路了.小哼得知后便去解救无助的小哈.此时的小哼已经弄清楚了迷宫的地图,现在小哼要以最快的速度去解救小哈.那么,问题来了... 二.问题的分析 首先我们用一个二维数组来存储这个迷宫,刚开始的时候,小哼处于迷宫的入口处(1,1),小哈在(p,q).其实这道题的的本质就在于找从(1,1)到(p,q)的最短路径. 此时摆在小哼面前的路有两条,我们可以先让小哼往右边走,直到走不通的时候再回到这里,再去尝试另外一个方向. 在这里我们规定一

【BZOJ4942】[Noi2017]整数 线段树+DFS(卡过)

[BZOJ4942][Noi2017]整数 题目描述去uoj 题解:如果只有加法,那么直接暴力即可...(因为1的数量最多nlogn个) 先考虑加法,比较显然的做法就是将A二进制分解成log位,然后依次更新这log位,如果最高位依然有进位,那么找到最高位后面的第一个0,将中间的所有1变成0,那个0变成1.这个显然要用到线段树,但是复杂度是nlog2n的,肯定过不去. 于是我在考场上yy了一下,这log位是连续的,我们每次都要花费log的时间去修改一个岂不是很浪费?我们可以先在线段树上找到这段区间

uva1103(dfs)

UVA - 1103 还是没写好,,看的别人的 1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <cstring> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <stack> 8 #include <cctype> 9 #include <str