score:0
Accepted answer
see my solution below. i put a blank in a cell when not solvable
using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
namespace sudoku
{
public partial class form1 : form
{
sudoku sudoku = null;
public form1()
{
initializecomponent();
sudoku = new sudoku(this);
}
private void solve_click(object sender, eventargs e)
{
sudoku.solve();
}
private void clear_click(object sender, eventargs e)
{
this.controls.remove(sudoku);
sudoku.dispose();
sudoku = new sudoku(this);
}
}
public class sudoku : panel
{
public const int top_margin = 50;
public const int left_margin = 50;
static list<cell> cells = new list<cell>();
static list<list<cell>> rows = null;
static list<list<cell>> columns = null;
static list<list<cell>> boxes = null;
public sudoku(form form)
{
form.controls.add(this);
this.left = left_margin;
this.top = top_margin;
this.width = (2 * left_margin) + (9 * cell.width);
this.height = (2 * top_margin) + (9 * cell.height);
rows = (new string('\0',9)).select(x => new list<cell>()).tolist();
columns = (new string('\0', 9)).select(x => new list<cell>()).tolist();
boxes = (new string('\0', 9)).select(x => new list<cell>()).tolist();
for (int i = 0; i < 81; i++)
{
cell newcell = new cell(i);
this.controls.add(newcell);
}
}
public static void solve()
{
for (int i = 0; i < 81; i++)
{
if (cells[i].textbox.text != "")
{
cells[i].value = int.parse(cells[i].textbox.text);
}
}
recursivesolve(0);
}
static boolean recursivesolve(int index)
{
boolean solved = false;
cell cell = cells[index];
if (cell.value != null)
{
if (index == 80)
{
solved = true;
}
else
{
solved = recursivesolve(index + 1);
}
}
else
{
for (int i = 1; i <= 9; i++)
{
if (!rows[cell.row].where(x => x.value == i).any())
{
if (!columns[cell.column].where(x => x.value == i).any())
{
if (!boxes[cell.box].where(x => x.value == i).any())
{
cell.value = i;
cell.textbox.text = i.tostring();
cell.textbox.selectionalignment = horizontalalignment.center;
cell.textbox.selectionfont = new font("ariel", 30);
if (index == 80)
{
solved = true;
}
else
{
solved = recursivesolve(index + 1);
if (solved)
{
break;
}
else
{
cell.value = null;
cell.textbox.text = "";
}
}
}
}
}
}
}
return solved;
}
public class cell : panel
{
public const int width = 100;
public const int height = 100;
public dictionary<int, int> boxdict = new dictionary<int, int>() {
{0,0}, {1,0},{2,0},{3,1},{4,1},{5,1},{6,2},{7,2},{8,2},
{9,0}, {10,0},{11,0},{ 12,1},{13,1},{14,1},{15,2},{16,2},{17,2},
{18,0}, {19,0},{20,0},{21,1},{22,1},{23,1},{24,2},{25,2},{26,2},
{27,3}, {28,3},{29,3},{30,4},{31,4},{32,4},{33,5},{34,5},{35,5},
{36,3}, {37,3},{38,3},{39,4},{40,4},{41,4},{42,5},{43,5},{44,5},
{45,3}, {46,3},{47,3},{48,4},{49,4},{50,4},{51,5},{52,5},{53,5},
{54,6}, {55,6},{56,6},{57,7},{58,7},{59,7},{60,8},{61,8},{62,8},
{63,6}, {64,6},{65,6},{66,7},{67,7},{68,7},{69,8},{70,8},{71,8},
{72,6}, {73,6},{74,6},{75,7},{76,7},{77,7},{78,8},{79,8},{80,8},
};
public int index = 0;
public int row = 0;
public int column = 0;
public int box = 0;
public int? value = null;
public richtextbox textbox = new richtextbox();
public cell() { }
public cell(int index)
{
cells.add(this);
this.index = index;
this.row = index / 9;
this.column = index % 9;
this.box = boxdict[index];
this.left = sudoku.left_margin + (column * width);
this.top = sudoku.top_margin + (row * height);
this.width = width;
this.height = height;
this.controls.add(textbox);
textbox.left = 0;
textbox.top = 0;
textbox.width = width;
textbox.height = height;
textbox.selectionalignment = horizontalalignment.center;
textbox.selectionfont = new font("ariel", 30);
//textbox.text = index.tostring();
textbox.multiline = false;
rows[row].add(this);
columns[column].add(this);
boxes[box].add(this);
}
}
public void dispose()
{
cells = new list<cell>();
rows = new list<list<cell>>();
columns = new list<list<cell>>(); ;
boxes = new list<list<cell>>();
}
}
}
Source: stackoverflow.com
Related Query
- Does using "new" on a struct allocate it on the heap or stack?
- Is there a way to set properties on struct instances using reflection?
- Using a class versus struct as a dictionary key
- If my struct implements IDisposable will it be boxed when used in a using statement?
- How to mutate a boxed struct using IL
- C# using reflection to create a struct
- Is there a way to initialize members of a struct without using a constructor?
- Compilation error. Using properties with struct
- Generate dynamic method to set a field of a struct instead of using reflection
- How to test whether a ref struct method is throwing an exception using xUnit?
- Why can I not deserialize this custom struct using Json.Net?
- Why C# 6.0 doesn't let to set properties of a non-null nullable struct when using Null propagation operator?
- Why does C# generate garbage when using a struct as a generic dictionary key?
- Using ref struct or class with P/Invoke
- Using Open Delegate to access a struct property setter generate exception
- Pros & cons of using struct to enforce validation of built-in type
- How do I enforce using a factory on a struct in C#
- Is there a down side to using a struct as input to a method?
- Dodging the performance hit from using `in` with a struct without making the struct readonly?
- When should I use a struct rather than a class in C#?
- Get property value from string using reflection
- How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#?
- Pass Method as Parameter using C#
- How do I get the current username in .NET using C#?
- How do I get the color from a hexadecimal color code using .NET?
- How to ignore a property in class if null, using json.net
- Using async/await for multiple tasks
- Deserialize JSON object into dynamic object using Json.net
- URL Encoding using C#
- How do I make calls to a REST API using C#?
More Query from same tag
- Viewing message streams in Visual Studio for signalR chat application
- How to convert Hex to RGB?
- Getting the upload progress during file upload using Webclient.Uploadfile
- Can you post an anonymous object as json to a webapi method and it match the properties of the json object to the parameters of the webapi call?
- TFS Shelvesets and Code review
- Canonical HTTP POST code?
- Nlog fixed file name per app session
- Visual Studio crashes when removing reference
- Can I host RavenDB inside my self-contained process?
- Merge multiple .NET binaries together?
- What is the relationship between delegates and events?
- Specify NotNull If Method Returns At All
- Updating to ASP NET 5 beta5 breaks everything
- Check if number is divisible by 24
- How do I take the "top n" using NHibernate Criteria API?
- Call C++ code from a C# application or port it?
- Converting wav to mp3 - ASP.NET
- Getting Error "The 'targetFramework' attribute currently references a version that is later than the installed version of the .NET Framework"
- How can I specify /debug:embedded from csproj?
- FORWARD_NULL after derefencing null?
- Fastest way to operate on individual bytes in an int
- Can a Func<> call itself recursively?
- Something like Snoop for Silverlight
- dynamically set value of DataKeyNames in gridview
- How do you get to the original message text in a Microsoft Bot Framework LuisIntent method
- When to use Task in Interface Methods in C#?
- Override .ToString method c#
- Windows RT and c#
- Check if Element exists in Selenium
- binding combobox to another combobox in wpf