Posted By

Emolyptic on 12/07/12


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

homskillet


Nonogram


 / Published in: C#
 

Nonogram Solver

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace CS3100{
  7. class Nonogram {
  8. public const int nonogramRows = 5;
  9. public const int nonogramCols = 5;
  10. public int[,] nonogram = new int[nonogramRows, nonogramCols];
  11. public int[][,] rows = new int[nonogramRows][,] { new int[,] {{1,1,0,0,0},{0,1,1,0,0},{0,0,1,1,0},{0,0,0,1,1} },
  12. new int[,] {{1,1,1,1,0},{0,1,1,1,1} },
  13. new int[,] {{1,1,0,1,0},{1,1,0,0,1},{0,1,1,0,1} },
  14. new int[,] {{1,1,0,1,0},{1,1,0,0,1},{0,1,1,0,1} },
  15. new int[,] {{1,1,0,1,0},{1,1,0,0,1},{0,1,1,0,1} } };
  16.  
  17. public int[][,] cols = new int[nonogramCols][,] { new int[,] {{1,0,0,0,0},{0,1,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,0,0,0,1} },
  18. new int[,] {{1,1,1,1,1} },
  19. new int[,] {{1,1,1,0,1} },
  20. new int[,] {{1,0,0,0,0},{0,1,0,0,0},{0,0,1,0,0},{0,0,0,1,0},{0,0,0,0,1} },
  21. new int[,] {{1,1,1,1,0},{0,1,1,1,1} } };
  22.  
  23. static void Main(){
  24. Nonogram nono = new Nonogram();
  25.  
  26. nono.createblanknono();
  27.  
  28. if (nono.solve(nono)) {
  29. Console.WriteLine("Solved");
  30. } else {
  31. Console.WriteLine("Failed");
  32. }
  33. Console.ReadLine();
  34.  
  35. }
  36. public bool solve(Nonogram nono) {
  37. //add try all permuts of rows
  38. for (int i = 0; i < nono.rows[0].Length; i += nonogramRows) {
  39. for (int e = 0; e < nonogramRows; e++) {
  40. nonogram[0, e] = nono.rows[0][(i + e) / nonogramRows, (i + e) % nonogramRows];
  41. }
  42. for (int j = 0; j < nono.rows[1].Length; j += nonogramRows) {
  43. for (int r = 0; r < nonogramRows; r++) {
  44. nonogram[1, r] = nono.rows[1][(j + r) / nonogramRows, (j + r) % nonogramRows];
  45. }
  46. for (int q = 0; q < nono.rows[2].Length; q += nonogramRows) {
  47. for (int m = 0; m < nonogramRows; m++) {
  48. nonogram[2, m] = nono.rows[2][(q + m) / nonogramRows, (q + m) % nonogramRows];
  49. }
  50. for (int k = 0; k < nono.rows[3].Length; k += nonogramRows) {
  51. for (int v = 0; v < nonogramRows; v++) {
  52. nonogram[3, v] = nono.rows[3][(k + v) / nonogramRows, (k + v) % nonogramRows];
  53. }
  54. for (int x = 0; x < nono.rows[4].Length; x += nonogramRows) {
  55. for (int a = 0; a < nonogramRows; a++) {
  56. nonogram[4, a] = nono.rows[4][(x + a) / nonogramRows, (x + a) % nonogramRows];
  57. }
  58. if (checkcols(nono)) {
  59. printNono(nono);
  60. return true;
  61. }
  62.  
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. bool checkcols(Nonogram nono) {
  71. for (int i = 0; i < nonogramCols; i++) {
  72. if(!checkcol(i,nono)){
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. bool checkcol(int num,Nonogram nono) {
  79. for (int i = 0; i < nono.cols[num].Length; i += nonogramCols) {
  80. if (findcolmatch(num, i, nono)) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. bool findcolmatch(int num, int i, Nonogram nono) {
  87. int count = 0;
  88. for (int w = 0; w < nonogramCols; w++) {
  89. if (nonogram[w, num] == nono.cols[num][(i + w) / nonogramCols, (i + w) % nonogramCols]) {
  90. count++;
  91. }
  92. }
  93. if (count == nonogramCols) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. }
  99. void createblanknono() {
  100. for (int i = 0; i < nonogramRows; i++) {
  101. for (int x = 0; x < nonogramCols; x++) {
  102. nonogram[i, x] = 0;
  103. }
  104. }
  105. }
  106. void printNono(Nonogram nono) {
  107. for (int i = 0; i < 25; i += 5) {
  108. Console.WriteLine( nono.nonogram[i / 5, i % 5].ToString() + " "
  109. + nono.nonogram[i / 5, (i+1) % 5].ToString() + " "
  110. + nono.nonogram[i / 5, (i+2) % 5].ToString() + " "
  111. + nono.nonogram[i / 5, (i+3) % 5].ToString() + " "
  112. + nono.nonogram[i / 5, (i+4) % 5].ToString());
  113.  
  114. }
  115. }
  116. }
  117. }

Report this snippet  

You need to login to post a comment.