ACM : POJ 2676 SudoKu DFS - 数独
Description
Input
Output
Sample Input
1
103000509
002109400
000704000
300502006
060000050
700803004
000401000
009205800
804000107
Sample Output
143628579
572139468
986754231
391542786
468917352
725863914
237481695
619275843
854396127
/*/
数独 AC代码:
/*/
#include"iostream"
#include"cstdio"
#include"cstring"
#include"queue"
#include"string"
using namespace std;
int map[11][11],count,n;
bool F[11][11];
bool H[11][11];
bool L[11][11]; struct Node {
int x,y;
} node[100]; int MAP(int i,int j) {
if(i<=3&&j<=3)return 1;
else if(i<=3&&j<=6)return 2;
else if(i<=3&&j<=9)return 3;
else if(i<=6&&j<=3)return 4;
else if(i<=6&&j<=6)return 5;
else if(i<=6&&j<=9)return 6;
else if(i<=9&&j<=3)return 7;
else if(i<=9&&j<=6)return 8;
else if(i<=9&&j<=9)return 9;
} int DFS(int n) {
if(n>count) {
return 1;
}
for(int j=1; j<=9; j++) {
if(!H[node[n].x][j]&&!L[node[n].y][j]&&!F[MAP(node[n].x,node[n].y)][j]) {
H[node[n].x][j]=L[node[n].y][j]=F[MAP(node[n].x,node[n].y)][j]=1;
map[node[n].x][node[n].y]=j;
if(DFS(n+1))return 1;
H[node[n].x][j]=L[node[n].y][j]=F[MAP(node[n].x,node[n].y)][j]=0;
}
}
return 0;
} int main() {
int flag=0;
while(cin>>n) {
for(int i=1; i<=n; i++) {
memset(map,0,sizeof(map));
memset(H,0,sizeof(H));
memset(F,0,sizeof(F));
memset(L,0,sizeof(L));
count = 0;
for(int j=1; j<=9; j++)
for(int k=1; k<=9; k++) {
scanf("%1d",&map[j][k]);
if(map[j][k]==0) {
count++;
node[count].x=j;
node[count].y=k;
} else {
H[j][map[j][k]]=1;
L[k][map[j][k]]=1;
F[MAP(j,k)][map[j][k]]=1;
}
}
DFS(1);
for(int j=1; j<=9; j++) {
for(int k=1; k<=9; k++)
cout<<map[j][k];
cout<<endl;
} }
}
return 0;
}
ACM : POJ 2676 SudoKu DFS - 数独的更多相关文章
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- 深搜+回溯 POJ 2676 Sudoku
POJ 2676 Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17627 Accepted: 8538 ...
- POJ 2676 Sudoku (数独 DFS)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14368 Accepted: 7102 Special Judg ...
- POJ 2676 - Sudoku - [蓝桥杯 数独][DFS]
题目链接:http://poj.org/problem?id=2676 Time Limit: 2000MS Memory Limit: 65536K Description Sudoku is a ...
- POJ - 2676 Sudoku 数独游戏 dfs神奇的反搜
Sudoku Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smalle ...
- 搜索 --- 数独求解 POJ 2676 Sudoku
Sudoku Problem's Link: http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...
- ACM: ICPC/CCPC Sudoku DFS - 数独
Sudoku Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other) Total Submis ...
- POJ 2676 Sudoku (DFS)
Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11694 Accepted: 5812 Special ...
- DFS POJ 2676 Sudoku
题目传送门 题意:数独问题,每行每列以及每块都有1~9的数字 分析:一个一个遍历会很慢.先将0的位子用vector存起来,然后用rflag[i][num] = 1 / 0表示在第i行数字num是否出现 ...
随机推荐
- linux常用命令-压缩解压命令
gzip [文件] 压缩后文件格式 .gz,这个命令只能压缩文件,不能压缩目录.并且这个命令压缩后不保留源文件 gunzip [文件] 或者 gzip -d [文件] 解压缩.gz的压缩文件 tar ...
- js中Window 对象及其的方法
window.location 对象 window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面.window.location 对象在编写时可不使用 wind ...
- PXE+Kickstart+DHCP+TFTP实现无人值守安装操作系统
PXE+Kickstart+DHCP+TFTP实现无人值守安装操作系统 PXE + Kickstart PXE的工作流程及配置文件 Kickstart的配置文件 Linux安装大致可以分为2个阶段 第 ...
- powershell例子
例子如下: $ErrorActionPreference="Stop" function getlist{ ls D:\tmp2|select name,extension,ful ...
- Unity3D 计算FPS
using UnityEngine; using System.Collections; public class FPS : MonoBehaviour { private const string ...
- 如何在Linux上使用文件作为内存交换区(Swap Area)
交换区域(Swap Area)有什么作用? 交换分区是操作系统在内存不足(或内存较低)时的一种补充.通俗的说,如果说内存是汽油,内存条就相当于油箱,交换区域则相当于备用油箱. Ubuntu Linux ...
- day7
本节作业: 选课系统 角色:学校.学员.课程.讲师要求:1. 创建北京.上海 2 所学校2. 创建linux , python , go 3个课程 , linux\py 在北京开, go 在上海开3. ...
- 序列化多个form表单内容同时提交
一.首先将表单主体序列化为json对象. 方法: //将表单序列化为json,这里加了个jQuery的扩展方法 $.fn.serializeJson = function () { var resul ...
- SQL 基础
创建模式 create schema <schema_name> authorization <username> 没有指定schema_name时默认是用户名 删除模式 dr ...
- 更改WAS Profiles的概要文件的server1的SDK版本
WebSphere只能使用IBM JDK 哦,不能使用sun的JDK哦.不过如果只是改jdk的版本的话可以参考如下步骤:(以集群为例,假设具有管理节点Dmgr01,应用概要AppSrv01) 1. 确 ...