文档章节

Algorithm Game-Sudoku

Wayen007
 Wayen007
发布于 2017/08/29 14:03
字数 604
阅读 1
收藏 0
点赞 0
评论 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
[LeetCode] Flip Game 翻转游戏之二

You are playing the following Flip Game with your friend: Given a string that contains only these two characters: and , you and your friend take turns to flip twoconsecutive int......

机器的心脏
2017/12/15
0
0
验证数独棋盘

原题   Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.   The Sudoku board could be partially filled, where empty cells are filled with the charact......

一贱书生
2016/12/14
6
0
poj 2676 Sudoku

Sudoku Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22158 Accepted: 10496 Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 ......

fire_to_cheat_
03/31
0
0
Cocos实战篇——作品集

【唠叨】 之前学校的WP课程大作业需要交好几个WP应用,所以随手写了几个游戏。 现在有空,把它分享给大家。 【小游戏目录】 CrazyDodge PaperPlane Sudoku 【CrazyDodge】 0、源码 https://...

shahdza
2015/03/19
0
0
Serpent.AI – 游戏代理框架(Python)

Serpent.AI - 游戏代理框架(Python) Serpent.AI是一个简单而强大的新颖框架,可帮助开发人员创建游戏代理。将您拥有的任何视频游戏变成一个成熟的实验的沙箱环境,所有这些都是熟悉的Pytho...

不知道自己是谁
2017/09/23
0
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

zk实战--rpc框架集群化

在看此篇内容时需要浏览下面内容 netty实战--手写rpc框架 前文功能简介以及功能扩充 利用netty来实现一个点对点的rpc调用。客户端和服务端都是靠手写地址进行socket同学的,无法1对多,也无法...

xpbob
11分钟前
7
0
springboot 发送邮件

获取授权码 添加配置 # 账号和密码spring.mail.username=aaa@qq.comspring.mail.password=bbb# 服务器地址spring.mail.host=smtp.qq.comspring.mail.properties.mail.smtp.ssl.en...

阿豪boy
11分钟前
0
0
如何使用GNU Ring?

文章名:如何使用GNU Ring? 作者:冰焰火灵X 1079092922@qq.com 文章许可:CC BY-SA 4.0 ##1. 安装 下载GNU Ring 点击左边选择你的系统版本(这里以 GNU/Linux 为例,我使用的是Mint 18.3)...

ICE冰焰火灵X
14分钟前
1
0
深入理解springMVC

什么是spring MVC Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块。使用 Spring 可插入的 MVC 架构,从而...

Java填坑之路
20分钟前
1
0
《射雕英雄传》书摘

1. 我虽是个飘泊江湖的贫家女子,可不是低三下四、不知自爱之人。你如真心爱我,须当敬我重我。我此生决无别念,就是钢刀架颈,也决意跟定了你。将来……将来如有洞房花烛之日,自然……自能...

k91191
30分钟前
0
0
解决:modal中datePicker 选中时,会触发modal的hidden.bs.modal事件

最近项目中发现了一个bug,具体表现为选中模态框上datepicker组件上的日期时,会触发模态框的关闭事件,导致数据编辑无法正常进行。网上搜索了下,解决方法如下: $('.datepicker').on('hid...

Funcy1122
34分钟前
0
0
Redis分布式锁的正确实现方式

前言 分布式锁一般有三种实现方式: 1.数据库乐观锁 2.基于Redis的分布式锁; 3.基于Zookeeper的分布式锁。本篇博客将介绍第二种方式,基于Redis实现分布式锁。虽然网上已经有各种介绍Redis...

大海201506
今天
0
0
ClassNotFoundException: javax.el.ELManager

这个是因为tomcat7中的el-api2.2,有些版本太低,建议升级tomcat到8.0,利用el-api3.0就会解决这个问题。

无语年华
今天
0
0
Jvm堆内存的划分结构和优化,垃圾回收详解(详细解答篇)

在JVM中堆空间划分如下图所示 上图中,刻画了Java程序运行时的堆空间,可以简述成如下2条 1.JVM中堆空间可以分成三个大区,新生代、老年代、永久代 2.新生代可以划分为三个区,Eden区,两个幸...

嘻哈开发者
今天
1
0
CentOS 7.4 设置系统字符编码

1.语言变量LANG在 /etc/locale 文件中。 2.可以通过/ect/profile 来修改LC_TYPE 变量的值 添加如下代码 export LC_ALL="zh_CN.GBK" export LANG="zh_CN.GBK" 到profile文件中,变量的可以修改...

qimh
今天
1
0

没有更多内容

加载失败,请刷新页面

加载更多

下一页

返回顶部
顶部