HDU 5547 暴力

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[5][5];
int ans[5][5];
int l[5][5];
int r[5][5];
int p[5][5];
int judge(int x,int y)
{
    if(x<2&&y<2) return 1;
    if(x>=2&&y<2) return 3;
    if(x<2&&y>=2) return 2;
    if(x>=2&&y>=2) return 4;
}
void solve()
{
    for(int i = 0; i<4; i++)
        for(int j = 0; j<4; j++)
    {
        l[i][ans[i][j]] = 1;
    }
    for(int j = 0; j<4; j++)
        for(int i = 0; i<4; i++)
    {
        r[j][ans[i][j]] = 1;
    }
    for(int i = 0; i<4; i++)
    {
        for(int j = 0; j<4; j++)
        {
            p[judge(i,j)][ans[i][j]] = 1;
        }
    }
    int flag = 1;
    int count = 0;
    while(flag)
    {
        flag = 0;
        for(int i = 0; i<4; i++)
        {
            for(int j = 0; j<4; j++)
            {
                count = 0;
                if(ans[i][j]) continue;
                for(int k = 1; k<=4; k++)
                {
                    if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)
                        count++;
                }
                if(count == 1)
                {
                    flag  = 1;
                    for(int k = 1; k<=4; k++)
                    {
                        if(l[i][k]==0&&r[j][k]==0&&p[judge(i,j)][k]==0)
                        {
                            ans[i][j] = k;
                            l[i][k] = 1;
                            r[j][k] = 1;
                            p[judge(i,j)][k] = 1;
                        }
                    }
                }
            }
        }
    }
}
int main()
{
    int t,kase = 0;
    scanf("%d",&t);
    while(t--)
    {
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        memset(ans,0,sizeof(ans));
        memset(p,0,sizeof(p));
        for(int i = 0; i<4; i++)
        {
            scanf("%s",s[i]);
        }
        for(int i = 0; i<4; i++)
        {
            for(int j = 0; j<4; j++)
            {
                if(s[i][j] == ‘*‘) ans[i][j] = 0;
                else ans[i][j] = s[i][j] - ‘0‘;
            }
        }
        solve();
        printf("Case #%d:\n",++kase);
        for(int i = 0; i<4; i++)
        {
            for(int j = 0; j<4; j++)
            {
                printf("%d",ans[i][j]);
            }
            printf("\n");
        }
    }
    return 0;
}
时间: 2024-10-20 12:30:00

HDU 5547 暴力的相关文章

hdu 4876 暴力剪枝

hdu 4876 终于过了, 之前写的代码虽然思路是这样的但是有好多可以优化的地方没有注意所以一直超时超时超时!,学习了一下别人的代码,虽然看上去没什么差别但实际上却可以节省很多时间,恩恩又学到了一些技巧~     ^_^ . [题意]:给定一些卡片,每个卡片上有数字,现在选k个卡片,绕成一个环,每次可以再这个环上连续选1 - k张卡片,得到他们的异或和的数,给定一个L,问能组成[L,R]所有数字的情况下,R的最大值是多少. [思路]:暴力+剪枝  枚举在m个数里选k个数的 C(m,k)种情况,

搜索----hdu 5547

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5547 数独,保证每一行每一列都有1,2,3,4 还有4 个2 * 2的小方块儿里也必须是1,2,3,4 输入: 3 **** 2341 4123 3214 *243 *312 *421 *134 *41* **3* 2*41 4*2* 输出: Case #1: 1432 2341 4123 3214 Case #2: 1243 4312 3421 2134 Case #3: 3412 1234 23

随手练——数独 HDU - 5547 坑!坑!坑!

题目链接:HDU-5547 http://acm.hdu.edu.cn/showproblem.php?pid=5547 解题思想:随手练-- 数独 POJ - 2676 (回溯法+DFS) HDU 的这题实在是太坑了,M 数组开成 int 就过不了,改成 char 就过了.对着别人AC的代码,一点点试,到最后才试出来,数组的问题,但是不能理解啊,什么鬼,这也错?? 然后发现题目描述里有一句:Each test case starts with an empty line followed by

HDU 5386 暴力

给出初始矩阵和目标矩阵,存在m中操作,可以分别把每行或者每列都涂成同一种颜色,数据保证有解 因为保证有解,所以初始矩阵完全没有用... 暴力寻找M次操作,若目标矩阵的行或列全和该操作的颜色一样,则最后进行此操作,并把所有涂的点涂为颜色0(可当任意颜色) 然后同样依次推出之前的操作,因为之后的操作会覆盖掉之前操作的点. #include "stdio.h" #include "string.h" struct Mark { int x,y; char op; }mar

hdu 5254(暴力)

题解:暴力所有点,直到不存在可以0变1的点. #include <stdio.h> #include <string.h> const int N = 505; int n, m, k, g[N][N], temp[N][N], vis[N][N]; bool judge(int x, int y) { if (x - 1 >= 0 && y - 1 >= 0) { if (temp[x - 1][y] && temp[x][y - 1]

hdu 5547

***题意:4*4数独,要求在同一行同一列不能有相同的数字,另外在2*2的小单元里也不能有相同的数字 思路:DFS暴力搜索, 每个位置填1—4,递归回溯,判断是否符合条件,递归到最后一个位置+1则输出答案*** #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<cctype> #include

hdu 2616 暴力使用 dfs求最短路径(剪枝有点依稀)

Kill the monster Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1241    Accepted Submission(s): 846 Problem Description There is a mountain near yifenfei’s hometown. On the mountain lived a big

HDU 5547 4*4数独 http://acm.split.hdu.edu.cn/status.php

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 #include<iostream> #include<cstdio> #include<cstring> #includ

BestCoder Round #60/HDU 5505 暴力数学

GT and numbers 问题描述 给出两个数NN和MM. NN每次可以乘上一个自己的因数变成新的NN. 求最初的NN到MM至少需要几步. 如果永远也到不了输出-1−1. 输入描述 第一行读入一个数TT表示数据组数. 接下来TT行,每行两个数NN和MM. T\leq1000T≤1000, 1\leq N \leq 10000001≤N≤1000000,1 \leq M \leq 2^{63}1≤M≤2?63??. 注意M的范围.hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格.