poj 2676九宫图(很基本的DFS)

   做这题主要学会了如何DFS一个九宫格,举一反三,以后遇到这种题目可别再不会了,一开始我想的是用八皇后的方法,发现不行啊,如何判断这行这列有没有用这个数字呢。最后的办法是建立数组row[][],col[][],grid[][],还有一个是大格和小九宫格的换算公式:3*(i/3)+j/3;

                                                                                                      Sudoku
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9748 Accepted: 4827 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. In some of the cells are written decimal digits from 1 to 9. The other cells are empty. The goal is to fill the empty cells with decimal digits from 1 to 9, one digit per cell, in such way that in each row, in each column and in each marked 3x3 subsquare, all the digits from 1 to 9 to appear. Write a program to solve a given Sudoku-task.

Input

The input data will start with the number of the test cases. For each test case, 9 lines follow, corresponding to the rows of the table. On each line a string of exactly 9 decimal digits is given, corresponding to the cells in this line. If a cell is empty it is represented by 0.

Output

For each test case your program should print the solution in the same format as the input data. The empty cells have to be filled according to the rules. If solutions is not unique, then the program may print any one of them.

Sample Input

1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107

Sample Output

143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127

Source

 
#include <iostream>
#include <fstream>
#include <cstdio>
using namespace std;
int n;
int map[10][10];
bool row[10][10]; //记录每行的数字是否可行
bool col[10][10]; //记录没列的数字是否可行
bool grid[10][10]; //记录每个九宫格的数字是否可行

void getmap()
{
char s[10];
for(int i=0 ;i<9 ;i++)
{
getchar();
scanf("%s",s);
for(int j=0; j<9; j++)
{
map[i][j]=s[j]-'0';
if(map[i][j]!=0)
{
int num=map[i][j];
int k=3*(i/3)+j/3; // 这个公式很关键。这是求每个点对应的九宫格
row[i][num]=true;
col[j][num]=true;
grid[k][num]=true;
}
}
}
}

bool dfs(int i,int j)
{
bool flag=false;
if(i==9) return true;
if(map[i][j])
{
if(j==8)
{
flag=dfs(i+1,0);
}
else
{
flag=dfs(i,j+1);
}
if(flag) //在这回溯,但不改变map的值,只起到一个传递的作用
return true;
else
return false;
}
else
{
int k=3*(i/3)+j/3;
for(int x=1; x<=9; x++)
{
if(!row[i][x] && !col[j][x] && !grid[k][x])
{
map[i][j]=x;
row[i][x]=true;
col[j][x]=true;
grid[k][x]=true;
if(j==8)
{
flag=dfs(i+1,0);
}
else
{
flag=dfs(i,j+1);
}

if(!flag) //这也是一个回溯
{
map[i][j]=0;
row[i][x]=false;
col[j][x]=false;
grid[k][x]=false;
}
else
{
return true;
}
}//if
}//for
}
return false;
}

void output()
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
printf("%d",map[i][j]);
}
printf("\n");
}
}

int main()
{
freopen("acm.txt","r",stdin);
scanf("%d",&n);
while(n--)
{
memset(row,false,sizeof(row));
memset(col,false,sizeof(col));
memset(grid,false,sizeof(grid));
getmap();
dfs(0,0);
output();
}
return 0;
}
POJ 1321 排兵布阵问题可以使用 DFS 算法求解。 题目要求在一个 n x n 的棋盘上,放置 k 个棋子,其中每行、每列都最多只能有一个棋子。我们可以使用 DFS 枚举每个棋子的位置,对于每个棋子,尝试将其放置在每一行中未被占用的位置上,直到放置了 k 个棋子。在 DFS 的过程中,需要记录每行和每列是否已经有棋子,以便在尝试放置下一个棋子时进行判断。 以下是基本DFS 模板代码: ```python def dfs(row, cnt): global ans if cnt == k: ans += 1 return for i in range(row, n): for j in range(n): if row_used[i] or col_used[j] or board[i][j] == '.': continue row_used[i] = col_used[j] = True dfs(i + 1, cnt + 1) row_used[i] = col_used[j] = False n, k = map(int, input().split()) board = [input() for _ in range(n)] row_used = [False] * n col_used = [False] * n ans = 0 dfs(0, 0) print(ans) ``` 其中,row 代表当前尝试放置棋子的行数,cnt 代表已经放置的棋子数量。row_used 和 col_used 分别表示每行和每列是否已经有棋子,board 则表示棋盘的状态。在尝试放置棋子时,需要排除掉无法放置的位置,即已经有棋子的行和列,以及棋盘上标记为 '.' 的位置。当放置了 k 个棋子时,即可计数一次方案数。注意,在回溯时需要将之前标记为已使用的行和列重新标记为未使用。 需要注意的是,在 Python 中,递归深度的默认限制为 1000,可能无法通过本题。可以通过以下代码来解除限制: ```python import sys sys.setrecursionlimit(100000) ``` 完整代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值