思路:每次找出可能情况最少的位置枚举可能情况!!!

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的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  2. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  3. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  4. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  5. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  6. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  7. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  8. Valid Sudoku&&Sudoku Solver

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  9. 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 ...

随机推荐

  1. DeviceOne开发HelloWord

    http://www.cnblogs.com/wjiaonianhua/p/5278061.html http://www.jb51.net/article/75693.htm 2015 年 9 月 ...

  2. Swift基础小结_2

    import Foundation // MARK: - ?和!的区别// ?代表可选类型,实质上是枚举类型,里面有None和Some两种类型,其实nil相当于OPtional.None,如果非nil ...

  3. Qt入门1---widget、mainwindow和Dialog区别

    摘要: 看了一个月的Qt,居然没有理清Qt中 ------------------------------------ 1.QMainWindow A main window provides a f ...

  4. Quartus13.0破解方法

    一定要按照步骤顺序才能破解,这里很关键 1.下载和打开Quartus II破解器,选择“应用”,选择“是”,找到bin(64位系统是bin64)目录下的sys_cpt.dll,“打开” 2.然后将li ...

  5. iOS 进阶 第十四天(0416)

    0416 注意调用关系,如下图: 就是initWithCoder:.initWithFrame.setup方法三元组

  6. CAD字体显示错乱问题解决方案

    最近这两天一直在画竣工图,用CAD用得挺多的,所以老是发现一些问题.今天在打开别人发过来的图纸时,我看到竟然还有钢筋符号无法显示…… 像这种问题的解决,据我所知就两种方法: 一.替换使用的字体 首先选 ...

  7. 如何查看系统内置的shell命令及帮助信息

    1.打开终端,输入help命令可以查看系统默认的shell(通常是bash)的内置的shell命令列表: [tansheng@localhost ~]$ help GNU bash, version ...

  8. C++中的链表节点用模板类和用普通类来实现的区别

    C++中的链表节点通常情况下类型都是一致的.因此我们可以用模板来实现. #include <iostream> using namespace std; template<typen ...

  9. C++中的抽象类及纯虚函数的实现与否

    1.含有纯虚函数的叫抽象类 2.抽象类(一般是基类)中的纯虚函数无论函数体实现与否,都没有关系,系统会自动忽略 3.继承自抽象类的子类,必须要实现父类的纯虚函数才可以实例化对象 4.抽象类不允许实例化 ...

  10. Daily Scrum4

    今天我们小组开会内容分为以下部分: part1:与负责这个项目的其他组进行会晤; part2:组内成员召开了简短会议,进行工作安排; part3:总结今日工作,对项目遇到的问题商讨解决办法; ◆Par ...