Nyoj 722 Sudoku

Source: Internet
Author: User
SudokuTime Limit: 1000 MS | memory limit: 65535 kb difficulty: 4
Description

Sudoku is a logic game that uses paper and pen for calculation. Based on the known numbers on the 9x9 disc, the player must deduce the numbers with all the remaining spaces, the numbers in each row, column, and every 3*3 intrauterine devices contain 1-9 and are not repeated. Each qualified Sudoku puzzle has only one answer, and the reasoning method is also based on this. Any questions without any solution or multiple solutions are unqualified.

One day, hrdv encountered a challenge that claimed to be the world's most difficult Sudoku. As a qualified programmer, he decided to compile a program to solve the problem ..

 
Input
The first row has a number of N (0 <n <100), indicating that there are N groups of test data, each group of test data is composed of 9x9 nine cells, 0 indicates that the corresponding grid is empty.
Output
Output A 9*9 nine-square-meter lattice for this Sudoku answer
Sample Input
10 0 5 3 0 0 0 0 08 0 0 0 0 0 0 2 00 7 0 0 1 0 5 0 04 0 0 0 0 5 3 0 00 1 0 0 7 0 0 0 60 0 3 2 0 0 0 8 00 6 0 5 0 0 0 0 90 0 4 0 0 0 0 3 00 0 0 0 0 9 7 0 0
Sample output
1 4 5 3 2 7 6 9 8 8 3 9 6 5 4 1 2 7 6 7 2 9 1 8 5 4 3 4 9 6 1 8 5 3 7 2 2 1 8 4 7 3 9 5 6 7 5 3 2 9 6 4 8 1 3 6 7 5 4 2 8 1 9 9 8 4 7 6 1 2 3 5 5 2 1 8 3 9 7 6 4 
Source
Original
Uploaded
TC _ Hu rendong

Problem solving: DFS, er ....... Dancing link does not ......

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <ctype.h> 8 #include <cmath> 9 #include <algorithm>10 #define LL long long11 using namespace std;12 int d[9][9];13 bool isValid(int x,int y,int val) {14     int i,j,a = x/3,b=y/3;15     for(i = 0; i < 9; i++)16         if(d[x][i] == val || d[i][y] == val) return false;17     for(i = 0; i < 3; i++) {18         for(j = 0; j < 3; j++) {19             if(d[i+a*3][j+b*3] == val) return false;20         }21     }22     return true;23 }24 bool  dfs(int cur) {25     if(cur == 81) return true;26     if(d[cur/9][cur%9])27         return dfs(cur+1);28     for(int i = 1; i < 10; i++) {29         if(isValid(cur/9,cur%9,i)){30             d[cur/9][cur%9] = i;31             if(dfs(cur+1)) return true;32         }33     }34     d[cur/9][cur%9] = 0;35     return false;36 }37 int main() {38     int kase,i,j;39     scanf("%d",&kase);40     while(kase--) {41         for(i = 0; i < 9; i++) {42             for(j = 0; j < 9; j++)43                 scanf("%d",d[i]+j);44         }45         dfs(0);46         for(i = 0; i < 9; i++){47             for(j = 0; j < 8; j++)48                 printf("%d ",d[i][j]);49             printf("%d\n",d[i][j]);50         }51     }52     return 0;53 }
View code
 
  

 

 

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.