题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5547

数据比较少,直接暴力DFS,检验成立情况即可

AC代码:但是不知道为什么用scanf,printf输入输出就WA了

 /* */
# include <iostream>
# include <stdio.h>
# include <string.h>
# include <cstdlib>
# include <cmath>
# include <climits>
# include <deque>
# include <queue>
# include <stack>
# include <vector>
# include <map>
# include <set>
# include <ctime>
# include <functional>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=5e4+;
const ll mod=1e9+;
char ma[][];
int vis[];
int p[][];
int tx[];
int ty[];
int flag, tot; int check()
{
for(int i=; i<; i++ )///检查每一行
{
vis[]=vis[]=vis[]=vis[]=;
for(int j=; j<; j++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )///填的数重复了不成立
return ;
vis[p[i][j]] = ;
}
} for(int j=; j<; j++ )///检查每一列
{
vis[]=vis[]=vis[]=vis[]=;
for(int i=; i<; i++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )
return ;
vis[p[i][j]] = ;
}
} vis[] = vis[] = vis[] = vis[] = ;
for(int i=; i<; i++ )///检查左上角的1/4方块
{
for(int j=; j<; j++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )
return ;
vis[p[i][j]] = ;
}
} vis[]=vis[]=vis[]=vis[]=;
for(int i=; i<; i++ )///检查右上角的1/4块
{
for(int j=; j<; j++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )
return ;
vis[p[i][j]] = ;
}
} vis[] = vis[] = vis[] = vis[]=;
for(int i=; i<; i++ )///检查左下角1/4块
{
for(int j=; j<; j++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )
return ;
vis[p[i][j]] = ;
}
} vis[] = vis[] = vis[] = vis[] = ;
for(int i=; i<; i++ )///检查右下角1/4块
{
for(int j=; j<; j++ )
{
if( p[i][j]== )
continue;
if( vis[p[i][j]] )
return ;
vis[p[i][j]] = ;
}
}
return ;
} void dfs(int x)
{
if( flag )
return ;
if( x==tot )
{
for(int i=; i<; i++ )
{
for(int j=; j<; j++ )
{
cout<<p[i][j];
//printf("%d", p[i][j]);
}
cout<<endl;
//printf("\n");
}
flag=;
return ;
} for(int i=; i<=; i++ )
{
p[tx[x]][ty[x]]=i;
if( check())
{
dfs(x+);
}
p[tx[x]][ty[x]] = ;
}
} int main()
{
int t;
scanf("%d", &t);
int k=;
//getchar();
while( t-- )
{
for(int i=; i<; i++ )
{
for(int j=; j<; j++ )
{
cin>>ma[i][j];
//scanf("%c", &ma[i][j]);
}
//getchar();
} for(int i=; i<; i++ )
{
for(int j=; j<; j++ )
{
if( ma[i][j]=='*' )
{
p[i][j] = ;
}
else
p[i][j] = ma[i][j] - '';
}
} flag=;
tot = ;
for(int i=; i<; i++ )
{
for(int j=; j<; j++ )
{
if( !p[i][j] )
{
/*依次记录空节点的坐标*/
tx[tot] = i;
ty[tot] = j;
tot++;
}
}
}
printf("Case #%d:\n", k++);
dfs();
}
return ;
}

Sudoku(简单DFS)的更多相关文章

  1. Red and Black(简单dfs)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. POJ 1979 Red and Black (简单dfs)

    题目: 简单dfs,没什么好说的 代码: #include <iostream> using namespace std; typedef long long ll; #define IN ...

  3. POJ1573(Robot Motion)--简单模拟+简单dfs

    题目在这里 题意 : 问你按照图中所给的提示走,多少步能走出来??? 其实只要根据这个提示走下去就行了.模拟每一步就OK,因为下一步的操作和上一步一样,所以简单dfs.如果出现loop状态,只要记忆每 ...

  4. POJ 2676 Sudoku (DFS)

    Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11694   Accepted: 5812   Special ...

  5. poj2676 Sudoku(DFS)

    做了很久还是参考了别人的答案orz,其实也不难啊.我要开始学一下怎么写搜索了... 题目链接:poj2676 Sudoku 题解:暴力搜索,DFS每个空白格子所放数字. #include<cst ...

  6. POJ1979 Red and Black (简单DFS)

    POJ1979 Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  7. POJ 2676 Sudoku (数独 DFS)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14368   Accepted: 7102   Special Judg ...

  8. CF760 C. Pavel and barbecue 简单DFS

    LINK 题意:给出n个数,\(a_i\)代表下一步会移动到第\(a_i\)个位置,并继续进行操作,\(b_i\)1代表进行一次翻面操作,要求不管以哪个位置上开始,最后都能满足 1.到达过所有位置 2 ...

  9. uva 784 Maze Exploration(简单dfs)

    这道题看上去非常麻烦,什么迷宫啊.门之类的,事实上挺简单的,就是让把与 * 连通的都置为 # 包含 * , 直接dfs就能够了,只是我wa了好多次...最后居然是多读了一个换行.忘了加getchar( ...

随机推荐

  1. .NET Core中使用GB2312编码

    原文:.NET Core中使用GB2312编码 .NET Core默认不支持GB2312,如果直接使用Encoding.GetEncoding("GB2312")的时候会抛出异常. ...

  2. [jsp学习笔记]servelt get post

    1.post提交数据是隐式的,get是通过在url里面传递的(可以看一下你浏览器的地址栏),用来传递一些不需要保密的数据. 2.用get时,传输数据的大小有限制 (注意不是参数的个数有限制),为2K: ...

  3. Java隐式类型转换和强制类型转换

    一.强制类型转换 char 和 整型之间的类型转换 char a7 = 'a'; System.out.println(a7); System.out.println( (int)a7 ); Syst ...

  4. 【CH1809】匹配统计(KMP)

    题目链接 摘自https://www.cnblogs.com/wyboooo/p/9829517.html 用KMP先求出以a[i]为结尾的前缀与b匹配的最长长度. 比如 f[i] = j,就表示a[ ...

  5. VS2017 配置 boost_1_70

    1. 下载与安装 1.1 安装方法1 (1) 下载 https://www.boost.org/ 或者使用 https://sourceforge.net/projects/boost/files/b ...

  6. Android Jetpack组件 - ViewModel,LiveData使用以及原理

    本文涉及的源码版本如下: com.android.support:appcompat-v7:27.1.1 android.arch.lifecycle:extensions:1.1.1 android ...

  7. Python——2x和3x的区别汇总

    1. 初始解释器编码: 2x:ascii 编码(不自持中文) 3x:unicode编码 推荐全部更换为utf-8 2. 输出方式不同 2x:print ‘你好’2.7版本的两种都支持 3x:print ...

  8. LNMP - Warning: require(): open_basedir restriction in effect错误解决方法

    LNMP 1.4或更高版本如果不想用防跨目录或者修改.user.ini的防跨目录的目录还需要将 /usr/local/nginx/conf/fastcgi.conf 里面的fastcgi_param ...

  9. keepalived实现nginx反向代理的高可用

    实现keepalived调用脚本进行资源监控 keepalived调用外部的辅助脚本进行资源监控,并根据监控的结果状态能实现优先动态调整 vrrp_script:自定义资源监控脚本,vrrp实例根据脚 ...

  10. Linux磁盘管理——BIOS和UEFI

    参考:BIOS and UEFI - CompTIA A+ 220-901 - 1.1 BIOS and UEFI As Fast As Possible 严格上来说BIOS和UEFI除了在搜索boo ...