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

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. [原创]使用benchmarksql和pgbench对PostgreSQL Plus Advanced Server进行性能测试

    一.测试环境 benchmarksql version:4.0.8 rhel 6.3 vmware esxi 二.理解benchmarksql性能测试原理TPC-C 1.理解TPC-C TPC-C模拟 ...

  2. memcached 高级机制(二)

    memcached删除机制 a) (1)有内存机制里说明了,这里会运用到LRU删除机制.我们知道,当我们在add或set一个值时,我们会设置这个值得期限.当某个值过期后,这个值并没有从内存中删除,我们 ...

  3. Android--将Bitmip转化成字符串

    因为自己做的东西想要上传到服务器,所以就选择了将Bitmip转化成了字符串在上传 其它格式的图片我们好像可以用Bitmap.Factory 去将他们转化成BitMap 转化成字符串的代码 //将bit ...

  4. IOS中 如何去除Tabview里面cell之间的下划线

    可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...

  5. MVC 中如何将带有标签的字符串转换为HTML 标签 显示出来?

    出现问题的现象:

  6. [小技巧]让你的GridView支持IQueryable,并自动实现真分页

    众所周知,asp.net自带的GridView在自带分页方面设计得很2,因为它是假分页,即内存分页.而且它不智能支持强大的Iqueryable. 但这表明微软忽略了现实中的分页需求吗?答案应该不是,我 ...

  7. Mono for Android (2)-- Android应用程序初认识

    一:日志记录 先添加using Android.Util; 在该命名控件下有log类 Log.Info("HA", "End onCreate"); //记录消 ...

  8. VC++编程中为程序加入启动画面功能

     如何为自己的程序加入启动画面 观察我们平常使用的软件,当我们双击软件的时候,会在主界面出现前,先行出现一个启动画面,由于前一阵子写了一个基于对话框的程序,亲自实验了下,今天就为大家简单的介绍下,在我 ...

  9. 微软职位内部推荐-Senior SDE

    微软近期Open的职位: Position: Senior SDE-- Mobile Products Android/iOS/WP Senior Developer Contact Person: ...

  10. 如何解决读取到文件末尾时碰到EOF导致的重复输出或者无用输出

    当读取到文件末尾时,会碰到EOF,如何解决呢?    方法一:我们可以通过(ch=fin.get())!=EOF来结束读取,这样就不会像eof()那样碰到EOF之后,还会再进行一次读取,导致输出一个无 ...