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 ...
随机推荐
- C#.Net 图片处理大全
C# How to: Image filtering by directly manipulating Pixel ARGB values C# How to: Image filtering imp ...
- 关于Filezilla是否支持sftp
我们知道filezilla是一个开源的ftp的解决方案,它提供了客户端和服务器端,支持的fpt, sftp, ftps,这是你可以从wiki中看到的关于filezilla的介绍,但是这里需要澄清一点就 ...
- linux 捕获信号处理中遇到的死锁
tag: 信号 signal sigchld 死锁 堆栈 我们的程序需要捕获信号自己处理,所以尝试对1-32的信号处理(后面33-64的信号不处理).但是在调试代码时,发现一个线程死锁的问题.程序 ...
- iOS之走进精益编程01
Model类 .h #import <Foundation/Foundation.h> @interface Product : NSObject @property (nonatomic ...
- verilog 奇数分频设计
module tw(clk,k_or,k1,k2); input clk; output k_or,k1,k2; reg [2:0] c1,c2; reg m1,m2; initial begin c ...
- 零基础学习Linux(二)网页乱码问题
上次的博文零基础学习Linux(一)环境搭建中我们已经将Linux环境搭建完毕了,接下来我们就可以进行相关的操作了,在进行操作之前,我们先来看一下大家可能遇到的中文网页乱码问题. 1.问题演示 a)输 ...
- UIScrowView swift
// // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...
- 路由设置 windows
打印路由信息: route print 如何临时添加电脑内部路由[ route add 网段 mask 子网掩码 网关] 例如:route add 172.18.0.0 mask 255.255.0. ...
- 关于四则运算的代码debug测试
1.首先检测题目是否能为负数,0? 截图: 总结:如图所示出题数目为0的时候,并没提示重新输入,而是输出空白,而当输出题目为负数的时候系统提示错误,并且提示终止 2.检测操作值得范围: 总结:当操 ...
- MVC5 自定义表单错误信息
1.