HDU - 1426 九宫格+输入处理

除了输入有点坑别的都还好

#include <bits/stdc++.h>

using namespace std;

#define     max(x, y)      x >= y ? x : y
#define     min(x, y)      x <= y ? x : y

#define     INF     0x3f3f3f3f

typedef struct node
{
    int x;
    int y;
    node(int xx, int yy)
    {
        x = xx;
        y = yy;
    }
} node;



char m[10][10];
int M[10][10];
vector<node> p;
int flag;
int cnt;

bool judge(int x, int y, int v)
{
    for (int i = 0; i < 9; i++) {              //检查行列
        if (M[i][y] == v) return false;
        if (M[x][i] == v) return false;
    }

    int xx = (x / 3) * 3;
    int yy = (y / 3) * 3;

    for (int i = xx; i < xx + 3; i++) {        //检查3*3
        for (int j = yy; j < yy + 3; j++) {
            if (M[i][j] == v) return false;
        }
    }

    return true;
}

void print()
{
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            if (j) cout << " ";
            cout << M[i][j];
        }
        cout << endl;
    }
}


void dfs(int index)
{

    if (flag) return ;

    if (index == p.size()) {
        flag = 1;
        print();
        return ;
    }


    int x = p[index].x;
    int y = p[index].y;

    for (int i = 1; i <= 9; i++) {

        if (judge(x, y, i)) {

            M[x][y] = i;
            dfs(index + 1);
        }

        if (flag) return ;
    }

    M[x][y] = 0;

}

void init()
{
    p.clear();
    flag = 0;
}

char s[105];

int main()
{

    cnt = 0;

    while (~scanf("%s", s))
    {
        init();

        M[0][0] = s[0] == '?' ? 0 : (s[0] - '0');

        if (M[0][0] == 0) p.push_back(node(0, 0));

        for (int i = 1; i < 9; i++) {
            scanf("%s", s);
            M[0][i] = s[0] == '?' ? 0 : (s[0] - '0');

            if (M[0][i] == 0) p.push_back(node(0, i));
        }


        for (int i = 1; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                scanf("%s", s);
                M[i][j] = s[0] == '?' ? 0 : (s[0] - '0');

                if (M[i][j] == 0) p.push_back(node(i, j));
            }
        }



        if (cnt++) cout << endl;


        dfs(0);


    }


    return 0;
}

 

附加:你敢不敢试一下这组数据

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值