HDOJ5547 SudoKu

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547

题目大意:填数独。。。

思路:爆搜

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <algorithm>
 5 using namespace std;
 6 bool row[5][5],col[5][5],siz[5][5];
 7 int G[5][5];
 8 bool flag;
 9 int Num(int x,int y){
10     if(x<=2&&y<=2) return 1;
11     if(x<=2&&y>2) return 2;
12     if(x>2&&y<=2) return 3;
13     if(x>2&&y>2) return 4;
14 }
15 void dfs(int x,int y){
16     if(y>4&&x==4){
17         flag=true;
18         return ;
19     }
20     if(flag) return ;
21     if(y>4) x++,y=1;
22     if(G[x][y]) {
23         dfs(x,y+1);
24         return;
25     }
26     if(flag) return ;
27     for(int i=1;i<=4;i++){
28         if(!row[x][i]&&!col[y][i]&&!siz[Num(x,y)][i]){
29             G[x][y]=i;
30             row[x][i]=col[y][i]=siz[Num(x,y)][i]=true;
31             dfs(x,y+1);
32             if(flag) return ;
33             G[x][y]=0;
34             row[x][i]=col[y][i]=siz[Num(x,y)][i]=false;
35         }
36     }
37 }
38 void init(){
39     flag=false;
40     memset(row,false,sizeof(row));
41     memset(col,false,sizeof(col));
42     memset(siz,false,sizeof(siz));
43 }
44 void solve(int T){
45     printf("Case #%d:\n",T);
46     init();
47     for(int x=1;x<=4;x++){
48         for(int y=1;y<=4;y++){
49             char tmp;
50             scanf(" %c",&tmp);
51             if(tmp==‘*‘){
52                 G[x][y]=0;
53             }
54             else {
55                 G[x][y]=tmp-‘0‘;
56                 int now=tmp-‘0‘;
57                 row[x][now]=col[y][now]=siz[Num(x,y)][now]=true;
58             }
59         }
60     }
61     dfs(1,1);
62     for(int i=1;i<=4;i++){
63         for(int j=1;j<=4;j++){
64             printf("%d",G[i][j]);
65         }
66         printf("\n");
67     }
68 }
69 int main(){
70     int T;
71     //freopen("C:\\Users\\acm\\Desktop\\ACM\\out.txt","w",stdout);
72     scanf("%d",&T);
73     for(int i=1;i<=T;i++) solve(i);
74 }
时间: 2024-12-26 05:21:53

HDOJ5547 SudoKu的相关文章

LeetCode37 Sudoku Solver

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red.  (Hard)

*Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partially

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

POJ Sudoku 数独填数 DFS

题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   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 Fig

Valid Sudoku leetcode

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. 判断九宫格的合理性(并不一定有解),只需要依次判断行.列.9个子九宫格是否

LeetCode OJ:Valid Sudoku(有效数独问题)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下

Java [leetcode 37]Sudoku Solver

题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 解题思路:

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F