寒假集训——搜索 B - Sudoku

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;
char s[10][10];

int panduan(int row,int cew)
{
    for(int i=0;i<4;i++)
    {
        if(s[row][i]==s[row][cew]&&i!=cew) return 0;
    }
    for(int j=0;j<4;j++)
    {
        if(s[j][cew]==s[row][cew]&&j!=row) return 0;
    }
    int mrow=row;
    int mcew=cew;
    if(row%2==1) row--;
    if(cew%2==1) cew--;
    for(int i=row;i<row+2;i++)
    {
        for(int j=cew;j<cew+2;j++)
        {
            if(s[i][j]==s[mrow][mcew]&&i!=mrow&&j!=mcew) return 0;
        }
    }
    return 1;
}

void dfs(int step)
{
    if(step==16)
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                printf("%c",s[i][j]);
            }
            printf("\n");
        }
    }

    int row=step/4;
    int cew=step%4;
    if(s[row][cew]==‘*‘)
    {
        for(int j=1;j<=4;j++)
        {
            s[row][cew]=j+‘0‘;
            if(panduan(row,cew)) dfs(step+1);
            s[row][cew]=‘*‘;
        }
    }
    else dfs(step+1);
}

int main()
{
    int cas=0;
    int t;
    cin>>t;
    while(t--)
    {
        for(int i=0;i<4;i++) scanf("%s",s[i]);
        printf("Case #%d:\n",++cas);
        dfs(0);
    }
    return 0;
}

  

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×44×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×22×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!!!

InputThe first line of the input gives the number of test cases, T(1≤T≤100)T(1≤T≤100). TT 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.OutputFor each test case, output one line containing
Case #x:, where xx 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

题目:B - Sudoku 思路:这个题目其实就是一个小一点的数独,因为很小,所以可以用枚举去搜索,完全不用担心会超时。方法很简单就是枚举每一个*位置为1,2,3,4;然后再回溯。具体:再main函数里面读入,然后进入搜索函数dfs,有一个step,如果step==16就结束了根据step可以判断出行列,然后搜这个位置,如果是*就枚举,否则就step++,进入下一个dfs注意要写一个数独的判断函数。

原文地址:https://www.cnblogs.com/EchoZQN/p/10337999.html

时间: 2024-10-09 22:57:41

寒假集训——搜索 B - Sudoku的相关文章

CSU-ACM寒假集训选拔-入门题

CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0,?h] 中. 当 Bobo 位于时间轴上 t 点,同时时间机器有 c 单位燃料时,他可以选择一个满足 \(\lceil\frac{x}{h}\rceil\leq c\) 的非负整数 x, 那么时间机器会在 [0,?x]中随机整数 y,使 Bobo 回到 (t???y) 点,同时消耗 y 单位燃料. (其中 ?

寒假集训总结

这个寒假的集训吧,缺少了总结. 首先去了秦皇岛的几天还是挺不错的,但是人还是很懒散,没有每日做总结,导致以前会做的题目不会做.COME OJ上的视频也没有看完.秦皇岛开阔了眼界吧. 放寒假前的几天吧,都在聊天,唉,真的有时候会感到孤独... 只写了两场CF的所有题题解对的.(还缺178场) 之后回学校吧一个人在寝室就看看游戏直播的视频,嗯,挺荒废的. 寒假的收货吧,会了时间线段树,一些些STL,dp做了总结,分块+暴力的方法. 之后应该干什么呢,3月17校赛,3月底天梯,4月初浙大校赛,4月底浙

高一寒假集训总结

集训的一天 早读改前一天晚上没过的题. 上午学新课,开新的一套题,学模版优化模版.前半段总是没睡醒,将近中午时效率是一天中最高的.一般不大讨论. 下午继续做题,效率也比较高.讨论比上午多. 晚上思路很乱,而且一般都是做到一套题的后几道,进程缓慢,有些错误往往要到第二天早上才能想通. 集训之最 最有感触的一道题:家谱树.为了存人名现学关联式容器,发现大家课下确实学了不少东西,开学之后要拿更多时间读奥赛书,自学一些内容. 最佩服的一种解法:王超星的挖水井.这道题我做了很久,还是没能解决.请教王超星的

HDU-2686 Matrix(多线程DP?)2017寒假集训

题意:给一个矩阵,求(1,1)到(n,n)的两条路径(不能相交),求能取到的最大值 数据范围:2 <= n <= 30,矩阵上的数 < 100 思路:记得在kuangbin最短路专题里有个要求差不多的题,当时写不出来,查题解是最大流???? 然后问了大佬,说这题也可以网络流做,不过呢...然后我就了解到了这个叫多线程DP的东西 多线程..????这不是C语言的那啥玩意...吗?课设好像还要用呢orz 其实这里是指DP数组存分别在两个点的状态能走出多少的最大值,走的时候不走到一个点上就行了

#寒假集训[20200112]

CCPC签到题集锦 2019秦皇岛: 给定正整数n,判断1/n是不是一个无穷循环小数.(t<=100,n<=100) 题解: 因为\(1/n\)是一个有理数,有理数不包含无限不循环小数,所以\(1/n\)不是有限小数就是无限循环小数.所以每次将其除以2和5,直到除不尽,若此时为1则有限小数,否则无限循环小数 CCPC哈尔滨: 给定n个整数,每个整数的范围0-100,问小于80的整数是否大于\(1/3\) 题解: 简单模拟 CCPC哈尔滨: 给定n个盒子,第i个盒子中有\(a_i\)个小球,现从

寒假集训日志(二)——最小生成树,拓扑排序,欧拉回路,连通路

今天学的内容挺多的. (一)首先说最小生成树,两种算法: 1.Kruskal算法( 将边排序,然后再选,关键在于检查是否连通,使用并查集) 2.Prim算法(使用点集,有点类似与最短路的算法) 第一题是并查集算法的使用: A - The Suspects Time Limit:1000MS     Memory Limit:20000KB     64bit IO Format:%I64d & %I64u Submit Status Description 严重急性呼吸系统综合症( SARS),

(寒假集训)Roadblock(最短路)

Roadblock 时间限制: 1 Sec  内存限制: 64 MB提交: 9  解决: 5[提交][状态][讨论版] 题目描述 Every morning, FJ wakes up and walks across the farm from his house to the barn.  The farm is a collection of N fields (1 <= N <= 250) connected by M bidirectional pathways (1 <= M

(寒假集训)Mooo Moo (完全背包)

Mooo Moo 时间限制: 1 Sec  内存限制: 64 MB提交: 5  解决: 4[提交][状态][讨论版] 题目描述 Farmer John has completely forgotten how many cows he owns!  He is too embarrassed to go to his fields to count the cows, since he doesn't want the cows to realize his mental lapse.  Ins

(寒假集训)Watering the Fields (最小生成树)

Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system to send water between his N fields (1 <= N <= 2000). Each field i is described by a distinct point (x