zoukankan      html  css  js  c++  java
  • 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;
    }
    


  • 相关阅读:
    完整约束二(学习笔记)
    完整约束一(学习笔记)
    表的创建与管理二(学习笔记)
    闪回技术(学习笔记)
    表的创建与管理一(学习笔记)
    借助AWR报告分析解决oracleCPU过高的问题(转)
    数据的集合运算(学习笔记)
    SQL:1999基本语法(学习笔记)
    表的连接操作(学习笔记)
    多表查询(学习笔记)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3220124.html
Copyright © 2011-2022 走看看