zoukankan      html  css  js  c++  java
  • 寒假集训——搜索 B

    #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("
    ");
            }
        }
    
        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:
    ",++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(1T100)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
    注意要写一个数独的判断函数。
  • 相关阅读:
    AutoCAD 命令统计魔幻球的实现过程(2)
    Autodesk 首届中国开发者夏令营将在6月1920在北京举行
    AutoCAD 命令统计魔幻球—AutoCAD + Windows Azure + WebGL
    AutoCAD 命令统计魔幻球的实现过程(4)
    小提示:Eclipse 中快速实现或Override基类或接口中的方法
    AutoCAD 2014 新特性概览
    Autodesk Infrastructure Map Server(AIMS)/MapGuide API 培训材料第1章
    AutoCAD 2014 新特性针对开发人员
    Civil 3D 2013利用API把三角网曲面提取为栅格网
    AutoCAD 命令统计魔幻球的实现过程(3)
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10337999.html
Copyright © 2011-2022 走看看