POJ2676

Problem

分析:我们可以这样考虑,如果一个数在位置(x,y)出现过了,则在第x行,第y列,以及(x,y)所对应的3*3所对应的方格中都不会出现,由此我们用三个数组标记这三个变量,然后在对于没有填数的位置进行dfs即可

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "vector"
 5 using namespace std;
 6 const int maxn=15;
 7
 8 int row[maxn][maxn]; //在第i行填入了num
 9 int col[maxn][maxn]; //在第j列填入了num
10 int pos[maxn][maxn]; //在(i,j)所对应的方块中填入了num
11 int res[maxn][maxn]; //记录最终值
12 typedef struct{
13     int r,c;
14 }point;
15 vector<point> vis; //记录没有填数的位置
16
17 int getpos(int i,int j){  //获取(i,j)所在方块号
18     int ii=i/3;
19     int jj=j/3;
20     return ii*3+jj;
21 }
22
23 void setAll(int i,int j,int num,int f){   //在(i,j)填入num
24     row[i][num]=f;
25     col[j][num]=f;
26     pos[getpos(i,j)][num]=f;
27 }
28
29 bool judge(int i,int j,int num){    //判断(i,j)位置能不能填数
30     if((!row[i][num])&&(!col[j][num])&&(!pos[getpos(i,j)][num]))
31         return true;
32     return false;
33 }
34
35 bool dfs(int n){  //填数
36     if(n<0)
37         return true;
38     for(int i=1;i<=9;i++){
39         int x=vis[n].r,y=vis[n].c;
40         if(judge(x,y,i)){
41             setAll(x,y,i,1);
42             res[x][y]=i;
43             if(dfs(n-1))
44                 return true;
45             setAll(x,y,i,0);
46         }
47     }
48     return false;
49 }
50
51 int main()
52 {
53     int T;
54     cin>>T;
55     while(T--){
56         memset(row,0,sizeof(row));
57         memset(col,0,sizeof(col));
58         memset(pos,0,sizeof(pos));
59         memset(res,0,sizeof(res));
60         vis.clear();
61         for(int i=0;i<9;i++){
62             for(int j=0;j<9;j++){
63                 char ch;
64                 cin>>ch;
65                 int cnt=ch-‘0‘;
66                 if(cnt){
67                     res[i][j]=cnt;
68                     setAll(i,j,cnt,1);
69                 }else{
70                     point a;
71                     a.r=i,a.c=j;
72                     vis.push_back(a);
73                 }
74             }
75         }
76         if(dfs(vis.size()-1)){
77             for(int i=0;i<9;i++){
78                 for(int j=0;j<9;j++){
79                     printf("%d",res[i][j]);
80                 }
81                 printf("\n");
82             }
83         }
84     }
85     return 0;
86 }

时间: 2024-08-07 00:17:12

POJ2676的相关文章

POJ2676 Sudoku [数独]

好题,也很实用,犯了几个错误 1.在枚举赋值的时候,思维有个错误:当当前的赋值不能填完这个数独,应该是继续下一个循环,而不是return false 终止枚举 2.Generic Programing写错了,,,本来那个memset想写成Generic Programing的,,,然后,永远只有第一组结果对 不说了,泪哈,,, #include <cstdio> #include <cstring> #include <iostream> #include <cs

poj2676 Sudoku(DFS)

做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 bool row_f[9][

POJ2676 Sudoku - DFS

POJ2676 Sudoku 传送门 题意: 填充未完成的数独...(就这么简单.... 思路: 爆搜即可. 可行性剪枝:用三个\(bool\)数组分别记录行.列.\(3*3\)的块中,\(9\)种数字的使用情况 AC Code: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N=9+10; char s[N][N];int mp[N][N];

POJ2676 2918 Sudoku 暴搜

#include <cstdio> #include <iostream> #include <iostream> #include <algorithm> using namespace std; int s[][2] = {{1,1},{1,4},{1,7},{4,1},{4,4},{4,7},{7,1},{7,4},{7,7}}; int a[10][10]; int ch[] = {0,0,0,1,1,1,2,2,2, 0,0,0,1,1,1,2,2

poj2676——dfs

POJ 2676  dfs Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14640   Accepted: 7217   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 o

POJ2676 Sudoku

Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21055   Accepted: 10043   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.

POJ--2676&amp;HDU--1421(数独,dfs)

Sudoku Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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

POJ2676 Sticks

木棒 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 147267   Accepted: 34890 Description 乔 治拿来一组等长的木棒,将它们随机地砍断,使得每一节木棍的长度都不超过50个长度单位.然后他又想把这些木棍恢复到为裁截前的状态,但忘记了初始时有多 少木棒以及木棒的初始长度.请你设计一个程序,帮助乔治计算木棒的可能最小长度.每一节木棍的长度都用大于零的整数表示. Input 输入包含多组数据,每

[DFS]poj2676(2918)

题意: 数独问题,给出一些点,然后要求填满格子.每行没列1-9不能重复,每个3*3的小格子也不能重复. 分析: 暴力搜索,那么主要的问题就是每次如何判断是否冲突.行和列的比较好想,row[i][j]=1表示第i行的j已经被占了,col[i][j]=1表示第i列的j已经被占了,那么每个小格子呢? 首先,每行每列都是012.345.678那么每个j/3表示它在这以行的第j/3个格子里,i/3表示在第i/3行的格子,那么i/3*3+j/3就表示该点处于那个小格子里,同理g[i][j]表示第i个格子里的