Advertisement
Guest User

Untitled

a guest
May 28th, 2015
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct _Array
  5. {
  6.     short *arr;
  7.     unsigned short size;
  8. } Array;
  9.  
  10. #define EMPTY_SQUARE -1
  11. #define NOT_FINISH -2
  12. #define FINISH_SUCCESS 0
  13. #define FINISH_FAILURE -3
  14. #define BOARD_SIZE 9
  15. #define FILLED 1
  16. #define FAIL 0
  17.  
  18.  
  19. typedef int bool;
  20. #define true 1
  21. #define false 0
  22.  
  23. short* bucketsort(short sudokuBoard[][9], int row, int col, int size, unsigned short*arrSize);
  24. Array ***PossibleDigits(short sudokuBoard[][9]);
  25. int FillBoard(short board[][9], Array ***possibilities);
  26. int OneStage(short board[][9], Array ***possibilities, int *x, int *y);
  27. void UpdatePossibilities(short board[][9], Array***possibilities, int row, int col, int squareValue);
  28. void UpdateSquarePossibilities(Array***possibilities, int row, int col, unsigned short arrSize, int squareValue);
  29. bool SearchForDuplications(short board[][9], int row, int col, int SquareValue);
  30. void printPossibilities(Array***possibilities, int x, int y);
  31. void sudokoPrintBoard(short board[][9]);
  32. void main()
  33. {
  34.     short board[9][9] =
  35.     { 5, -1, 4, -1, 7, -1, -1, 1, -1,
  36.     6, -1, 2, 1, -1, -1, 3, -1, -1,
  37.     1, -1, 8, -1, 4, -1, -1, 6, -1,
  38.     -1, 5, -1, -1, 6, -1, -1, 2, -1,
  39.     -1, 2, -1, 8, -1, 3, -1, -1, -1,
  40.     -1, -1, -1, -1, -1, 4, -1, 5, 6,
  41.     -1, 6, 1, 5, 3, 7, 2, 8, 4,
  42.     -1, 8, 7, -1, 1, 9, -1, 3, -1,
  43.     -1, -1, -1, 2, 8, -1, -1, -1, 9 };
  44.     Array*** possible_solutions;
  45.  
  46.     printf("Initial board\n");
  47.     sudokoPrintBoard(board);
  48.  
  49.     printf("Press enter to start playing...\n");
  50.     getchar();
  51.  
  52.     possible_solutions = PossibleDigits(board);
  53.  
  54.     if (FillBoard(board, possible_solutions) == -1)
  55.         printf("User's selections led to duplications\n");
  56.     else
  57.     {
  58.         sudokoPrintBoard(board);
  59.         printf("Board filled successfully\n");
  60.     }
  61. }
  62.  
  63. void sudokoPrintBoard(short board[][9])
  64. {
  65.     int i, j;
  66.  
  67.     for (i = 0; i < BOARD_SIZE; i++)
  68.     {
  69.         for (j = 0; j < BOARD_SIZE; j++)
  70.         {
  71.  
  72.             if (board[i][j] == -1)
  73.                 printf("%d ", board[i][j]);
  74.  
  75.             else
  76.                 printf(" %d ", board[i][j]);
  77.         }
  78.         printf("\n");
  79.  
  80.     }
  81.  
  82. }
  83. int FillBoard(short board[][9], Array ***possibilities)
  84. {
  85.     int status;
  86.     int x, y;
  87.     int option;
  88.     int squareValue;
  89.  
  90.     status = OneStage(board, possibilities, &x, &y);
  91.  
  92.     while (status == NOT_FINISH)
  93.     {
  94.         printPossibilities(possibilities, x, y);
  95.  
  96.         scanf("%d", &option);
  97.  
  98.         squareValue = possibilities[x][y]->arr[option - 1];
  99.         board[x][y] = squareValue;
  100.  
  101.         UpdatePossibilities(board, possibilities, x, y, squareValue);
  102.  
  103.         status = OneStage(board, possibilities, &x, &y);
  104.     }
  105.  
  106.     if (status == FINISH_SUCCESS)
  107.         return(FILLED);
  108.     else
  109.         return(FAIL);
  110.  
  111. }
  112.  
  113. void printPossibilities(Array***possibilities,int x,int y)
  114. {
  115.     int i;
  116.  
  117.     for (i = 0; i < possibilities[x][y]->size; i++)
  118.         printf("%d. %d\n", i+1, possibilities[x][y]->arr[i]);
  119.     return;
  120. }
  121.  
  122. int OneStage(short board[][9], Array ***possibilities, int *x, int *y)
  123. {
  124.     bool full = true;
  125.     bool dupFound = false;
  126.     int i, j;
  127.     int xIndex, yIndex;
  128.     int squareValue;
  129.     unsigned short minPossibilities = 9;
  130.  
  131.     for (i = 0; i < BOARD_SIZE; i++)
  132.     {
  133.         for (j = 0; j < BOARD_SIZE; j++)
  134.         {
  135.             if (board[i][j] == EMPTY_SQUARE)
  136.             {
  137.                 if (possibilities[i][j]->size == 1)
  138.                 {
  139.                     squareValue = possibilities[i][j]->arr[0];
  140.                     board[i][j] = squareValue;
  141.  
  142.                     dupFound = SearchForDuplications(board, i, j, squareValue);
  143.  
  144.                     if (dupFound == true)
  145.                         return(FINISH_FAILURE);
  146.  
  147.                     free(possibilities[i][j]->arr);
  148.                     free(possibilities[i][j]);
  149.                     possibilities[i][j] = NULL;
  150.  
  151.                     UpdatePossibilities(board, possibilities, i, j, squareValue);
  152.  
  153.                 }
  154.                 else
  155.                 {
  156.                     full = false;
  157.                     if (minPossibilities > possibilities[i][j]->size)
  158.                     {
  159.                         minPossibilities = possibilities[i][j]->size;
  160.                         xIndex = i;
  161.                         yIndex = j;
  162.                     }
  163.                 }
  164.             }
  165.         }
  166.     }
  167.  
  168.     if (full == false)
  169.     {
  170.         *x = xIndex;
  171.         *y = yIndex;
  172.         return(NOT_FINISH);
  173.     }
  174.     else
  175.         return(FINISH_SUCCESS);
  176. }
  177.  
  178. bool SearchForDuplications(short board[][9], int row, int col, int SquareValue)
  179. {
  180.     int count = 0;
  181.     int i, j;
  182.     int rowBlock;
  183.     int colBlock;
  184.  
  185.  
  186.     for (i = 0; i < BOARD_SIZE; i++)
  187.     {
  188.         if (board[row][i] == SquareValue)
  189.             count++;
  190.     }
  191.  
  192.     if (count>1)
  193.         return(true);
  194.  
  195.     count = 0;
  196.  
  197.     for (i = 0; i < BOARD_SIZE; i++)
  198.     {
  199.         if (board[i][col] == SquareValue)
  200.             count++;
  201.     }
  202.  
  203.     if (count>1)
  204.         return(true);
  205.  
  206.     count = 0;
  207.     rowBlock = row / 3;
  208.     colBlock = col / 3;
  209.  
  210.     for (i = 3 * rowBlock; i < 3 * rowBlock + 3; i++)
  211.     {
  212.         for (j = 3 * colBlock; j < 3 * colBlock + 3; j++)
  213.         {
  214.             if (board[i][j] == SquareValue)
  215.                 count++;
  216.         }
  217.     }
  218.  
  219.     if (count>1)
  220.         return(true);
  221.     else
  222.         return(false);
  223. }
  224.  
  225. void UpdatePossibilities(short board[][9], Array***possibilities, int row, int col, int squareValue)
  226. {
  227.     int i;
  228.     int j;
  229.     int rowBlock;
  230.     int colBlock;
  231.  
  232.     for (i = 0; i < BOARD_SIZE; i++)
  233.     {
  234.         if (board[row][i] == EMPTY_SQUARE)
  235.             UpdateSquarePossibilities(possibilities, row, i, possibilities[row][i]->size, squareValue);
  236.     }
  237.  
  238.     for (i = 0; i < BOARD_SIZE; i++)
  239.     {
  240.         if (board[i][col] == EMPTY_SQUARE)
  241.             UpdateSquarePossibilities(possibilities, i, col, possibilities[row][i]->size, squareValue);
  242.     }
  243.  
  244.     rowBlock = row / 3;
  245.     colBlock = col / 3;
  246.  
  247.     for (i = 3 * rowBlock; i < 3 * rowBlock + 3; i++)
  248.     {
  249.         for (j = 3 * colBlock; j < 3 * colBlock + 3; j++)
  250.         {
  251.             if (board[i][j] == EMPTY_SQUARE)
  252.                 UpdateSquarePossibilities(possibilities, i, j, possibilities[row][i]->size, squareValue);
  253.         }
  254.     }
  255.  
  256.     return;
  257. }
  258.  
  259. void UpdateSquarePossibilities(Array***possibilities, int row, int col, unsigned short arrSize, int squareValue)
  260. {
  261.     unsigned short i;
  262.     unsigned short newSize;
  263.     int write = 0;
  264.     short* updatedArr;
  265.     bool found = false;
  266.  
  267.     for (i = 0; i < arrSize && found == false; i++)
  268.     {
  269.         if (possibilities[row][col]->arr[i] == squareValue)
  270.         {
  271.             found = true;
  272.         }
  273.     }
  274.  
  275.     if (found == false)
  276.         return;
  277.  
  278.     newSize = arrSize - 1;
  279.     updatedArr = (short*)malloc(newSize*sizeof(short));
  280.  
  281.     for (i = 0; i < arrSize; i++)
  282.     {
  283.         if (possibilities[row][col]->arr[i] != squareValue)
  284.         {
  285.             updatedArr[write] = possibilities[row][col]->arr[i];
  286.             write++;
  287.         }
  288.     }
  289.  
  290.     free(possibilities[row][col]->arr);
  291.  
  292.     possibilities[row][col]->arr = updatedArr;
  293.     possibilities[row][col]->size = newSize;
  294.  
  295.     return;
  296. }
  297.  
  298. Array ***PossibleDigits(short sudokuBoard[][9])
  299. {
  300.     Array*** newBoard;
  301.     int i;
  302.     int j;
  303.  
  304.     newBoard = (Array***)malloc(BOARD_SIZE*(sizeof(Array**)));
  305.  
  306.     if (!newBoard)  //if allocation faild
  307.     {
  308.         //allocation faild
  309.     }
  310.  
  311.     for (i = 0; i < BOARD_SIZE; i++)
  312.     {
  313.         newBoard[i]= (Array**)malloc(BOARD_SIZE*(sizeof(Array*)));
  314.         if (!newBoard)  //if allocation faild
  315.         {
  316.             //allocation faild
  317.         }
  318.     }
  319.  
  320.     for (i = 0; i<9; i++)
  321.     {
  322.         for (j = 0; j<9; j++)
  323.         {
  324.             newBoard[i][j] = (Array*)malloc(sizeof(Array));   //allocation every cell to Array struct
  325.             if (!newBoard[i][j])  //if allocation faild
  326.             {
  327.                 //exit
  328.             }
  329.         }
  330.     }
  331.  
  332.  
  333.     for (i = 0; i <BOARD_SIZE; i++)
  334.     {
  335.         for (j = 0; i < BOARD_SIZE; i++)
  336.         {
  337.             if (sudokuBoard[i][j] == EMPTY_SQUARE)
  338.                 newBoard[i][j]->arr = bucketsort(sudokuBoard, i, j, BOARD_SIZE, &newBoard[i][j]->size);
  339.             else
  340.                 newBoard[i][j]->arr = NULL;
  341.         }
  342.     }
  343.  
  344.     return(newBoard);
  345. }
  346.  
  347. short* bucketsort(short sudokuBoard[][9], int row, int col, int size, unsigned short*arrSize)
  348. {
  349.     int i, j;   //index for
  350.     int count[10];   //new array        
  351.     unsigned short sizeCount = 0;
  352.     int squareValue;
  353.     int sudokuBlockSize = 3;
  354.     short *arr;
  355.     int write = 0;
  356.     int rowBlock;
  357.     int colBlock;
  358.  
  359.     for (i = 0; i < size; i++)
  360.         count[i] = 0;
  361.  
  362.     for (i = 0; i < size; i++)
  363.     {
  364.         squareValue = sudokuBoard[i][col];
  365.  
  366.         if (squareValue != EMPTY_SQUARE)
  367.             count[sudokuBoard[i][col]]++;
  368.     }
  369.  
  370.     for (i = 0; i < size; i++)
  371.     {
  372.         squareValue = sudokuBoard[row][i];
  373.  
  374.         if (squareValue != EMPTY_SQUARE)
  375.             count[sudokuBoard[row][i]]++;
  376.     }
  377.  
  378.     rowBlock = row / 3;
  379.     colBlock = col / 3;
  380.  
  381.     for (i = 3 * rowBlock; i < 3 * rowBlock + 3; i++)
  382.     {
  383.         for (j = 3 * colBlock; j < 3 * colBlock + 3; j++)
  384.         {
  385.             squareValue = sudokuBoard[i][j];
  386.  
  387.             if (squareValue != EMPTY_SQUARE)
  388.                 count[sudokuBoard[i][j]]++;
  389.         }
  390.     }
  391.  
  392.     for (i = 1; i <= size; i++)
  393.     if (count[i] == 0)
  394.         sizeCount++;
  395.  
  396.     *arrSize = sizeCount;
  397.  
  398.     arr = (short*)malloc(sizeCount*sizeof(short));
  399.  
  400.     for (i = 1; i <= size; i++)
  401.     {
  402.         if (count[i] == 0)
  403.         {
  404.             arr[write] = i;
  405.             write++;
  406.         }
  407.     }
  408.  
  409.     return(arr);
  410. }
  411.  
  412. int countDigits(int num)  /*This function count the amount of digits in 'num'*/
  413. {
  414.     int count;
  415.     count = 0;
  416.  
  417.     while (num > 0)
  418.     {
  419.         num = num / 10;
  420.         count++;
  421.     }
  422.  
  423.     return(count);
  424. }
  425.  
  426. void printMultTable(int maxMult) /*This function prints the Multiplication Table at the size of maxMult x maxMult*/
  427. {
  428. int i; /*loop index*/
  429. int j; /*loop index*/
  430. int maxcount;/*holds the maximum amount of digits a number can reach in the current mult table*/
  431. int count;  /*Holds the current amount of digits*/
  432. int nextcount; /*Holds the amount of digits of the next number*/
  433.  
  434.     maxcount = countDigits(maxMult);  /*Call to function*/
  435.  
  436.     for (i = 1; i <= maxMult; i++) /*Main loop*/
  437.     {
  438.         printf("%*d", maxcount, i);
  439.    
  440.        for (j = 2; j <= maxMult; j++)
  441.     {
  442.  
  443.         count = countDigits(i*j);
  444.         nextcount = countDigits((maxMult)*j);
  445.  
  446. if (count < nextcount)
  447.     {
  448.         printf("%*d", nextcount + 1, i*j);
  449.         }
  450.     else
  451.             printf("%*d", count + 1, i*j);
  452.     }
  453.  
  454.         printf("\n");
  455.     }
  456.  
  457.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement