标签:
数独游戏 在9x9的方格内进行, 分为3x3的小方格,被称为“区”。
<%@ Page Language="C#" inherits="Skyiv.Ben.Web.SudokuPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>银河 - 数独</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button Text="返回" OnClick="BtnUriHome_Click" runat="server" />
<asp:Button Text="开始" OnClick="BtnSubmit_Click" runat="server" />
<hr />
<div>
<a href="http://www.sudoku.name/index-cn.php" target="_blank">数独游戏</a>
在9x9的方格内进行, 分为3x3的小方格,被称为“区”。<br />
数独游戏首先从已经填入数字的格子开始。<br />
数独游戏的目的是根据下列规则,用1至9之间的数字填满空格:<br />
每个数字在每一行、每一列和每一区只能出现一次。<br />
</div>
<div>
<asp:TextBox Runat="Server" Id="tbxInput" MaxLength="512" Wrap="False"
TextMode="MultiLine" Columns="15" Rows="14" />
<asp:TextBox Runat="Server" Id="tbxOutput" ReadOnly="True" Wrap="False"
TextMode="MultiLine" Columns="15" Rows="14" />
</div>
</form>
</body>
</html>
using System;
using System.IO;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyiv.Ben.Etc;
namespace Skyiv.Ben.Web
{
public class SudokuPage : Page
{
protected TextBox tbxInput;
protected TextBox tbxOutput;
public void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
tbxInput.Text = "+---+---+---+\n|2..|
|
|\n|8..|1..|.2.|\n"
+ "|..5|.7.|.3.|\n+---+---+---+\n|.7.|.3.|1..|\n|.9.|.4.|.8.|\n"
+ "|..4|
|.7.|\n+---+---+---+\n|.3.|.9.|6..|\n|.8.|..5|..3|\n"
+ "|
|
|..5|\n+---+---+---+";
}
public void BtnUriHome_Click(object sender, EventArgs e)
{
Response.Redirect(Pub.UriHome);
}
public void BtnSubmit_Click(object sender, EventArgs e)
{
try
{
Sudoku sudoku = new Sudoku(new StringReader(tbxInput.Text));
StringWriter writer = new StringWriter();
sudoku.Out(writer);
tbxOutput.Text = writer.ToString();
}
catch (Exception ex)
{
tbxOutput.Text = "Error: " + ex.Message;
}
}
}
}
using System;
using System.IO;
using System.Collections.Generic;
namespace Skyiv.Ben.Etc
{
sealed class Sudoku
{
byte[,] input, output;
int steps = 0;
public Sudoku(TextReader reader)
{
input = new byte[9, 9];
for (int y = 0; y < 9; )
{
string s = reader.ReadLine();
if (s == null) break;
s = s.Replace(‘.‘, ‘0‘);
int x = 0;
for (int i = 0; i < s.Length; i++)
if (Char.IsDigit(s, i) && x < 9) input[x++, y] = (byte)(s[i] - ‘0‘);
if (x != 0) y++;
}
}
public void Out(TextWriter writer)
{
Compute(input);
Out(writer, output);
}
void Out(TextWriter writer, byte[,] output)
{
for (int y = 0; y <= output.GetLength(1); y++)
{
if (y % 3 == 0) writer.WriteLine("+---+---+---+");
if (y >= output.GetLength(1)) break;
for (int x = 0; x <= output.GetLength(0); x++)
{
if (x % 3 == 0) writer.Write(‘|‘);
if (x >= output.GetLength(0)) break;
writer.Write((output[x, y] == 0) ? ‘.‘ : (char)(output[x, y] + ‘0‘));
}
writer.WriteLine();
}
}
bool Compute(byte[,] input)
{
List<byte[,]> list = StepIt(input);
if (list == null) return true;
foreach (byte[,] temp in list) if (Compute(temp)) return true;
return false;
}
// return null for finish
List<byte[,]> StepIt(byte[,] input)
{
if (steps++ > 100000) throw new Exception("太复杂了");
output = input;
int theX = -1, theY = -1;
byte[] theDigits = null;
for (int y = 0; y < input.GetLength(1); y++)
{
for (int x = 0; x < input.GetLength(0); x++)
{
if (input[x, y] != 0) continue;
byte[] digits = GetDigits(input, x, y);
if (digits.Length == 0) return new List<byte[,]>();
if (theDigits != null && theDigits.Length <= digits.Length) continue;
theX = x;
theY = y;
theDigits = digits;
}
}
if (theDigits == null) return null;
List<byte[,]> result = new List<byte[,]>();
foreach (byte digit in theDigits)
{
byte[,] temp = (byte[,])input.Clone();
temp[theX, theY] = digit;
result.Add(temp);
}
return result;
}
byte[] GetDigits(byte[,] input, int x, int y)
{
bool[] mask = new bool[10];
for (int i = 0; i < 9; i++)
{
mask[input[x, i]] = true;
mask[input[i, y]] = true;
}
for (int i = x / 3 * 3; i < x / 3 * 3 + 3; i++)
for (int j = y / 3 * 3; j < y / 3 * 3 + 3; j++)
mask[input[i, j]] = true;
List<byte> list = new List<byte>();
for (int i = 1; i < mask.Length; i++) if (!mask[i]) list.Add((byte)i);
return list.ToArray();
}
}
}
版权声明:本文为博主http://www.zuiniusn.com原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/u013948190/article/details/47271015