Poj 2918 tudoku [Search]

Source: Internet
Author: User

Like poj2676 ,,,

Forgive me for the number of water questions = !...

#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;for(int times=1;times<=test;times++){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);cout<<"Scenario #"<<times<<":\n";for(int i=1;i<=9;i++){for(int j=1;j<=9;j++){cout<<map[i][j];}cout<<'\n';}cout<<'\n';}return 0;}


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.