Sudoku

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

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(1≤T≤100). 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
 
题意:数独,横的、竖的、每个四分之一块不能有相同的数字。
题解:开三个数组记录横的、竖的、四分之一块1、2、3、4出现的情况,标记一下。然后枚举每个位置,如果没有数字的话,if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)来确定填何数,如果有两个可以填,先跳过。
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char s[][];
int ans[][];
int l[][];
int r[][];
int p[][];
int judge(int x,int y)
{
if(x<&&y<) return ;
if(x>=&&y<) return ;
if(x<&&y>=) return ;
if(x>=&&y>=) return ;
}
void solve()
{
for(int i = ; i<; i++)
for(int j = ; j<; j++)
{
l[i][ans[i][j]] = ;
}
for(int j = ; j<; j++)
for(int i = ; i<; i++)
{
r[j][ans[i][j]] = ;
}
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
p[judge(i,j)][ans[i][j]] = ;
}
}
int flag = ;
int count = ;
while(flag)
{
flag = ;
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
count = ;
if(ans[i][j]) continue;
for(int k = ; k<=; k++)
{
if(l[i][k]==&&r[j][k]==&&p[judge(i,j)][k]==)
count++;
}
if(count == )
{
flag = ;
for(int k = ; k<=; k++)
{
if(l[i][k]==&&r[j][k]==&&p[judge(i,j)][k]==)
{
ans[i][j] = k;
l[i][k] = ;
r[j][k] = ;
p[judge(i,j)][k] = ;
}
}
}
}
}
}
}
int main()
{
int t,kase = ;
scanf("%d",&t);
while(t--)
{
memset(l,,sizeof(l));
memset(r,,sizeof(r));
memset(ans,,sizeof(ans));
memset(p,,sizeof(p));
for(int i = ; i<; i++)
{
scanf("%s",s[i]);
}
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
if(s[i][j] == '*') ans[i][j] = ;
else ans[i][j] = s[i][j] - '';
}
}
solve();
printf("Case #%d:\n",++kase);
for(int i = ; i<; i++)
{
for(int j = ; j<; j++)
{
printf("%d",ans[i][j]);
}
printf("\n");
}
}
return ;
}
 
 

HDU 5547 暴力的更多相关文章

  1. HDU 5547 Sudoku (暴力)

    题意:数独. 析:由于只是4*4,完全可以暴力,要注意一下一些条件,比如2*2的小方格也得是1234 代码如下: #pragma comment(linker, "/STACK:102400 ...

  2. E - Sudoku HDU - 5547 (搜索+暴力)

    题目链接:https://cn.vjudge.net/problem/HDU-5547 具体思路:对于每一位上,我们可以从1到4挨着去试, 具体判断这一位可不可以的时候,看当前这一位上的行和列有没有冲 ...

  3. HDU - 5547 数独(回溯法)

    题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 正所谓:骗分过样例,暴力出奇迹. 解题思想(暴力出奇迹(DFS+回溯)): 1 ...

  4. HDU - 5547 Sudoku(数独搜索)

    Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself ...

  5. HDU 5547 Sudoku(DFS)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=5547 题目: Sudoku Time Limit: 3000/1000 MS (Java/Others ...

  6. HDU 6351暴力枚举 6354计算几何

    Beautiful Now Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  7. The 2015 China Collegiate Programming Contest H. Sudoku hdu 5547

    Sudoku Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  8. HDU 5944 暴力

    Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)T ...

  9. hdu 4039 暴力

    思路:用map将每个字符串与一个数字进行对应,然后暴力统计就好了 #include<cstring> #include<iostream> #include<cstdio ...

随机推荐

  1. C语言strtok()函数:字符串分割

    头文件:#include <string.h> 定义函数:char * strtok(char *s, const char *delim); 函数说明:strtok()用来将字符串分割成 ...

  2. hdu 1020

    //自信满满地交上去~~but...超时了 #include <iostream> #include <string.h> #include <stdio.h> u ...

  3. velocity 语法

    1,如果调用是第一条就加上类名current. #foreach($info in $aboutlist) <li><a href="$!{info.href}" ...

  4. java memcache应用

    import java.io.Serializable; import java.text.DateFormat; import java.util.Date; import java.util.Ma ...

  5. Struts2的运行机制简介

    1.客户端通过URL请求tomcat 2.URL找到对应站点的WEB.xml  发现里面有  struts2配置 3.执行StrutsPrepareAndExecuteFilter类的init方法 4 ...

  6. runtime基础知识

    看到一篇不错的runtime方面博客: 引言 相信很多同学都听过运行时,但是我相信还是有很多同学不了解什么是运行时,到底在项目开发中怎么用?什么时候适合使用?想想我们的项目中,到底在哪里使用过运行时呢 ...

  7. shell 各种循环判断

    shell支持的循环有 Shell if else Shell case esac Shell for循环 Shell while循环 Shell until循环

  8. eclipse修改豆沙绿

    长时间的使用eclipse开发会很累吧  设置一个保护眼睛的豆沙绿色 不刺眼 是不是会更好一些呢 那么如何设置呢现在就教大家   工具/原料 eclipse jdk 方法/步骤 1 首先打开eclip ...

  9. windows下python+Django+eclipse开发环境的配置

    1.JDK环境的安装 在http://www.java.com/zh_CN/download/faq/develop.xml 页面下,点击JDK下载,下载所需的jdk版本.直接点击安装即可. 2.py ...

  10. 2015年4月27日---C语言:输出特殊图案,请在c环境中运行,看一看,Very Beautiful!

    ---恢复内容开始--- 题目:输出特殊图案,请在c环境中运行,看一看,Very Beautiful! 1.程序分析:字符共有256个.不同字符,图形不一样. 2.程序源代码: [code=c] #i ...