SOJ-3385,2689,2485+POJ-2676,2918(趣味数独)(Sudoku)

int d[10][10];
int row[10][10], col[10][10], square[10][10];
int INDEX;
struct my
{
    int x, y;
} wo[100];
int getsquare(int i, int j)
{
    return i / 3 * 3 + j / 3;
}
int check(int x, int y, int k)
{
    return (row[x][k] == 0 && col[y][k] == 0 && square[getsquare(x, y)][k] == 0);
}
int dfs()
{
    if (INDEX != 0) {
        my tmp = wo[--INDEX];        
        int i;
        for (i = 1; i <= 9; ++i) {
            if (check(tmp.x, tmp.y, i)) {
                row[tmp.x][i] = col[tmp.y][i] = 1;
                square[getsquare(tmp.x, tmp.y)][i] = 1;
                if (dfs()) {
                    d[tmp.x][tmp.y] = i;
                    return 1;
                } else {
                    row[tmp.x][i] = col[tmp.y][i] = 0;
                    square[getsquare(tmp.x, tmp.y)][i] = 0;
                }
            }
        }
        wo[INDEX++] = tmp;
        return 0;
    }
    return 1;
}
bool check_it(int d[][10], int a, int b, int x, int y)
{
    int test[10] = {0};
    int i, j;
    for (i = a; i < b; ++i) {
        for (j = x; j < y; ++j) {
            test[d[i][j]]++;
        }
    }
    for (j = 1; j <= 9; ++j)
        if (test[j] != 1)
            return false;
    return true;
}
int main()
{
    int cases = 1, i, j;
    char s[150];
    int t;
    cin>>t;
    getchar();
    while (t--) {
        INDEX = 0;
        memset(row, 0, sizeof(row));
        memset(col, 0, sizeof(col));
        memset(square, 0, sizeof(square));
        for (i = 0; i < 9; ++i) {
            gets(s);
            for (j = 0; j < 9; ++j) {
                char c = s[j];
                d[i][j] = c - '0';
                if (i == 0 && j == 0 && d[i][j] == -1) return 0;
                if (d[i][j]) {
                    row[i][d[i][j]] = 1;
                    col[j][d[i][j]] = 1;
                    square[getsquare(i, j)][d[i][j]] = 1;
                } else {
                    wo[INDEX].x = i;
                    wo[INDEX].y = j;
                    ++INDEX;
                }
            }            
        }
        dfs();
        int tag = 0;
        for (i = 0; i < 9; ++i) {
            int test[10] = {0};
            for (j = 0; j < 9; ++j) {
                test[d[j][i]]++;
            }
            for (j = 1; j <= 9; ++j)
                if (test[j] != 1)
                    break;
            if (j != 10) {
                tag = 1;
                break;
            }
        }
        for (i = 0; i < 9; ++i) {
            int test[10] = {0};
            for (j = 0; j < 9; ++j) {
                test[d[i][j]]++;
            }
            for (j = 1; j <= 9; ++j)
                if (test[j] != 1)
                    break;
            if (j != 10) {
                tag = 1;
                break;
            }
        }
        if (!check_it(d, 0, 3, 0, 3)) tag = 1;
        if (!check_it(d, 0, 3, 3, 6)) tag = 1;
        if (!check_it(d, 0, 3, 6, 9)) tag = 1;
        if (!check_it(d, 3, 6, 0, 3)) tag = 1;
        if (!check_it(d, 3, 6, 3, 6)) tag = 1;
        if (!check_it(d, 3, 6, 6, 9)) tag = 1;
        if (!check_it(d, 6, 9, 0, 3)) tag = 1;
        if (!check_it(d, 6, 9, 3, 6)) tag = 1;
        if (!check_it(d, 6, 9, 6, 9)) tag = 1;
        if (tag == 1) {
            printf("Could not complete this grid.\n\n");
            continue;
        }
        for (i = 0; i < 9; ++i) {
            for (j = 0; j < 8; ++j) {
                printf("%d", d[i][j]);
            }
            printf("%d\n", d[i][j]);
        }
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(SOJ-3385,2689,2485+POJ-2676,2918(趣味数独)(Sudoku))