lightoj 1397 - Sudoku Solver
思路:每次找出可能情况最少的位置枚举可能情况!!!
poj2676和这题一样不过poj数据比较水,很容易过。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define M 100005
using namespace std;
char str[][];
struct point
{
int x,y;
}p[];
int cnt,tot;
bool is_ok(int x,int y,char c) //判断c是否可以放
{
int u=x/*;
int v=y/*;
for(int i=;i<;i++) //3x3的小格子
for(int j=;j<;j++)
if(str[u+i][v+j]==c) return ;
for(int i=;i<;i++) //行和列
if(str[i][y]==c||str[x][i]==c) return ;
return ;
}
void find_best_way(int &x,int &y) //找出可能情况最少的位置
{
int best=;
for(int i=;i<cnt;i++){
if(str[p[i].x][p[i].y]!='.') continue;
int num=;
for(char j='';j<='';j++)
if(is_ok(p[i].x,p[i].y,j)) num++;
if(num<best) x=p[i].x,y=p[i].y,best=num;
}
}
bool dfs()
{
if(tot==){
for(int i=;i<;i++){
for(int j=;j<;j++)
printf("%c",str[i][j]);
printf("\n");
}
return ;
}
int x,y;
find_best_way(x,y);
tot--;
for(char j='';j<='';j++){
if(is_ok(x,y,j)){
str[x][y]=j;
if(dfs()) return ;
}
}
tot++;
str[x][y]='.';
return ;
}
int main()
{
int t,ca=,n,m,a;
scanf("%d",&t);
while(t--){
cnt=;
for(int i=;i<;i++){
scanf("%s",str[i]);
for(int j=;j<;j++)
if(str[i][j]=='.'){
p[cnt].x=i;
p[cnt++].y=j;
}
}
tot=cnt;
printf("Case %d:\n",++ca);
dfs();
}
return ;
}
lightoj 1397 - Sudoku Solver的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver
1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...
随机推荐
- poj 2377 Bad Cowtractors
题目连接 http://poj.org/problem?id=2377 Bad Cowtractors Description Bessie has been hired to build a che ...
- 【迷你微信】基于MINA、Hibernate、Spring、Protobuf的即时聊天系统 :1.技术简介之Mina连接
欢迎阅读我的开源项目<迷你微信>服务器与<迷你微信>客户端 Apache MINA(Multipurpose Infrastructure for Network Applic ...
- Redis到底该如何利用?【转自:http://www.cnblogs.com/capqueen/p/HowToUseRedis.html】
Redis是个好东西,经过上两个星期的研究和实践,目前正在项目里大规模的替换掉原来的本地内存cache.但是替换过程中却发现,Redis这东西高端,大气上档次,似乎不是我想象里的使用方法. 在没有深入 ...
- js贪吃蛇
function init() { w = 40; m = 20; d = w * m / 2; food = null; dm = new ht.DataModel(); g3d = new ht. ...
- 为SM30视图分配事务代码
Tcode:SE93
- loadView, viewDidLoad 快速使用
一 loadView: 在每次访问 UIViewController时,且其 view = nil 时,会调用这个方法,所以大家在开发中想自己设置 view 的可以用这个方法,在这个方法中自定义 v ...
- Android:apk文件结构
Android apk文件,即Android application package文件. 每个要安装到Android平台的应用都要被编译打包为一个单独的文件,后缀名为.apk,其中包含了应用的二进制 ...
- Invalid object name ‘sys.configurations’. (Microsoft SQL Server, Error: 208)
http://blogs.msdn.com/b/ramaprasanna/archive/2009/09/16/invalid-object-name-sys-configurations-micro ...
- Angular 2 Quickstart
写一个Angular 2的应用最基本的步骤概括为三步:写root组件,启动它(Boostrap),写index.html. 一些关于命名空间的基本知识 把所有代码放入一个立即调用函数中,通过传入一个空 ...
- C实现面向对象封装、继承、多态
参考资料: http://blog.chinaunix.net/uid-26750235-id-3102371.html http://www.eventhelix.com/rea ...