题目描述: http://poj.org/problem?id=2676
转自: http://zhangjian110518.blog.163.com/blog/static/74991703200933092722785/
题目技巧性不强,DFS过的,用时16MS。不过写的过程中要注意从后面往前搜。
/************************************************************************/ /*思路: 先从后面开始搜,也就是从第八十个开始搜 1、如果一个小的方格内已经包含了非零的数,则继续向下搜 2、如果一个小的方格内是一个零数,也就是还没有放入相应的数,则对其从零到九开始尝试 3、对每一个数的尝试,检查其合法性:在其所在的3*3方格内是否合适;在此行是否合适:在此列是否合适 4、如果经过以上条件可以的话那么这个数字就可以放在此小方格上,然后继续进行搜索。 ************************************************************************/ #include <cstdio> const int n = 9; int board[9][9]; char ch[10]; //返回当前board[x][y]中的值是否可行 bool ok(int x, int y) { // 3 * 3 for (int i = x / 3 * 3; i < x / 3 * 3 + 3; ++i) { for (int j = y / 3 * 3; j < y / 3 * 3 + 3; ++j) { if (x == i && y == j) continue; if (board[x][y] == board[i][j]) // the number has been used return false; } } int temp = board[x][y]; // check the row for (int j = 0; j < n; ++j) { if (j == y) continue; if (board[x][j] == temp) return false; } // check the column for (int i = 0; i < n; ++i) { if (i == x) continue; if (board[i][y] == temp) return false; } return true; // ok } int dfs(int location) { if (location == -1) return 1; if (board[location / n][location % n] != 0) // has number return dfs(location - 1); else { for (int i = 1; i <= n; ++i) { // try board[location / n][location % n] = i; if (ok(location / n, location % n)) { if (dfs(location - 1)) return 1; } board[location / n][location % n] = 0; } } return 0; } int main() { int nCases; scanf("%d", &nCases); while (nCases--) { for (int i = 0; i < n; ++i) { scanf("%s", ch); for (int j = 0; j < n; ++j) { board[i][j] = ch[j] - '0'; } } dfs(80); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { printf("%d", board[i][j]); } printf("\n"); } } return 0; }
发表评论
-
ACM 之 Java BigInteger
2011-06-01 20:26 0Java 的大整数类在ACM 中大有用武之地 ... -
判断点是否构成多边形, 顶点连续给出
2011-05-26 14:27 0#include <cstdio> #inc ... -
poj pku 1981 Circle and Points 点与圆 位置关系
2011-05-26 11:29 1259题目描述: http://poj.org/problem?id ... -
poj 1032 Parliament 数学
2011-05-25 17:34 1210题目描述: http://poj.org/problem?i ... -
poj 1385 Lifting the Stone 多边形重心
2011-05-25 11:13 1026题目描述: http://poj.org/problem?i ... -
hdoj 2064 汉诺塔III 递推
2011-05-15 22:29 881题目描述: http://acm.hdu.edu.cn/sh ... -
hdoj 1207 汉诺塔II dp 动态规划
2011-05-15 21:22 1663题目描述: http://acm.hdu.edu.cn/sh ... -
poj 2506 Tiling 递推
2011-05-15 11:18 903题目描述: http://poj.org/problem?i ... -
poj 2420 A Star not a Tree? 多边形 费马点
2011-05-14 18:57 1794题目描述: http://poj.org/problem?i ... -
poj 2954 Triangle Pick 定理
2011-05-14 16:36 1081题目描述: http://poj.org/problem?i ... -
poj 1012 Joseph
2011-05-10 17:42 1229题目描述:poj.org/problem?id=10 ... -
zoj 1081 Points Within 点与多边形关系
2011-05-07 17:51 1128题目描述: http://acm.zju.edu.cn/on ... -
poj 1835 宇航员
2011-05-03 17:00 798题目描述:http://poj.org/problem?id ... -
poj 2398 Toy Storage
2011-04-23 20:19 713题目描述:http://www.poj.org/proble ... -
poj 1654 Area 多边形面积
2011-04-23 20:10 894题目描述:http://poj.org/proble ... -
poj 2318 TOYS 点 直线 位置关系
2011-04-23 10:06 663题目描述:http://poj.org/problem?id= ... -
poj pku 1673 EXOCENTER OF A TRIANGLE 三角形 垂心
2011-04-09 16:41 541题目描述:http://poj.org/problem?id= ... -
pc 111303 uva 10195 The Knights Of The Round Table
2011-04-04 16:06 749题目描述:http://www.programming-cha ... -
pc 111302 uva 10180 Rope Crisis in Ropeland!
2011-04-03 20:46 834题目描述: http://www.programming-ch ... -
poj 1971 Parallelogram Counting 平行四边形个数
2011-04-03 10:05 1210题目描述:http://poj.org/problem?id= ...
相关推荐
北大POJ2676-Sudoku 解题报告+AC代码
简单搜索题 数独 答案 POJ 2676 也可以没事玩玩数独。
poj 3435 Sudoku Checker.md
poj 2488——dfs深度优先遍历 //给行数列数,求问能否遍历,给出字典序的一种遍历
NULL 博文链接:https://128kj.iteye.com/blog/1750462
北大POJ3733-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ1020-Anniversary Cake 解题报告+AC代码
北大POJ3373-Changing Digits【DFS+强剪枝】 解题报告+AC代码
北大POJ3009-Curling 2.0【DFS+Vector+回溯+剪枝】 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ1691-Painting A Board 【拓扑+DFS】 解题报告+AC代码
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ1014-Dividing【DFS】【多重背包+二进制优化】 解题报告+AC代码
POJ-2870 Light Up + DFS(1级DFS+1级DFS) + Python - 思维导图 链接:https://blog.csdn.net/xxdragon126/article/details/122095922?spm=1001.2014.3001.5501
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
各种搜索算法,配合POJ上的题目,含标程,及各题解题思路。
北大POJ1159-Palindrome 解题报告+AC代码
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
NULL 博文链接:https://128kj.iteye.com/blog/1745671
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告