Sudoku--DFS

    xiaoxiao2024-11-21  0

    Sudoku Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 18056 Accepted: 8745 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 题目链接:http://poj.org/problem?id=2676

    这两天怎么了,两个题了,输入的时候老是出错,难不成我已经不会输入了?!!!

    题目的意思是说有一个数独,规则是每一行不能有相同的数字,每一列不能有相同的数字,每个3*3的方格不能有相同的数字。很简单的规则吧,但我却不知道怎么实现3*3的方格不能有重复的数字,无奈,去看了大神的博客,哦,原来这么简单。

    http://blog.csdn.net/lyy289065406/article/details/6647977

    大神就是大神啊,我还是太渣了,讲的很清楚,重点是推出3×((i-1)/3)+(j-1)/3=k的这个式子来就可以了,还要继续努力啊。

    代码:

    #include <cstdio> #include <cstring> #include <iostream> using namespace std; bool row[100][100]; bool cow[100][100]; bool quan[100][100]; int map1[100][100]; int dfs(int x,int y) { int k; if(x==10) return true;//已经编立完了 bool flag=false; if(map1[x][y])//已经有数字了 { if(y==9) flag=dfs(x+1,1); else flag=dfs(x,y+1); if(flag) return true;//回溯 else return false; } else { k=3*((x-1)/3)+(y-1)/3+1;//计算在第几个方块 for(int i=1;i<=9;i++)//暴力1~9 { if(!row[x][i]&&!cow[y][i]&&!quan[k][i]) { map1[x][y]=i; row[x][i]=true; cow[y][i]=true; quan[k][i]=true; if(y==9) flag=dfs(x+1,1); else flag=dfs(x,y+1); if(!flag)//递归失败,继续回溯 { map1[x][y]=0; row[x][i]=false; cow[y][i]=false; quan[k][i]=false; } else return true; } } } return false; } int main() { int t; int k,i,j; scanf("%d",&t); while(t--) { memset(row,0,sizeof(row)); memset(cow,0,sizeof(cow)); memset(quan,0,sizeof(quan)); memset(map1,0,sizeof(map1)); /*for(i=1;i<=9;i++) { for(j=1;j<=9;j++) { scanf("%1d",&map1[i][j]); if(map1[i][j]) { k=3*((i-1)/3)+(j-1)/3+1; row[i][map1[i][j]]=true; cow[i][map1[i][j]]=true; quan[k][map1[i][j]]=true;//就是这些,不知道为什么出错了,大神路过请指教。。 } } }*/ char MAP[10][10]; for(i=1;i<=9;i++) for(j=1;j<=9;j++) { cin>>MAP[i][j]; map1[i][j]=MAP[i][j]-'0'; if(map1[i][j]) { int k=3*((i-1)/3)+(j-1)/3+1; row[i][ map1[i][j] ]=true; cow[j][ map1[i][j] ]=true; quan[k][ map1[i][j] ]=true; } } dfs(1,1); for(i=1;i<=9;i++) { for(j=1;j<=9;j++) { printf("%d",map1[i][j]); } cout<<endl; } } return 0; }

    转载请注明原文地址: https://ju.6miu.com/read-1293875.html
    最新回复(0)