搜索----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
2341
4123
#include<stdio.h>
#include<string.h>
using namespace std;
char a[100][100];
int panduan(int row,int col)
{
    for(int i=0;i<4;i++)
    {
        if(a[row][i]==a[row][col]&&i!=col)
        return 0;
    }  ///判断该数字是否可以存在这一行里
    for(int i=0;i<4;i++)
    {
        if(a[i][col]==a[row][col]&&i!=row)
        return 0;
    }  ///判断该数字是否可以存在这一列里
    int mrow=row;
    int mcol=col;
    if(row%2==1)row-=1;
    if(col%2==1)col-=1;
    for(int i=row;i<=row+1;i++)
    {
        for(int j=col;j<=col+1;j++)
        {
            {
                if(a[i][j]==a[mrow][mcol]&&i!=mrow&&j!=mcol)return 0;
            }
        }
    }  ///判断2*2的小方框里是不是1,2,3,4
    return 1;
}
void dfs(int cur)
{  

    if(cur==4*4)///如果到达最后一个点则输出全部
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                printf("%c",a[i][j]);
            }
            printf("\n");
        }
        return ;
    }
    int row=cur/4;///行
    int col=cur%4;  ///列
    if(a[row][col]==‘*‘)
    {
        for(int j=1;j<=4;j++)  ///吧1,2,3,4每个数都试一遍
        {
            a[row][col]=j+‘0‘;
            if(panduan(row,col)==1)  ///符合条件则进入下一个点
            {
                dfs(cur+1);
            }
            a[row][col]=‘*‘;  ///取消标记
        }
    }
    else
    {
        dfs(cur+1);
    }
}
int main()
{
    int kase=0;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0;i<4;i++)
        {
            scanf("%s",a[i]);
        }
        printf("Case #%d:\n",++kase);
        dfs(0);
    }
    return 0;
}  
时间: 2024-08-03 12:35:19

搜索----hdu 5547的相关文章

[搜索] hdu 4016 Magic Bitwise And Operation

主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=4016 Magic Bitwise And Operation Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1315    Accepted Submission(s): 504 Problem Description Given n

随手练——数独 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 1254] 推箱子

推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5343    Accepted Submission(s): 1503 Problem Description 推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子

hdu 5547

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

记忆化搜索 hdu 1331

Function Run Fun Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2586    Accepted Submission(s): 1255 Problem Description We all love recursion! Don't we? Consider a three-parameter recursive f

(水搜索 hdu 1728)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 17645    Accepted Submission(s): 4281 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

(记忆化搜索)hdu 1978

How many ways Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3296    Accepted Submission(s): 1948 Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下:1.机器人一开始在棋盘的起始点并有起

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

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 hi