Sudoku POJ

Description

A Sudoku grid is a 16x16 grid of cells grouped in sixteen 4x4 squares, where some cells are filled with letters from A to P (the first 16 capital letters of the English alphabet), as shown in figure 1a. The game is to fill all the empty grid cells with letters from A to P such that each letter from the grid occurs once only in the line, the column, and the 4x4 square it occupies. The initial content of the grid satisfies the constraints mentioned above and guarantees a unique solution.

Write a Sudoku playing program that reads data sets from a text file.

Input

Each data set encodes a grid and contains 16 strings on 16 consecutive lines as shown in figure 2. The i-th string stands for the i-th line of the grid, is 16 characters long, and starts from the first position of the line. String characters are from the set {A,B,…,P,-}, where – (minus) designates empty grid cells. The data sets are separated by single empty lines and terminate with an end of file.

Output

The program prints the solution of the input encoded grids in the same format and order as used for input.

Sample Input

--A----C-----O-I
-J--A-B-P-CGF-H-
--D--F-I-E----P-
-G-EL-H----M-J--
----E----C--G---
-I--K-GA-B---E-J
D-GP--J-F----A--
-E---C-B--DP--O-
E--F-M--D--L-K-A
-C--------O-I-L-
H-P-C--F-A--B---
---G-OD---J----H
K---J----H-A-P-L
--B--P--E--K--A-
-H--B--K--FI-C--
--F---C--D--H-N-

Sample Output

FPAHMJECNLBDKOGI
OJMIANBDPKCGFLHE
LNDKGFOIJEAHMBPC
BGCELKHPOFIMAJDN
MFHBELPOACKJGNID
CILNKDGAHBMOPEFJ
DOGPIHJMFNLECAKB
JEKAFCNBGIDPLHOM
EBOFPMIJDGHLNKCA
NCJDHBAEKMOFIGLP
HMPLCGKFIAENBDJO
AKIGNODLBPJCEFMH
KDEMJIFNCHGAOPBL
GLBCDPMHEONKJIAF
PHNOBALKMJFIDCEG
IAFJOECGLDPBHMNK

题意:就是让每行,每列,每个4*4的十六宫格中A~P只出现一次。输出16*16的宫格信息。

思路:我总感觉这题用dancing links会好做很多

①肯定是选择可填入的字母最少的位置开始dfs,这样分支比较少

②如果一个位置只剩一个字母可以填,就填上这个字母(废话)

③如果所有的字母不能填在该行(列、十六宫格),立刻回溯

④如果某个字母只能填在该行(列、十六宫格)的某处,立刻填写

 

首先除了dfs的结束条件外,一次写下对位置的②剪枝,对行,列,十六宫格的③、④剪枝,因为②、④剪枝填了一个字母,需要再次判断结束条件,

然后就是对可能性最小的位置进行dfs

 

(代码参考网上,侵删)

#include<iostream>
#include<string.h>
#include<cstdio>

using namespace std;

int maps[17][17];
int table[17][17];
int num;

void put_in(int x,int y,int k)
{
    num++;
    maps[x][y] = k;
    table[x][y] |= 1<<k;
    for(int i=1; i<=16; i++)
    {
        table[i][y] |= 1<<k;
        table[x][i] |= 1<<k;
    }
    int r = (x-1)/4*4+1;
    int c = (y-1)/4*4+1;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            table[r+i][c+j] |= 1<<k;
        }
    }
}


int _count(int x)
{
    for(int i=0; x; i++)
    {
        if(x & 1)
        {
            if(x>>1 == 0)
                return i;
            return -1;
        }
        x >>= 1;
    }
    return -1;
}

int row(int x,int k)
{
    int t = -1;
    for(int y=1; y<=16; y++)
    {
        if(maps[x][y] == k)
            return -1;
        if(maps[x][y] >= 0)
            continue;
        if((table[x][y]&1<<k) == 0)
        {
            if(t != -1)
                return -1;
            t = y;
        }
    }
    if(t != -1)
        return t;
    return -2;
}

int col(int y,int k)
{
    int t = -1;
    for(int x=1; x<=16; x++)
    {
        if(maps[x][y] == k)
            return -1;
        if(maps[x][y] >= 0)
            continue;
        if((table[x][y]&1<<k) == 0)
        {
            if(t != -1)
                return -1;
            t = x;
        }
    }
    if(t != -1)   
        return t;
    return -2;  
}

void cuble(int r,int c,int k,int &x,int &y)
{
    x = -2;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            if(maps[i+r][j+c] == k)
            {
                x=-1;
                return;
            }
            if(maps[i+r][j+c] >= 0)
                continue;
            if((table[i+r][j+c]&1<<k) == 0)
            {
                if(x != -2)
                {
                    x=-1;
                    return;
                }
                x = i;
                y = j;
            }
        }
    }
}

int cal(int x)
{
    int cnt = 0;
    while(x)
    {
        if(x&1)
            cnt++;
        x >>= 1;
    }
    return cnt;
}

bool dfs()
{
    if(num == 256)
    {
        for(int i=1; i<=16; i++)
        {
            for(int j=1; j<=16; j++)
            {
                printf("%c",maps[i][j]+'A');
            }
            puts("");
        }
        puts("");
        return 1;
    }
    for(int i=1; i<=16; i++)
    {
        for(int j=1; j<=16; j++)
        {
            if(maps[i][j] >= 0)
                continue;
            int k = _count(table[i][j]);
            if(k != -1)
                put_in(i,j,k);
        }
    }
    for(int x=1; x<=16; x++)
    {
        for(int k=0; k<16; k++)
        {
            int y = row(x,k);
            if(y == -2)
                return 0;
            if(y != -1)
                put_in(x,y,k);

        }
    }
    for(int y=1; y<=16; y++)
    {
        for(int k=0; k<16; k++)
        {
            int x = col(y,k);
            if(x == -2)
                return 0;
            if(x != -1)
                put_in(x,y,k);

        }
    }
    for(int r=1; r<=16; r+=4)
    {
        for(int c=1; c<=16; c+=4)
        {
            for(int k=0; k<16; k++)
            {
                int x,y;
                cuble(r,c,k,x,y);
                if(x == -2)
                    return 0;
                if(x != -1)
                    put_in(r+x,c+y,k);

            }
        }
    }
    if(num == 256)
    {
        for(int i=1; i<=16; i++)
        {
            for(int j=1; j<=16; j++)
            {
                printf("%c",maps[i][j]+'A');
            }
            puts("");
        }
        puts("");
        return 1;
    }
    int t_num;
    int t_maps[17][17];
    int t_table[17][17];
    t_num = num;
    for(int i=1; i<=16; i++)
    {
        for(int j=1; j<=16; j++)
        {
            t_maps[i][j] = maps[i][j];
            t_table[i][j] = table[i][j];
        }
    }
    int mx,my,mn=16;
    for(int i=1; i<=16; i++)
    {
        for(int j=1; j<=16; j++)
        {
            if(maps[i][j]>=0)
                continue;
            int r = 16 - cal(table[i][j]);
            if(r  < mn)
            {
                mn = r;
                mx = i,my = j;
            }
        }
    }
    for(int k=0; k<16; k++)
    {
        if((table[mx][my] & 1<<k) == 0)
        {
            put_in(mx,my,k);
            if(dfs())
                return 1;
            num = t_num;
            for(int i=1; i<=16; i++)
            {
                for(int j=1; j<=16; j++)
                {
                    maps[i][j] = t_maps[i][j];
                    table[i][j] = t_table[i][j];
                }
            }
        }
    }
    return 0;
}

int main()
{
    char s[20];
    while(~scanf("%s",s))
    {
        num = 0;
        memset(table,0,sizeof(table));
        for(int j=1; j<=16; j++)
        {
            if(s[j-1]!='-')
                put_in(1,j,s[j-1]-'A');
            else
                maps[1][j] = -1;
        }
        for(int i=2; i<=16; i++)
        {
            scanf("%s",s);
            for(int j=1; j<=16; j++)
            {
                if(s[j-1]!='-')
                    put_in(i,j,s[j-1]-'A');
                else
                    maps[i][j] = -1;
            }
        }
        dfs();
    }

}
View Code

 

 

原文地址:https://www.cnblogs.com/iwannabe/p/10591987.html