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

相关文章

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Java Instrumentation从概念到基本用法详解

《JavaInstrumentation从概念到基本用法详解》JavaInstrumentation是java.lang.instrument包提供的API,允许开发者在类被JVM加载时对其进行修改... 目录一、什么是 Java Instrumentation主要用途二、核心概念1. Java Agent

Kotlin 协程之Channel的概念和基本使用详解

《Kotlin协程之Channel的概念和基本使用详解》文章介绍协程在复杂场景中使用Channel进行数据传递与控制,涵盖创建参数、缓冲策略、操作方式及异常处理,适用于持续数据流、多协程协作等,需注... 目录前言launch / async 适合的场景Channel 的概念和基本使用概念Channel 的

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

DNS查询的利器! linux的dig命令基本用法详解

《DNS查询的利器!linux的dig命令基本用法详解》dig命令可以查询各种类型DNS记录信息,下面我们将通过实际示例和dig命令常用参数来详细说明如何使用dig实用程序... dig(Domain Information Groper)是一款功能强大的 linux 命令行实用程序,通过查询名称服务器并输

MySql基本查询之表的增删查改+聚合函数案例详解

《MySql基本查询之表的增删查改+聚合函数案例详解》本文详解SQL的CURD操作INSERT用于数据插入(单行/多行及冲突处理),SELECT实现数据检索(列选择、条件过滤、排序分页),UPDATE... 目录一、Create1.1 单行数据 + 全列插入1.2 多行数据 + 指定列插入1.3 插入否则更

C#连接SQL server数据库命令的基本步骤

《C#连接SQLserver数据库命令的基本步骤》文章讲解了连接SQLServer数据库的步骤,包括引入命名空间、构建连接字符串、使用SqlConnection和SqlCommand执行SQL操作,... 目录建议配合使用:如何下载和安装SQL server数据库-CSDN博客1. 引入必要的命名空间2.