Algorithm Game-Sudoku 原

Wayen007

Sudoku Solver:

• Write a program to solve a Sudoku puzzle by filling the empty cells.
• Empty cells are indicated by the character '.'.
• You may assume that there will be only one unique solution.

...and its solution numbers marked in red.

C# Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sudoku
{
class Suduku
{
/// <summary>
/// Keep all of values related every time after running the programme.
/// </summary>
private static string str;

/// <summary>
/// Keep all of array's data
///
/// </summary>
private static string arrayStr;

static void Main(string[] args)
{
string[] a = new string[9];
string[,] arr = new string[,] {
{"5","3",".",".","7",".",".",".","." },
{"6",".",".","1","9","5",".",".","." },
{".","9","8",".",".",".",".","6","." },
{"8",".",".",".","6",".",".",".","3" },
{"4",".",".","8",".","3",".",".","1" },
{"7",".",".",".","2",".",".",".","6" },
{".","6",".",".",".",".","2","8","." },
{".",".",".","4","1","9",".",".","5" },
{".",".",".",".","8",".",".","7","9" }
};

// If key is true, "while" cyscle will continue to run. Otherwise, the process will jump out of cycle.
bool key = true;

// This array keep results after running once 'while' cycle.
string[,] result = new string[8, 8];
result = arr;

Console.WriteLine("   -------Sudoku Game Question--------");
result = arr;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Write("   " + result[i, j]);
}
Console.Write("\n\n");
}
Console.Write("   Let's play this game!\n\n");

// "while" cycle that it can deal with datas and replace '.';
while (key)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
if (arr[i, j] == ".")
{
Array.Clear(a, 0, a.Length);

// X stand for rows, Y stand for columns.
int X = i / 3;
int Y = j / 3;
for (int i1 = 0; i1 < 3; i1++)
{
for (int j1 = 0; j1 < 3; j1++)
{
a = isExist(a, arr[X * 3 + i1, Y * 3 + j1]);
}
}
// Check row's values existed
for (int m = 0; m < 9; m++)
{
a = isExist(a, arr[i, m]);
}
// Check column's values existed
for (int n = 0; n < 9; n++)
{
a = isExist(a, arr[n, j]);
}

for (int k = 0; k < 9; k++)
{
if (a[k] != "")
{
str += a[k];
}
else
{
str += "";
}
}

//Console.WriteLine("Verify this string:{0}", str);
if (str.Length != 8)
{
str = string.Empty;
continue;
}
else
{
if (!str.Contains("1"))
{
result[i, j] = "1";
}
if (!str.Contains("2"))
{
result[i, j] = "2";
}
if (!str.Contains("3"))
{
result[i, j] = "3";
}
if (!str.Contains("4"))
{
result[i, j] = "4";
}
if (!str.Contains("5"))
{
result[i, j] = "5";
}
if (!str.Contains("6"))
{
result[i, j] = "6";
}
if (!str.Contains("7"))
{
result[i, j] = "7";
}
if (!str.Contains("8"))
{
result[i, j] = "8";
}
if (!str.Contains("9"))
{
result[i, j] = "9";
}
}
}
else
{
continue;
}
}
}

// Clean up this string
arrayStr = string.Empty;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
arrayStr += result[i, j];
}
}

// According to key value, whether the cycle continue to run.
if (arrayStr.Contains("."))
{
key = true;
}
else
{
key = false;
}
}

// Print Sudoku Game Result
Console.WriteLine("   -------Sudoku Game Result--------");
result = arr;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
Console.Write("   " + result[i, j]);
}
Console.Write("\n\n");
}

}

/// <summary>
/// Create a new array to store row's or column's values.
/// </summary>
/// <param name="a1">store row's or column's values</param>
/// <param name="v">array arr's data</param>
/// <returns></returns>
private static string[] isExist(string[] a, string v)
{
switch (v)
{
case "1": a[0] = v; break;
case "2": a[1] = v; break;
case "3": a[2] = v; break;
case "4": a[3] = v; break;
case "5": a[4] = v; break;
case "6": a[5] = v; break;
case "7": a[6] = v; break;
case "8": a[7] = v; break;
case "9": a[8] = v; break;
default: break;
}
return a;
}
}
}

Running results:

Wayen007

QA/测试工程师
poj 3074 Sudoku

dlx算法解数独,有空会写详解,暂时就这样了 ......52..8.4......3...9...5.1...6..2..7........3.....6...1..........7.4.......3.end Sample Output 5273894168194267354367518293756921841......

locusxt
2014/01/17
0
0

##KidSudoku 儿童数独## I'm a father of a lovely 4-years old girl. I wanna give her a gift -- a sudoku game, but she is too young to play the real sudoku, so I wanna make a simpl......

2015/06/11
0
0

2015/05/26
755
0
JavaScript 游戏引擎--GMP game engine

GMP 是个杰出的制作基于 sprite ，2D 游戏的引擎。它可以很容易就设计出最 retro 风格的街机游戏，同时也是很好的制作游戏的工具（比如 sudoku 和 gogopop）。 在线演示...

2014/02/19
1K
0

2011/04/06
11.1K
4

OSChina 周三乱弹 —— 有一天考拉麻麻拉肚子了

Osc乱弹歌单（2018）请戳（这里） 【今日歌曲】 @瘟神灬念 ：分享周汇洋的单曲《Man Srae（曼斯拉之舞）》:美滋滋。。。。 手机党少年们想听歌，请使劲儿戳（这里） 我听了一下 赶紧关了， ...

36分钟前
9
3
oh-my-zsh 自定义

GitHub 地址 基于 oh-my-zsh 的自定义配置，增加了一些个人常用插件与皮肤。 采用的是 git submodule 来维护，包括 oh-my-zsh，之所以这么搞，主要是手头有多台 linux 需要维护， 每台机器、...

6
0
Docker安装踩坑：E_FAIL 0x80004005的解决

karma123

6
0
js垃圾回收机制和引起内存泄漏的操作

JS的垃圾回收机制了解吗? Js具有自动垃圾回收机制。垃圾收集器会按照固定的时间间隔周期性的执行。 JS中最常见的垃圾回收方式是标记清除。 工作原理：是当变量进入环境时，将这个变量标记为“...

Jack088

18
0

em_aaron

27
0