• hdu 2780 Su-Su-Sudoku(DFS数独)


    题目链接:hdu2780

    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #include<math.h>
    #include<algorithm>
    #define N 15
    using namespace std;
    int map[N][N],v1[15],v2[15];
    int flag,n,ok;
    struct node
    {
        int x,y;
    }s[6];
    void judge1()//判断行和列
    {
        int i,j;
        for(i = 0 ; i < 9 ; i ++)
        {
            memset(v1,0,sizeof(v1));
            memset(v2,0,sizeof(v2));
            for(j = 0 ; j < 9 ; j ++)
            {
                if(v1[map[i][j]])
                {
                    ok = 0;
                    return;
                }
                else
                v1[map[i][j]] = 1;
                if(v2[map[j][i]])
                {
                    ok = 0;
                    return;
                }
                else
                v2[map[j][i]] = 1;
            }
        }
    }
    void judge2()//判断3*3矩形
    {
        int i,j,x,y;
        for(i = 0 ; i <= 6 ; i += 3 )
        {
            for(j = 0 ; j <= 6 ; j += 3)
            {
                memset(v1,0,sizeof(v1));
                for(x = 0 ; x < 3 ; x ++)
                for(y = 0 ; y < 3 ; y ++)
                {
                    if(v1[map[x + i][y + j]])
                    {
                        ok = 0;
                        return ;
                    }
                    else v1[map[x + i][y + j]] = 1;
                }
            }
        }
    }
    void print()
    {
        ok = 1;
        judge1();
        if(!ok)
        {
            printf("Could not complete this grid.
    ");
            return ;
        }
        ok = 1;
        judge2();
        if(!ok)
          printf("Could not complete this grid.
    ");
        else
        {
            for(int i = 0 ; i < 9 ; i ++)
            {
                for(int j = 0 ; j < 9 ; j ++)
                printf("%d",map[i][j]);
                printf("
    ");
            }
        }
    }
    bool judge(int cur,int t)
    {
        int i,j;
        for(i = 0 ; i < 9 ; i ++)
          if(map[ s[cur].x ][i] == t || map[i][ s[cur].y ] == t)
           return 0;
        int x = s[cur].x/3 * 3;
        int y = s[cur].y/3 * 3;
        for(i = 0 ; i < 3 ; i ++)
          for(j = 0 ; j < 3 ; j ++)
            if(map[x + i][y + j] == t) return 0;
        return 1;
    }
    void dfs(int cur)
    {
        if(cur == n)
        {
            flag = 1;
            print();
            return ;
        }
        if(flag) return ;
        for(int i = 1 ; i <= 9 ; i ++)
        if(judge(cur,i) && !flag)
        {
            map[s[cur].x][s[cur].y] = i;
            dfs(cur + 1);
            map[s[cur].x][s[cur].y] = 0;
        }
    }
    int main()
    {
        int i,j,T;
        char a[15];
        scanf("%d",&T);
        while(T--)
        {
            n = 0;
            for(i = 0 ; i < 9 ; i ++)
            {
                scanf("%s",a);
                for(j = 0 ; j < 9 ; j ++)
                {
                    map[i][j] = a[j] - '0';
                    if(!map[i][j])
                    {
                        s[n].x = i;
                        s[n].y = j;
                        n ++;
                    }
                }
            }
            flag = 0;
            dfs(0);
            if(!flag)
             printf("Could not complete this grid.
    ");
            if(T) printf("
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    格式化输出数字
    传教士经验
    集合
    替换
    连接
    填充
    取值
    分割
    创建日历和日期列表
    常用日期格式
  • 原文地址:https://www.cnblogs.com/riskyer/p/3220124.html
Copyright © 2020-2023  润新知