支持命令行的数独

#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
 int x;     //存放数组的横坐标
 int y;     //存入数组的纵坐标
} ElemType;

static ElemType list[81];   //定义一个表空间
int len = -1;               //len记录表的大小


void  AddListElem (ElemType);
int   GetListElem (ElemType *, int);
void  ShowMe (int [][9]);
int   AddCount (int [][9]);
int   CheckLine (int [][9], int, int);
int   CheckRow (int [][9], int, int);
int   CheckPalace (int [][9], int, int, int);

int GetListElem (ElemType *e, int i)
{//取得元素
 if (i < 0)
 {
  return 0;
 }
 *e = list[i];
 return 1;
}

void AddListElem (ElemType e)
{//把数独原本为空的坐标存入表中
 len++;
 list[len] = e;
}

int CheckPalace (int arr[][9], int line, int row, int count)
{//检查小宫
 int i, j;
 int a, b;
 i = line / 3 * 3;
 j = row / 3 * 3;    //i,j为一个小宫的起始横纵下标
 for (a = i; a < i + 3; ++a)
 {
  for (b = j; b < j + 3; ++b)
  {
   if (arr[a][b] == count)
   {
    return 1;
   }
  }
 }
 return 0;
}

int CheckRow (int arr[][9], int row, int count)
{//检查列
 int i;
 for (i = 0; i < 9; ++i)
 {
  if (arr[i][row] == count)
  {
   return 1;
  }
 }
 return 0;
}

int CheckLine (int arr[][9], int line, int count)
{//检查行
 int j;
 for (j = 0; j < 9; ++j)
 {
  if (arr[line][j] == count)
  {
   return 1;
  }
 }
 return 0;
}

int AddCount (int arr[][9])
{//添加数
 int i, j, k;
 int count;
 ElemType e;

 k = -1;
 for (i = 0; i < 9; ++i)
 {
  for (j = 0; j < 9; )
  {
   count = 1;
   if (!arr[i][j])
   {
    k++;
    while ( ( CheckLine (arr, i, count) || CheckRow (arr, j, count)
     || CheckPalace (arr, i, j, count) ) && count <= 9 )
    {
     count ++;
    }
    if (count > 9)
    {
     do
     {
      arr[i][j] = 0;
      
      if ( GetListElem (&e, k - 1) )
      {
       k--;
       i = e.x;
       j = e.y;
      
      }
      else
      {
       return 0;
      }
      count = arr[i][j] + 1;
      while ( ( CheckLine (arr, i, count) || CheckRow (arr, j, count)
        || CheckPalace (arr, i, j, count) ) && count <= 9 )
      {
       count++;
      }
     }while (count > 9);
     arr[i][j] = count;
    }
    else
    {
     arr[i][j] = count;
     j++;
    }
   }
   else
   {
    
    j++;
   }
  }
 }
 return 1;
}

void ShowMe (int arr[][9])
{//显示数独
 int i, j;
 for (i = 0; i < 9; ++i)
 {
  if ( (i+1) % 3 == 1 )
  {
   printf ("  ------------------------/n");
  }
  for (j = 0; j < 9; ++j)
  {
   if ( (j + 1) % 3 == 1 )
   {
    printf ("%2c", '|');
   }
   printf ("%2d", arr[i][j]); 
  }
  printf ("%2c/n", '|');
 }
 printf ("  ------------------------/n");
}

int main (int argc, char *argv[])
{
 FILE  *out;
 char  ch;
 int i, j, k;
 ElemType e;
 int arr[9][9];
 memset ( arr, 0, 81 * sizeof (int) );


 if ( argc == 2 && !strcmp ("/?", argv[1]) || argc == 1 )
 {
  printf ("求出数独的解/n/n");
  printf ("SUDOKU 数据 路径/n/n");
  return 0;
 }
 if (argc == 3 || argc == 2)
 {
  k = 0;
  for (i = 0; i < 9; ++i)
  {
   for (j = 0; j < 9; ++j)
   {
    ch = argv[1][k];
    arr[i][j] = atoi (&ch);
    //sscanf (&argv[1][k], "%d", &arr[i][j]);
    k++;
    if ( !arr[i][j] )
    {
     e.x = i;
     e.y = j;
     AddListElem (e);
    }
   }
  }
  if ( AddCount (arr) )
  {
   //ShowMe (arr);
  }
  else
  {
   printf ("该数独无解/n");
   return -1;
  }

  
  if (argc == 3)
  {
   out = fopen (argv[2], "at");
   if (out)
   {
    for (i = 0; i < 9; ++i)
    {
     for (j = 0; j < 9; ++j)
     {
      fprintf (out, "%2d", arr[i][j]);
     }
     fprintf (out, "/n");
    }
    fprintf (out, "/n");
    fclose (out);
   }
   return 0;
  }

  ShowMe (arr);
 }
 else
 {
  printf ("使用格式有误/n");
  return 1;
 }

 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数独是一种经典的逻辑填字游戏,通过在9x9方格内填入数字1-9,使得每一行、每一列和每一个3x3的九宫格内数字都不重复。在解决数独问题时,可以利用Matlab编程来实现。 对于Matlab来说,有多种算法可以用于解决数独问题。常见的算法包括回溯算法、剪枝算法等。其中,回溯算法是一种通过试错的方式来解决问题的方法。通过逐个尝试每个位置的可能数字,如果发现某个数字无法满足数独的要求,则回溯到上一个位置重新选择数字,直到填满整个数独。 为了编写一个数独求解程序,我们可以使用Matlab中的矩阵操作和循环结构。首先,将数独问题转化为一个9x9的二维数组,空位可以用0或空格表示。然后,编写一个求解函数,该函数接受一个数独矩阵作为参数,通过回溯算法来填充数字,直到解决数独。 在求解函数中,我们可以使用循环结构来遍历数独矩阵的每一个位置,如果该位置为空,则尝试填入1-9的数字,并检查是否满足数独的要求。如果满足要求,则继续下一个位置的填充;如果不满足要求,则回溯到上一个位置重新选择数字。当所有位置都填满时,即找到了数独的解。 使用Matlab来编写数独求解程序,可以大幅简化代码的编写和调试过程。同时,Matlab具有强大的矩阵运算能力和丰富的数学函数库,可以更高效地处理数独问题。在使用Matlab编程过程中,可以参考CSDN等技术网站上的相关教程和代码示例,以便更好地理解和应用这些算法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值