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

 

posted @ 2015-03-14 00:29  __560  阅读(237)  评论(0编辑  收藏  举报