当前位置: 首页 > news >正文

HDU 5547 Sudoku(DFS)

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

题目:

Sudoku

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2372    Accepted Submission(s): 804

Problem Description
 
Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller.

Actually, Yi Sima was playing it different. First of all, he tried to generate a  4×4 board with every row contains 1 to 4, every column contains 1 to 4. Also he made sure that if we cut the board into four 2×2 pieces, every piece contains 1 to 4.

Then, he removed several numbers from the board and gave it to another guy to recover it. As other counselors are not as smart as Yi Sima, Yi Sima always made sure that the board only has one way to recover.

Actually, you are seeing this because you've passed through to the Three-Kingdom Age. You can recover the board to make Yi Sima happy and be promoted. Go and do it!!!
 
Input
 
The first line of the input gives the number of test cases,  T(1T100)T test cases follow. Each test case starts with an empty line followed by 4 lines. Each line consist of 4 characters. Each character represents the number in the corresponding cell (one of '1', '2', '3', '4'). '*' represents that number was removed by Yi Sima.

It's guaranteed that there will be exactly one way to recover the board.
 
Output
 
For each test case, output one line containing  Case #x:, where  x is the test case number (starting from 1). Then output 4 lines with 4 characters each. indicate the recovered board.
 
Sample Input
 
3
****
2341
4123
3214
*243
*312
*421
*134
*41*
**3*
2*41
4*2*
 
Sample Output
Case #1:
1432
2341
4123
3214
Case #2:
1243
4312
3421
2134
Case #3:
3412
1234
2341
4123
 
题意:
4*4的数独游戏,最终要凑成每行每列每个分块的值为1+2+3+4;即1-4每行每列每块不能重复出现。
 
思路:
用DFS做,将已经填的空数为判断标记,都填好即输出并返回。列和行比较好判断,至于分块,我定义了一个计算的方式:t=row*2/2+col(row和col都是从0开始的,t表示为第几个分块)。这样很明显,从左上角的4个小分块到右下角的4个小分块分别对应的就是0-3。
 
代码:
 1 #include <cstdio>
 2 #include <vector>
 3 #include <cstring>
 4 using namespace std;
 5 struct node{
 6     int r,c;
 7 };
 8 vector<node>v;
 9 char chess[10][10];
10 int col[10][10];//col[i][x]标记第i列 x数是否已经用过
11 int row[10][10];//row[i][x]标记第i行 x数是否已经用过
12 int block[10][10];//block[i][x]标记第i个分块,x数是否已经用过
13 int ok;
14 void dfs(int num){
15     if(ok)  return ;
16     if(num==v.size()){//所有数都填好
17         for (int i=0; i<4; i++) {
18             puts(chess[i]);
19         }
20         ok=1;
21         return ;
22     }
23     for(int i=1;i<=4;i++){
24         int r=v[num].r;
25         int c=v[num].c;
26         if(!col[c][i] && !row[r][i] && !block[r/2*2+c/2][i]){
27             chess[r][c]=i+'0';
28             col[c][i]=row[r][i]=1;
29             block[r/2*2+c/2][i]=1;
30             dfs(num+1);
31             col[c][i]=row[r][i]=0;//回溯还原状态
32             block[r/2*2+c/2][i]=0;
33             chess[r][c]='*';
34         }
35     }
36 }
37 int main(){
38     int t;
39     scanf("%d",&t);
40     for (int i=1; i<=t; i++) {
41         printf("Case #%d:\n",i);
42         v.clear();
43         memset(block, 0, sizeof(block));//初始值都为0,即都未用过
44         memset(col, 0, sizeof(col));
45         memset(row, 0, sizeof(row));
46         ok=0;
47         for (int j=0; j<4; j++) {
48             scanf("%s",chess[j]);
49             for (int k=0; k<4; k++) {
50                 if(chess[j][k]=='*'){//将需要填空的点放入vector容器
51                     node x;
52                     x.r=j;x.c=k;
53                     v.push_back(x);
54                 }else{
55                     int x=chess[j][k]-'0';//标记已经用过的点
56                     block[j/2*2+k/2][x]=1;
57                     row[j][x]=1;
58                     col[k][x]=1;
59                 }
60             }
61         }
62         dfs(0);//0表示在填空v[0]点
63     }
64     return 0;
65 }

 

转载于:https://www.cnblogs.com/uniles/p/7196555.html

相关文章:

  • FreeRTOS 消息队列
  • lintcode-109-数字三角形
  • 关于python ide
  • git学习
  • SVM
  • Java编码格式
  • 【SignalR学习系列】2. 第一个SignalR程序
  • Passing the Message 单调栈两次
  • SSM搭建
  • 格式化angularjs日期'/Date(-62135596800000)/'
  • window.location
  • ASP.NET Core API 版本控制
  • 2017.7.25 jqGrid在编辑态无法获取数据,得到的是html代码
  • 人机猜拳
  • 【网络开发】详谈socket请求Web服务器过程
  • 【技术性】Search知识
  • ES学习笔记(12)--Symbol
  • Git同步原始仓库到Fork仓库中
  • httpie使用详解
  • Javascript基础之Array数组API
  • Redis字符串类型内部编码剖析
  • 初识MongoDB分片
  • 大数据与云计算学习:数据分析(二)
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 如何设计一个微型分布式架构?
  • 软件开发学习的5大技巧,你知道吗?
  • 使用SAX解析XML
  • 项目管理碎碎念系列之一:干系人管理
  • 一道闭包题引发的思考
  • 用jQuery怎么做到前后端分离
  • 支付宝花15年解决的这个问题,顶得上做出十个支付宝 ...
  • (2)(2.10) LTM telemetry
  • (6)添加vue-cookie
  • (C#)if (this == null)?你在逗我,this 怎么可能为 null!用 IL 编译和反编译看穿一切
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (翻译)Entity Framework技巧系列之七 - Tip 26 – 28
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (附源码)计算机毕业设计SSM保险客户管理系统
  • (机器学习-深度学习快速入门)第一章第一节:Python环境和数据分析
  • (转)ABI是什么
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转)一些感悟
  • .gitignore文件—git忽略文件
  • .helper勒索病毒的最新威胁:如何恢复您的数据?
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • /dev/VolGroup00/LogVol00:unexpected inconsistency;run fsck manually
  • [ 2222 ]http://e.eqxiu.com/s/wJMf15Ku
  • [2016.7 day.5] T2
  • [CC2642R1][VSCODE+Embedded IDE+IAR Build+Cortex-Debug] TI CC2642R1基于VsCode的开发环境
  • [CF482B]Interesting Array
  • [COI2007] Sabor
  • [C进阶] 数据在内存中的存储——浮点型篇
  • [Design Pattern] 工厂方法模式