Guest User

Untitled

a guest
May 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. \datethis
  2. @*Intro. Mike Spivey announced a programming contest in February 2005,
  3. asking for a program that solves ``sudoku'' puzzles (which evidently
  4. appear daily in British newspapers). This program takes a sudoku
  5. specification in standard input and creates --- on standard output ---
  6. a file that can be piped into {\mc DANCE} in order to deduce all
  7. solutions.
  8.  
  9. Brief explanation: Each possible placement of a digit corresponds to
  10. a row, column, and box where that digit does not yet appear.
  11. We want an exact cover of those rows, columns, and boxes.
  12.  
  13. Apology: I wrote this in a big hurry. But I couldn't resist
  14. the task, because it is such a nice application of exact covering.
  15.  
  16. @c
  17. #include <stdio.h>
  18. char buf[11];
  19. int row[9][10], col[9][10], box[9][10]; /* things to cover */
  20. int board[9][9]; /* positions already filled */
  21.  
  22. main()
  23. {
  24. register int j,k,d,x;
  25. for (k=0;k<9;k++) @<Input row |k|@>;
  26. @<Output the column names needed by {\mc DANCE}@>;
  27. for (j=0;j<9;j++) for (k=0;k<9;k++) if (!board[k][j])
  28. @<Output the possibilities for filling column |j| of row |k|@>;
  29. }
  30.  
  31. @ In a production system I would of course try to give more
  32. informative error messages about malformed input data.
  33. Here I simply quit, if the rules haven't been followed.
  34.  
  35. @d panic(m) {@+fprintf(stderr,"%s!\n%s",m,buf);@+exit(-1);@+}
  36.  
  37. @<Input...@>=
  38. {
  39. fgets(buf,11,stdin);
  40. if (buf[9]!='\n')
  41. panic("Input line should have 9 characters exactly!\n");
  42. for (j=0;j<9;j++) if (buf[j]!='.') {
  43. if (buf[j]<'1' || buf[j]>'9')
  44. panic("Illegal character in input!\n");
  45. d=buf[j]-'0';
  46. if (row[k][d]) panic("Two identical digits in a row!\n");
  47. row[k][d]=1;
  48. if (col[j][d]) panic("Two identical digits in a column!\n");
  49. col[j][d]=1;
  50. x=((int)(k/3))*3+((int)(j/3));
  51. if (box[x][d]) panic("Two identical digits in a box!\n");
  52. box[x][d]=1;
  53. board[k][j]=1;
  54. }
  55. }
  56.  
  57. @ First we print out all the positions, rows, columns, and boxes that
  58. need to be covered.
  59.  
  60. @<Output the col...@>=
  61. for (k=0;k<9;k++) for (j=0;j<9;j++)
  62. if (!board[k][j]) printf(" p%d%d",k,j);
  63. for (k=0;k<9;k++) for (d=1;d<=9;d++) {
  64. if (!row[k][d]) printf(" r%d%d",k,d);
  65. if (!col[k][d]) printf(" c%d%d",k,d);
  66. if (!box[k][d]) printf(" b%d%d",k,d);
  67. }
  68. printf("\n");
  69.  
  70. @ Then we print out all the possible placements.
  71.  
  72. @<Output the poss...@>=
  73. {
  74. x=((int)(k/3))*3+((int)(j/3));
  75. for (d=1;d<=9;d++) if (!row[k][d] && !col[j][d] && !box[x][d])
  76. printf("p%d%d r%d%d c%d%d b%d%d\n",k,j,k,d,j,d,x,d);
  77. }
  78.  
  79. @*Index.
Add Comment
Please, Sign In to add comment