文档章节

Algorithm Game-Sudoku

Wayen007
 Wayen007
发布于 2017/08/29 14:03
字数 604
阅读 1
收藏 0

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.

          

  • Answer :

          

...and its solution numbers marked in red.

C# Codes:

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

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");
            }
            Console.ReadKey();


        }

        /// <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
粉丝 0
博文 4
码字总数 1466
作品 0
闵行
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

##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
儿童数独--kidsudoku

酷爸联盟的第一个项目,适合2岁以上儿童玩的数独游戏,目前已经告于段落。 KidSudoku 儿童数独 I'm a father of a lovely 4-years old girl. I wanna give her a gift -- a sudoku game, but...

吴珂
2015/05/26
755
0
用 jQuery 开发游戏 —— 25 款 jQuery 游戏插件

现在是游戏时间,收集了 25款jQuery开发的游戏,从数独、迷宫、推箱子到赛车,还有超级玛丽哦,尝试一下吧。 1. jQuery Puzzle Mini Game This is a simple puzzle mini game. What you onl...

红薯
2011/04/06
10.7K
4
JavaScript 游戏引擎--GMP game engine

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

叶秀兰
2014/02/19
1K
0

没有更多内容

加载失败,请刷新页面

加载更多

OSChina 周一乱弹 —— 鱼生不值得

Osc乱弹歌单(2018)请戳(这里) 【今日歌曲】 @瘟神灬念:分享新裤子的单曲《没有理想的人不伤心 (Remix版)》: 《没有理想的人不伤心 (Remix版)》- 新裤子 手机党少年们想听歌,请使劲儿戳...

小小编辑
13分钟前
3
3
arts-week10

Algorithm 905. Sort Array By Parity - LeetCode Review Who’s Afraid of the Big Bad Preloader? 一文读懂前端缓存 一个网络请求3个步骤:请求,处理,响应,而前端缓存主要在请求处响应这两步...

yysue
今天
4
0
00.编译OpenJDK-8u40的整个过程

前言 历经2天的折腾总算把OpenJDK给编译成功了,要说为啥搞这个,还得从面试说起,最近出去面试经常被问到JVM的相关东西,总感觉自己以前学的太浅薄,所以回来就打算深入学习,目标把《深入理...

凌晨一点
今天
5
0
python: 一些关于元组的碎碎念

初始化元组的时候,尤其是元组里面只有一个元素的时候,会出现一些很蛋疼的情况: def checkContentAndType(obj): print(obj) print(type(obj))if __name__=="__main__": tu...

Oh_really
昨天
6
2
jvm crash分析工具

介绍一款非常好用的jvm crash分析工具,当jvm挂掉时,会产生hs_err_pid.log。里面记录了jvm当时的运行状态以及错误信息,但是内容量比较庞大,不好分析。所以我们要借助工具来帮我们。 Cras...

xpbob
昨天
176
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部