Poj2676 Sudoku [Sudoku]

Source: Internet
Author: User

A good question is also very useful and has made several mistakes.

1. Thinking has an error in enumeration assignment: when the current assignment cannot be completed, it should be to continue the next loop, rather than return false to terminate the enumeration.

2. The generic programing is wrong. Originally, the memset wanted to be written as the generic programing. Then, there will always be only the first group of results

No, tears ,,,

#include <cstdio>#include <cstring>#include <iostream>#include <cstdlib>using namespace std;int map[10][10];char tmp[10][10];bool row[10][10];bool col[10][10];bool grid[10][10];bool DFS(int x,int y){int here=3*((x-1)/3)+(y-1)/3+1;if(x==10)return true;if(map[x][y]){bool flag;return y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);}else{for(int num=1;num<=9;num++){if(!row[x][num]&&!col[y][num]&&!grid[here][num]){map[x][y]=num;row[x][num]=true;col[y][num]=true;grid[here][num]=true;bool flag;y==9?flag=DFS(x+1,1):flag=DFS(x,y+1);if(flag)return true;else{map[x][y]=0;row[x][num]=false;col[y][num]=false;grid[here][num]=false;}}}}return false;}int main(){   //freopen("/home/rainto96/in.txt","r",stdin);int test;cin>>test;while(test--){memset(map,0,sizeof(map));memset(tmp,0,sizeof(tmp));memset(grid,0,sizeof(grid));memset(col,0,sizeof(col));memset(row,0,sizeof(row));for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){cin>>tmp[i][j];int here=3*((i-1)/3)+(j-1)/3+1;map[i][j]=tmp[i][j]-'0';if(map[i][j]){row[i][map[i][j]]=true;col[j][map[i][j]]=true;grid[here][map[i][j]]=true;}}}DFS(1,1);for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){cout<<map[i][j];}cout<<'\n';}}return 0;}


Poj2676 Sudoku [Sudoku]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.