HDOJ5547 和 HDOJ1426 DFS简单题



Sudoku

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


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(1T100) 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
 

Source
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:   6107  6106  6105  6104  6103 



这是5547的题目,比起1426简单点,因为数据规模小一些,其实两题的程序相似的,一个套路出的
附上代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int maxn = 6;
const int maxm = 20;
char a[maxn][maxn];
int dx[maxm],dy[maxm],tot;
bool flag;
char cc[]={'1','2','3','4'};

bool check(int x, int y, char c){
    for (int i=0;i<4;i++) if (a[x][i]==c) return 0;
    for (int i=0;i<4;i++) if (a[i][y]==c) return 0;
    int nx=(x/2)*2;
    int ny=(y/2)*2;
    for (int i=nx;i<=nx+1;i++)
        for (int j=ny;j<=ny+1;j++)
          if (a[i][j]==c) return 0;
    return 1;
}

void output(){
    for (int i=0;i<4;i++){
        for (int j=0;j<4;j++) printf("%c",a[i][j]);
        printf("\n");
    }
}


void dfs(int num){
    if (num==tot+1){
        flag=1;
        output();
        return;
    }
    if (flag) return;

    int xx=dx[num];
    int yy=dy[num];
    for (int i=0;i<4;i++) {
        if (check(xx,yy,cc[i])) {
            a[xx][yy]=cc[i];
            dfs(num+1);
            if (flag) return;
            a[xx][yy]='*';
        }
    }
}

int main(){
    int n;
    cin>>n;
    for (int t=1;t<=n;t++){
        tot=0;
        for (int i=0;i<4;i++){
            for (int j=0;j<4;j++) {
                cin>>a[i][j];
                if (a[i][j]=='*'){
                    tot++;
                    dx[tot]=i;
                    dy[tot]=j;
                }
            }
        }

        flag=0;
        printf("Case #%d:\n",t);
        dfs(1);
    }
    return 0;
}








Sudoku Killer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8229    Accepted Submission(s): 2534


Problem Description
自从2006年3月10日至11日的首届数独世界锦标赛以后,数独这项游戏越来越受到人们的喜爱和重视。
据说,在2008北京奥运会上,会将数独列为一个单独的项目进行比赛,冠军将有可能获得的一份巨大的奖品———HDU免费七日游外加lcy亲笔签名以及同hdu acm team合影留念的机会。
所以全球人民前仆后继,为了奖品日夜训练茶饭不思。当然也包括初学者linle,不过他太笨了又没有多少耐性,只能做做最最基本的数独题,不过他还是想得到那些奖品,你能帮帮他吗?你只要把答案告诉他就可以,不用教他是怎么做的。

数独游戏的规则是这样的:在一个9x9的方格中,你需要把数字1-9填写到空格当中,并且使方格的每一行和每一列中都包含1-9这九个数字。同时还要保证,空格中用粗线划分成9个3x3的方格也同时包含1-9这九个数字。比如有这样一个题,大家可以仔细观察一下,在这里面每行、每列,以及每个3x3的方格都包含1-9这九个数字。

例题:


答案:

 

Input
本题包含多组测试,每组之间由一个空行隔开。每组测试会给你一个 9*9 的矩阵,同一行相邻的两个元素用一个空格分开。其中1-9代表该位置的已经填好的数,问号(?)表示需要你填的数。
 

Output
对于每组测试,请输出它的解,同一行相邻的两个数用一个空格分开。两组解之间要一个空行。
对于每组测试数据保证它有且只有一个解。
 

Sample Input
           
           
7 1 2 ? 6 ? 3 5 8 ? 6 5 2 ? 7 1 ? 4 ? ? 8 5 1 3 6 7 2 9 2 4 ? 5 6 ? 3 7 5 ? 6 ? ? ? 2 4 1 1 ? 3 7 2 ? 9 ? 5 ? ? 1 9 7 5 4 8 6 6 ? 7 8 3 ? 5 1 9 8 5 9 ? 4 ? ? 2 3
 

Sample Output
           
           
7 1 2 4 6 9 3 5 8 3 6 5 2 8 7 1 9 4 4 9 8 5 1 3 6 7 2 9 2 4 1 5 6 8 3 7 5 7 6 3 9 8 2 4 1 1 8 3 7 2 4 9 6 5 2 3 1 9 7 5 4 8 6 6 4 7 8 3 2 5 1 9 8 5 9 6 4 1 7 2 3
 

Author
linle
 

Source
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1045  2614  1067  2952  3368 


这是1426的题目,很类似是吧!!!
点击我的博客可以看到该题题解:
  http://blog.csdn.net/computer_user/article/details/76998932


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值