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

2023-11-01 05:30
文章标签 基本 poj dfs 2676 九宫

本文主要是介绍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

Southeastern Europe 2005
#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 2676九宫图(很基本的DFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/320797

相关文章

MyBatis-Flex BaseMapper的接口基本用法小结

《MyBatis-FlexBaseMapper的接口基本用法小结》本文主要介绍了MyBatis-FlexBaseMapper的接口基本用法小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具... 目录MyBATis-Flex简单介绍特性基础方法INSERT① insert② insertSelec

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

C++中使用vector存储并遍历数据的基本步骤

《C++中使用vector存储并遍历数据的基本步骤》C++标准模板库(STL)提供了多种容器类型,包括顺序容器、关联容器、无序关联容器和容器适配器,每种容器都有其特定的用途和特性,:本文主要介绍C... 目录(1)容器及简要描述‌php顺序容器‌‌关联容器‌‌无序关联容器‌(基于哈希表):‌容器适配器‌:(

windos server2022里的DFS配置的实现

《windosserver2022里的DFS配置的实现》DFS是WindowsServer操作系统提供的一种功能,用于在多台服务器上集中管理共享文件夹和文件的分布式存储解决方案,本文就来介绍一下wi... 目录什么是DFS?优势:应用场景:DFS配置步骤什么是DFS?DFS指的是分布式文件系统(Distr

使用Python进行文件读写操作的基本方法

《使用Python进行文件读写操作的基本方法》今天的内容来介绍Python中进行文件读写操作的方法,这在学习Python时是必不可少的技术点,希望可以帮助到正在学习python的小伙伴,以下是Pyth... 目录一、文件读取:二、文件写入:三、文件追加:四、文件读写的二进制模式:五、使用 json 模块读写

基本知识点

1、c++的输入加上ios::sync_with_stdio(false);  等价于 c的输入,读取速度会加快(但是在字符串的题里面和容易出现问题) 2、lower_bound()和upper_bound() iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。 iterator upper_bou

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int