/ Published in: C#
Nonogram Solver
Expand |
Embed | Plain Text
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CS3100{ class Nonogram { public const int nonogramRows = 5; public const int nonogramCols = 5; static void Main(){ nono.createblanknono(); if (nono.solve(nono)) { Console.WriteLine("Solved"); } else { Console.WriteLine("Failed"); } Console.ReadLine(); } public bool solve(Nonogram nono) { //add try all permuts of rows for (int i = 0; i < nono.rows[0].Length; i += nonogramRows) { for (int e = 0; e < nonogramRows; e++) { nonogram[0, e] = nono.rows[0][(i + e) / nonogramRows, (i + e) % nonogramRows]; } for (int j = 0; j < nono.rows[1].Length; j += nonogramRows) { for (int r = 0; r < nonogramRows; r++) { nonogram[1, r] = nono.rows[1][(j + r) / nonogramRows, (j + r) % nonogramRows]; } for (int q = 0; q < nono.rows[2].Length; q += nonogramRows) { for (int m = 0; m < nonogramRows; m++) { nonogram[2, m] = nono.rows[2][(q + m) / nonogramRows, (q + m) % nonogramRows]; } for (int k = 0; k < nono.rows[3].Length; k += nonogramRows) { for (int v = 0; v < nonogramRows; v++) { nonogram[3, v] = nono.rows[3][(k + v) / nonogramRows, (k + v) % nonogramRows]; } for (int x = 0; x < nono.rows[4].Length; x += nonogramRows) { for (int a = 0; a < nonogramRows; a++) { nonogram[4, a] = nono.rows[4][(x + a) / nonogramRows, (x + a) % nonogramRows]; } if (checkcols(nono)) { printNono(nono); return true; } } } } } } return false; } bool checkcols(Nonogram nono) { for (int i = 0; i < nonogramCols; i++) { if(!checkcol(i,nono)){ return false; } } return true; } bool checkcol(int num,Nonogram nono) { for (int i = 0; i < nono.cols[num].Length; i += nonogramCols) { if (findcolmatch(num, i, nono)) { return true; } } return false; } bool findcolmatch(int num, int i, Nonogram nono) { int count = 0; for (int w = 0; w < nonogramCols; w++) { if (nonogram[w, num] == nono.cols[num][(i + w) / nonogramCols, (i + w) % nonogramCols]) { count++; } } if (count == nonogramCols) { return true; } else { return false; } } void createblanknono() { for (int i = 0; i < nonogramRows; i++) { for (int x = 0; x < nonogramCols; x++) { nonogram[i, x] = 0; } } } void printNono(Nonogram nono) { for (int i = 0; i < 25; i += 5) { Console.WriteLine( nono.nonogram[i / 5, i % 5].ToString() + " " + nono.nonogram[i / 5, (i+1) % 5].ToString() + " " + nono.nonogram[i / 5, (i+2) % 5].ToString() + " " + nono.nonogram[i / 5, (i+3) % 5].ToString() + " " + nono.nonogram[i / 5, (i+4) % 5].ToString()); } } } }
You need to login to post a comment.
