zoukankan      html  css  js  c++  java
  • hdu 1426 Sudoku Killer

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1426

      1 #include<stdio.h>
      2 #include<math.h>
      3 #include<string.h>
      4 #include<stdlib.h>
      5 #include<iostream>
      6 using namespace std;
      7 char map[12][12];
      8 bool row[12][12],list[12][12];
      9 int pos[12*9][2];
     10 int k;
     11 
     12 int check(int v,int num)//第V个?上替换num值
     13 {
     14     int n,m;
     15     if(pos[v][0]>=1 && pos[v][0]<=3)
     16     n=1;
     17     if(pos[v][0]>=4 && pos[v][0]<=6)
     18     n=4;
     19     if(pos[v][0]>=7 && pos[v][0]<=9)
     20     n=7;
     21     if(pos[v][1]>=1 && pos[v][1]<=3)
     22     m=1;
     23     if(pos[v][1]>=4 && pos[v][1]<=6)
     24     m=4;
     25     if(pos[v][1]>=7 && pos[v][1]<=9)
     26     m=7;
     27     for(int i=n;i<n+3;i++)
     28     {
     29         for(int j=m;j<m+3;j++)
     30         {
     31             if(map[i][j] == num+'0')
     32             return 0;
     33         }
     34     }
     35     return 1;
     36 }
     37 int dfs(int v)//从第0个?开始查找
     38 {
     39     int i;
     40     if(v==k)//如果全部找到返回1
     41     return 1;
     42     for(i=1;i<=9;i++)
     43     {
     44         if(!row[pos[v][0]][i] && !list[pos[v][1]][i] && check(v,i))//判断同行、同列和九宫格
     45         {
     46             row[pos[v][0]][i]=true;
     47             list[pos[v][1]][i]=true;
     48             map[pos[v][0]][pos[v][1]]=i+'0';
     49             if(dfs(v+1))
     50             return 1;
     51             row[pos[v][0]][i]=false;
     52             list[pos[v][1]][i]=false;
     53             map[pos[v][0]][pos[v][1]]='?';
     54         }
     55     }
     56     return 0;
     57 }
     58 void Output()
     59 {
     60     for(int i=1;i<=9;i++)
     61     {
     62         printf("%c",map[i][1]);
     63         for(int j=2;j<=9;j++)
     64         {
     65             printf(" %c",map[i][j]);
     66         }
     67         printf("
    ");
     68     }
     69     return ;
     70 }
     71 
     72 int main()
     73 {
     74     int sum=0;//格式问题
     75     //freopen("in.txt","r",stdin);
     76     while(1)
     77     {
     78         memset(row,false,sizeof(row));
     79         memset(list,false,sizeof(list));
     80         k=0;
     81         for(int i=1;i<=9;i++)
     82         {
     83             for(int j=1;j<=9;j++)
     84             {
     85                 if(!(cin >> map[i][j]))
     86                 exit(0);
     87                 if(map[i][j]=='?')
     88                 {
     89                     pos[k][0]=i;//记录?的x和y
     90                     pos[k++][1]=j;
     91                     continue;
     92                 }
     93                 row[i][map[i][j]-'0']=true;//记录同行出现的数
     94                 list[j][map[i][j]-'0']=true;//记录同列出现的数
     95             }
     96         }
     97         dfs(0);
     98         //printf("%d
    ",k);
     99         if(sum++)
    100         printf("
    ");
    101         Output();
    102 
    103     }
    104     return 0;
    105 }
  • 相关阅读:
    中国正在消失的老行当
    ie9 scrollbar中hover 高度增高的bug
    (替月光博客备份)百度百科:游荡在中国的窃贼
    严格模式下 W3C Strict 验证的几个注意事项
    [转]滤镜渐变使用 IE浏览器
    1.什么是串口?
    6.串口操作之API篇 GetCommTimeouts SetCommTimeouts
    5.串口操作之API篇 SetupComm GetCommState SetCommState
    TeeChart经验总结 13.Export之2.对象保存
    解决"手机存储暂不能使用""SIM卡存储暂不能使用"
  • 原文地址:https://www.cnblogs.com/xuesen1995/p/4440405.html
Copyright © 2011-2022 走看看